aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Leon Sandøy <[email protected]>2020-07-12 15:27:23 +0200
committerGravatar Leon Sandøy <[email protected]>2020-07-12 15:27:23 +0200
commit3fce243e15996eb81157c198544fcc705e46e1e6 (patch)
tree20f4cf2073230bd8822104682e186fa8a1ccfd1c
parentRefactor python_news.py to use webhook util. (diff)
Relay all DMs and embeds to #dm-log.
https://github.com/python-discord/bot/issues/667
-rw-r--r--bot/__main__.py1
-rw-r--r--bot/cogs/dm_relay.py66
2 files changed, 67 insertions, 0 deletions
diff --git a/bot/__main__.py b/bot/__main__.py
index 37e62c2f1..49388455a 100644
--- a/bot/__main__.py
+++ b/bot/__main__.py
@@ -54,6 +54,7 @@ bot.load_extension("bot.cogs.verification")
# Feature cogs
bot.load_extension("bot.cogs.alias")
bot.load_extension("bot.cogs.defcon")
+bot.load_extension("bot.cogs.dm_relay")
bot.load_extension("bot.cogs.duck_pond")
bot.load_extension("bot.cogs.eval")
bot.load_extension("bot.cogs.information")
diff --git a/bot/cogs/dm_relay.py b/bot/cogs/dm_relay.py
new file mode 100644
index 000000000..32ac0e4ee
--- /dev/null
+++ b/bot/cogs/dm_relay.py
@@ -0,0 +1,66 @@
+import logging
+
+import discord
+from discord import Color
+from discord.ext.commands import Cog
+
+from bot import constants
+from bot.bot import Bot
+from bot.utils.messages import send_attachments
+from bot.utils.webhooks import send_webhook
+
+log = logging.getLogger(__name__)
+
+
+class DMRelay(Cog):
+ """Debug logging module."""
+
+ def __init__(self, bot: Bot):
+ self.bot = bot
+ self.webhook_id = constants.Webhooks.dm_log
+ self.webhook = None
+ self.bot.loop.create_task(self.fetch_webhook())
+
+ async def fetch_webhook(self) -> None:
+ """Fetches the webhook object, so we can post to it."""
+ await self.bot.wait_until_guild_available()
+
+ try:
+ self.webhook = await self.bot.fetch_webhook(self.webhook_id)
+ except discord.HTTPException:
+ log.exception(f"Failed to fetch webhook with id `{self.webhook_id}`")
+
+ @Cog.listener()
+ async def on_message(self, message: discord.Message) -> None:
+ """Relays the message's content and attachments to the dm_log channel."""
+ clean_content = message.clean_content
+ if clean_content:
+ await send_webhook(
+ webhook=self.webhook,
+ content=message.clean_content,
+ username=message.author.display_name,
+ avatar_url=message.author.avatar_url
+ )
+
+ # Handle any attachments
+ if message.attachments:
+ try:
+ await send_attachments(message, self.webhook)
+ except (discord.errors.Forbidden, discord.errors.NotFound):
+ e = discord.Embed(
+ description=":x: **This message contained an attachment, but it could not be retrieved**",
+ color=Color.red()
+ )
+ await send_webhook(
+ webhook=self.webhook,
+ embed=e,
+ username=message.author.display_name,
+ avatar_url=message.author.avatar_url
+ )
+ except discord.HTTPException:
+ log.exception("Failed to send an attachment to the webhook")
+
+
+def setup(bot: Bot) -> None:
+ """Load the DMRelay cog."""
+ bot.add_cog(DMRelay(bot))