Parcourir la source

Fix race condition which could cause crahses on start

Alex Taber il y a 6 ans
Parent
révision
e160be4b27
1 fichiers modifiés avec 16 ajouts et 4 suppressions
  1. 16
    4
      app.py

+ 16
- 4
app.py Voir le fichier

@@ -15,9 +15,11 @@
15 15
 """
16 16
 
17 17
 from matrix_client.client import MatrixClient
18
+
18 19
 import discord
19
-from discord import Webhook, RequestsWebhookAdapter
20
+import requests
20 21
 import re
22
+
21 23
 from config import *
22 24
 
23 25
 matrix_client = MatrixClient(matrix_homeserver)
@@ -41,7 +43,6 @@ def prepare_matrix_content(message):
41 43
 guild = None
42 44
 channel = None
43 45
 emojis = {}
44
-webhook = None
45 46
 
46 47
 @discord_client.event
47 48
 async def on_ready():
@@ -52,7 +53,6 @@ async def on_ready():
52 53
 	channel = discord_client.get_channel(discord_channel)
53 54
 	guild = channel.guild
54 55
 	emojis = {":{}:".format(emoji.name): "<:{}:{}>".format(emoji.name, emoji.id) for emoji in guild.emojis}
55
-	webhook = Webhook.from_url(webhook_url, adapter=RequestsWebhookAdapter())
56 56
 
57 57
 @discord_client.event
58 58
 async def on_message(message):
@@ -86,7 +86,19 @@ async def on_raw_message_delete(paylod):
86 86
 
87 87
 def send_webhook(username, avatar_url, content, matrix_id):
88 88
 	unmatched_messages_cache[username] = matrix_id;
89
-	webhook.send(content=content, username=username, avatar_url=avatar_url)
89
+
90
+	payload = {}
91
+
92
+	payload['content'] = content
93
+	payload['avatar_url'] = avatar_url
94
+	payload['username'] = username
95
+
96
+	for tries in range(5):
97
+		r = requests.post(webhook_url, data = payload)
98
+		if r.ok:
99
+			return
100
+		else:
101
+			print(r.status_code)
90 102
 
91 103
 def prepare_discord_content(content):
92 104
 	content = content.replace("@everyone", "@\u200Beveryone")