aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2019-03-11 20:50:12 +0100
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2019-03-11 20:50:12 +0100
commit060ad955210572d5869807dad5f02dc7615a89b5 (patch)
tree2df88b2333802e0b8eae1b1ccddcd788c34f3447
parent9e5798e922dc03fced416bdb4fcb2e9a213a9d48 (diff)
downloaddiscord-060ad955210572d5869807dad5f02dc7615a89b5.zip
discord-060ad955210572d5869807dad5f02dc7615a89b5.tar.bz2
Protects access to the socket using a mutex.
-rw-r--r--jh-discord.py55
1 files changed, 34 insertions, 21 deletions
diff --git a/jh-discord.py b/jh-discord.py
index 96e1e92..d9b25ad 100644
--- a/jh-discord.py
+++ b/jh-discord.py
@@ -3,6 +3,7 @@ import asyncio
import argparse
import socket
import threading
+from threading import Lock
import sys
import time
@@ -40,10 +41,12 @@ parser.add_argument(
args = parser.parse_args()
is_connected = False
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
-
client = discord.Client()
+server_mutex = Lock()
def get_jh_reply ():
+ global server
+
is_done = False
result = ""
jh_reply = b""
@@ -74,6 +77,7 @@ async def on_ready():
global is_connected
global server
global args
+
print('Logged in as')
print(client.user.name)
print(client.user.id)
@@ -86,34 +90,43 @@ async def on_ready():
async def on_message(message):
global server
global args
+ global server_mutex
if (message.author.id == client.user.id):
return
- server.sendall(
- b"?RLR "
- + bytes(message.clean_content.replace('\n', ' '), "utf8")
- + b"\n"
- )
+ has_lock = False
+ try:
+ msg = bytes(message.clean_content.replace('\n', ' '), "utf8")
- result = get_jh_reply()
+ server_mutex.acquire()
+ has_lock = True
+ server.sendall(b"?RLR " + msg + b"\n")
- if (args.print_chat):
- print(
- str(message.server)
- + "#"
- + str(message.channel.name)
- + " <"
- + str(message.author.name)
- + "> "
- + str(message.clean_content)
- )
+ result = get_jh_reply()
+ server_mutex.release()
+ has_lock = False
- if (len(result) > 0):
- print("#" + str(message.channel.name) + " <- " + result)
+ if (args.print_chat):
+ print(
+ str(message.server)
+ + "#"
+ + str(message.channel.name)
+ + " <"
+ + str(message.author.name)
+ + "> "
+ + str(message.clean_content)
+ )
- if (len(result) > 0):
- await client.send_message(message.channel, result)
+ if (len(result) > 0):
+ print("#" + str(message.channel.name) + " <- " + result)
+
+ if (len(result) > 0):
+ await client.send_message(message.channel, result)
+ except Exception as exception:
+ if (has_lock):
+ server_mutex.release()
+ print(exception)
def exit_if_disconnected ():
while True: