|
8 | 8 | import logging |
9 | 9 | import logging.config |
10 | 10 | import sys |
| 11 | +import time |
11 | 12 | from concurrent.futures import ThreadPoolExecutor |
12 | 13 | from typing import Callable, List |
13 | 14 |
|
@@ -185,33 +186,45 @@ def main(): |
185 | 186 | misskey_client = Misskey(conf.MISSKEY_DOMAIN, i=conf.MISSKEY_API_TOKEN) |
186 | 187 |
|
187 | 188 | 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) |
215 | 228 |
|
216 | 229 | asyncio.get_event_loop().run_until_complete(discord_runner()) |
217 | 230 | else: |
|
0 commit comments