tangled
alpha
login
or
join now
zenfyr.dev
/
xmpp-discord-bridge
0
fork
atom
fork of https://f-hub.org/XMPP/xmpp-discord-bridge
0
fork
atom
overview
issues
pulls
pipelines
xmpp: Use webhook.send
Alexander PapaTutuWawa
4 years ago
3bafcb91
edf70774
+21
-34
1 changed file
expand all
collapse all
unified
split
xmpp_discord_bridge
main.py
+21
-34
xmpp_discord_bridge/main.py
···
16
16
from slixmpp.exceptions import XMPPError, IqError
17
17
from slixmpp.xmlstream import register_stanza_plugin
18
18
from slixmpp.jid import JID
19
19
-
from discord import Status
19
19
+
from discord import Status, Embed
20
20
import requests
21
21
22
22
from xmpp_discord_bridge.slixmpp.oob import OOBData
···
194
194
str(vcard)))
195
195
196
196
# Aquire a webhook
197
197
-
webhook_url = ""
197
197
+
wh = ""
198
198
for webhook in await dchannel.webhooks():
199
199
if webhook.name == "discord-xmpp-bridge":
200
200
-
webhook_url = webhook.url
200
200
+
wh webhook
201
201
break
202
202
-
if not webhook_url:
203
203
-
webhook = await dchannel.create_webhook(name="discord-xmpp-bridge",
204
204
-
reason="Bridging Discord and XMPP")
205
205
-
webhook_url = webhook.url
206
206
-
self._webhooks[muc] = webhook_url
202
202
+
if not wh:
203
203
+
wh = await dchannel.create_webhook(name="discord-xmpp-bridge",
204
204
+
reason="Bridging Discord and XMPP")
205
205
+
self._webhooks[muc] = wh
207
206
208
207
# TODO: Check if we need to set it
209
208
# Make sure our virtual users can join
···
237
236
if not message["to"] == self._bot_jid_full:
238
237
return
239
238
240
240
-
webhook = {
241
241
-
"content": message["body"],
242
242
-
"username": message["from"].resource
243
243
-
}
244
244
-
245
245
-
if self._relay_xmpp_avatars and self._avatars.get_avatar_url(message["from"]):
246
246
-
webhook["avatar_url"] = self._avatars.get_avatar_url(message["from"])
239
239
+
content = message["body"]
240
240
+
embed = None
247
241
248
242
# Look for mentions and replace them
249
243
guild, channel = self._muc_map[muc]
250
244
for member in self._discord.get_guild(guild).get_channel(channel).members:
251
245
self._logger.debug("Checking %s", member.display_name)
252
252
-
if "@" + member.display_name in webhook["content"]:
246
246
+
if "@" + member.display_name in content:
253
247
self._logger.debug("Found mention for %s. Replaceing.",
254
248
member.display_name)
255
255
-
webhook["content"] = webhook["content"].replace("@" + member.display_name,
256
256
-
member.mention)
249
249
+
content = content.replace("@" + member.display_name,
250
250
+
member.mention)
257
251
258
252
if message["oob"]["url"] and message["body"] == message["oob"]["url"]:
259
259
-
webhook["embed"] = [{
260
260
-
"type": "rich",
261
261
-
"url": message["oob"]["url"]
262
262
-
}]
253
253
+
embed = Embed(url=message["oob"]["url"], type="rich")
263
254
264
264
-
self._logger.debug("Webhook: %s", str(webhook))
265
265
-
266
266
-
req = requests.post(self._webhooks[muc],
267
267
-
data=webhook,
268
268
-
headers={
269
269
-
"Content-Type": "application/json"
270
270
-
})
271
271
-
if req.status_code != 200:
272
272
-
self._logger.error("Failed to POST to %s with content %s: %s",
273
273
-
self._webhooks[muc],
274
274
-
str(webhook),
275
275
-
req.text)
255
255
+
try:
256
256
+
await self._webhooks[muc].send(content=content,
257
257
+
username=message["from"].resource,
258
258
+
avatar_url=self._avatars.get_avatar_url(message["from"]),
259
259
+
embed=embed)
260
260
+
except Exception as err:
261
261
+
self._logger.error("Webhook execution failed: %s",
262
262
+
err)
276
263
277
264
def virtual_user_update_presence(self, muc, uid, pshow, pstatus=None):
278
265
"""