Skip to content

Commit

Permalink
Add send to discord by send_loop(yopen)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgDora56 committed Aug 1, 2024
1 parent ad9d2ef commit 9dc619b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ async def on_message(message):
if 0 < len(command[1]) <= 20 and \
0 < len(command[2]) <= 20:
try:
YOpener(command[1], command[2], message.channel)
YOpener(command[1], command[2], message.channel).run()
except:
pass # 握りつぶして良い
else:
Expand Down
23 changes: 17 additions & 6 deletions openroom.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,42 @@ def openroom(user, pw):
return roomno

class YOpener:
def __init__(self, roomname, pw, dchannel):
YQUI_WS_URI = "ws://yqui.net/ws"

async def __init__(self, roomname, pw, dchannel):
websocket.enableTrace(False)

self.tryCount = -1
self.joinedRoom = -1
self.status = "lobby" # lobby, waiting
self.roomname = roomname
self.pw = pw
self.channel = dchannel
self.rooms = []
self.message_stock = []

async def run(self):
await self.send_loop()

YQUI_WS_URI = "ws://yqui.net/ws"
self.ws = websocket.WebSocketApp(YQUI_WS_URI,
on_message = lambda ws, msg: self.on_message(ws, msg),
on_error = lambda ws, msg: self.on_error(ws, msg),
on_close = lambda ws: self.on_close(ws))
self.ws.on_open = lambda ws: self.on_open(ws)
self.ws.run_forever()

# FIXME: Discord送信がうまくいかない足掻き
async def send_loop(self):
while True:
if len(self.message_stock) > 0:
msg = self.message_stock.pop(0)
await self.channel.send(msg)
await asyncio.sleep(0.1)

def send_to_channel(self, msg):
if self.channel == None:
print("Send: " + msg)
else:
asyncio.run(self.channel.send(msg))

self.message_stock.append(msg)
def on_message(self, ws, message):
msg = json.loads(message)
print(msg)
Expand Down

0 comments on commit 9dc619b

Please sign in to comment.