xmpp: Use webhook.send

This commit is contained in:
PapaTutuWawa 2021-09-16 18:29:55 +02:00
parent edf70774a2
commit 3bafcb916c

View File

@ -16,7 +16,7 @@ from slixmpp.componentxmpp import ComponentXMPP
from slixmpp.exceptions import XMPPError, IqError
from slixmpp.xmlstream import register_stanza_plugin
from slixmpp.jid import JID
from discord import Status
from discord import Status, Embed
import requests
from xmpp_discord_bridge.slixmpp.oob import OOBData
@ -194,16 +194,15 @@ class BridgeComponent(ComponentXMPP):
str(vcard)))
# Aquire a webhook
webhook_url = ""
wh = ""
for webhook in await dchannel.webhooks():
if webhook.name == "discord-xmpp-bridge":
webhook_url = webhook.url
wh webhook
break
if not webhook_url:
webhook = await dchannel.create_webhook(name="discord-xmpp-bridge",
reason="Bridging Discord and XMPP")
webhook_url = webhook.url
self._webhooks[muc] = webhook_url
if not wh:
wh = await dchannel.create_webhook(name="discord-xmpp-bridge",
reason="Bridging Discord and XMPP")
self._webhooks[muc] = wh
# TODO: Check if we need to set it
# Make sure our virtual users can join
@ -237,42 +236,30 @@ class BridgeComponent(ComponentXMPP):
if not message["to"] == self._bot_jid_full:
return
webhook = {
"content": message["body"],
"username": message["from"].resource
}
if self._relay_xmpp_avatars and self._avatars.get_avatar_url(message["from"]):
webhook["avatar_url"] = self._avatars.get_avatar_url(message["from"])
content = message["body"]
embed = None
# Look for mentions and replace them
guild, channel = self._muc_map[muc]
for member in self._discord.get_guild(guild).get_channel(channel).members:
self._logger.debug("Checking %s", member.display_name)
if "@" + member.display_name in webhook["content"]:
if "@" + member.display_name in content:
self._logger.debug("Found mention for %s. Replaceing.",
member.display_name)
webhook["content"] = webhook["content"].replace("@" + member.display_name,
member.mention)
content = content.replace("@" + member.display_name,
member.mention)
if message["oob"]["url"] and message["body"] == message["oob"]["url"]:
webhook["embed"] = [{
"type": "rich",
"url": message["oob"]["url"]
}]
embed = Embed(url=message["oob"]["url"], type="rich")
self._logger.debug("Webhook: %s", str(webhook))
req = requests.post(self._webhooks[muc],
data=webhook,
headers={
"Content-Type": "application/json"
})
if req.status_code != 200:
self._logger.error("Failed to POST to %s with content %s: %s",
self._webhooks[muc],
str(webhook),
req.text)
try:
await self._webhooks[muc].send(content=content,
username=message["from"].resource,
avatar_url=self._avatars.get_avatar_url(message["from"]),
embed=embed)
except Exception as err:
self._logger.error("Webhook execution failed: %s",
err)
def virtual_user_update_presence(self, muc, uid, pshow, pstatus=None):
"""