From 3bafcb916ce9d5d006bcfe2f04ba54e2c063e43a Mon Sep 17 00:00:00 2001 From: Alexander PapaTutuWawa Date: Thu, 16 Sep 2021 18:29:55 +0200 Subject: [PATCH] xmpp: Use webhook.send --- xmpp_discord_bridge/main.py | 55 ++++++++++++++----------------------- 1 file changed, 21 insertions(+), 34 deletions(-) diff --git a/xmpp_discord_bridge/main.py b/xmpp_discord_bridge/main.py index ade5e32..515696e 100644 --- a/xmpp_discord_bridge/main.py +++ b/xmpp_discord_bridge/main.py @@ -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): """