Skip to content

Commit 1575ad7

Browse files
authored
Merge branch 'develop' into fix_version
2 parents 51bb3ce + ddc4df5 commit 1575ad7

1 file changed

Lines changed: 40 additions & 27 deletions

File tree

run.py

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import logging
99
import logging.config
1010
import sys
11+
import time
1112
from concurrent.futures import ThreadPoolExecutor
1213
from typing import Callable, List
1314

@@ -185,33 +186,45 @@ def main():
185186
misskey_client = Misskey(conf.MISSKEY_DOMAIN, i=conf.MISSKEY_API_TOKEN)
186187

187188
async def discord_runner():
188-
# pylint: disable=E1101
189-
async with websockets.connect(
190-
"wss://"
191-
+ misskey_client.address
192-
+ "/streaming"
193-
+ "?i="
194-
+ misskey_client.token
195-
) as ws:
196-
await ws.send(
197-
json.dumps(
198-
{"type": "connect", "body": {"channel": "main", "id": "main"}}
199-
)
200-
)
201-
while True:
202-
data = json.loads(await ws.recv())
203-
if data["type"] == "channel" and data["body"]["type"] == "mention":
204-
note = data["body"]["body"]
205-
host = note["user"].get("host")
206-
mentions = note.get("mentions")
207-
if (
208-
(host is None or host == conf.MISSKEY_DOMAIN)
209-
and mentions
210-
and misskey_client.i()["id"] in mentions
211-
):
212-
analyze.analyze_message(
213-
note["text"].replace("\xa0", " ").split(" ", 1)[1]
214-
)(MisskeyClient(misskey_client, note))
189+
while True:
190+
try:
191+
# pylint: disable=E1101
192+
async with websockets.connect(
193+
"wss://"
194+
+ misskey_client.address
195+
+ "/streaming"
196+
+ "?i="
197+
+ misskey_client.token
198+
) as ws:
199+
await ws.send(
200+
json.dumps(
201+
{
202+
"type": "connect",
203+
"body": {"channel": "main", "id": "main"},
204+
}
205+
)
206+
)
207+
while True:
208+
data = json.loads(await ws.recv())
209+
if (
210+
data["type"] == "channel"
211+
and data["body"]["type"] == "mention"
212+
):
213+
note = data["body"]["body"]
214+
host = note["user"].get("host")
215+
mentions = note.get("mentions")
216+
if (
217+
(host is None or host == conf.MISSKEY_DOMAIN)
218+
and mentions
219+
and misskey_client.i()["id"] in mentions
220+
):
221+
analyze.analyze_message(
222+
note["text"]
223+
.replace("\xa0", " ")
224+
.split(" ", 1)[1]
225+
)(MisskeyClient(misskey_client, note))
226+
except websockets.ConnectionClosedError:
227+
time.sleep(1)
215228

216229
asyncio.get_event_loop().run_until_complete(discord_runner())
217230
else:

0 commit comments

Comments
 (0)