-
-
Notifications
You must be signed in to change notification settings - Fork 751
Relay all DMs sent to the bot to #dm_log #1041
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
cb5e361
Add the #dm_log ID to constants.
9042325
Refactor Duck Pond embed sender to be a util.
ef65033
Refactor python_news.py to use webhook util.
3fce243
Relay all DMs and embeds to #dm-log.
5007e73
Replace channel ID with webhook ID for dm_log.
4349fde
Only relay DMs, and only from humans.
df1730e
Fix DuckPond tests now that send_webhook is gone.
aaf8db7
Add a way to respond to DMs.
ed23687
Better docstring for DMRelay cog.
ab15466
Add avatar_url in python_news.py
87c2ef7
Only mods+ may use the commands in this cog.
3119369
Don't run on_message if self.webhook is None.
aa0b20b
Remove redundant clean_content variable.
ea62b6b
Store last DM user in RedisCache.
02f7799
Merge branch 'master' into dm_relay
lemonsaurus 042f472
Remove caching of last_dm_user.
e940cef
Merge branch 'master' into dm_relay
lemonsaurus d8b570c
Merge branch 'master' into dm_relay
kosayoda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import logging | ||
|
|
||
| import discord | ||
| from discord import Color | ||
| from discord.ext import commands | ||
| from discord.ext.commands import Cog | ||
|
|
||
| from bot import constants | ||
| from bot.bot import Bot | ||
| from bot.utils.checks import in_whitelist_check, with_role_check | ||
| from bot.utils.messages import send_attachments | ||
| from bot.utils.webhooks import send_webhook | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class DMRelay(Cog): | ||
| """Relay direct messages to and from the bot.""" | ||
|
|
||
| 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()) | ||
|
|
||
| @commands.command(aliases=("reply",)) | ||
| async def send_dm(self, ctx: commands.Context, member: discord.Member, *, message: str) -> None: | ||
| """ | ||
| Allows you to send a DM to a user from the bot. | ||
|
|
||
| A `member` must be provided. | ||
|
|
||
| This feature should be used extremely sparingly. Use ModMail if you need to have a serious | ||
| conversation with a user. This is just for responding to extraordinary DMs, having a little | ||
| fun with users, and telling people they are DMing the wrong bot. | ||
|
|
||
| NOTE: This feature will be removed if it is overused. | ||
| """ | ||
| try: | ||
| await member.send(message) | ||
| await ctx.message.add_reaction("✅") | ||
| return | ||
|
|
||
| except discord.errors.Forbidden: | ||
| log.debug("User has disabled DMs.") | ||
| await ctx.message.add_reaction("❌") | ||
|
|
||
| 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.""" | ||
| # Only relay DMs from humans | ||
| if message.author.bot or message.guild or self.webhook is None: | ||
| return | ||
|
|
||
| if message.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 cog_check(self, ctx: commands.Context) -> bool: | ||
| """Only allow moderators to invoke the commands in this cog.""" | ||
| checks = [ | ||
| with_role_check(ctx, *constants.MODERATION_ROLES), | ||
| in_whitelist_check( | ||
| ctx, | ||
| channels=[constants.Channels.dm_log], | ||
| redirect=None, | ||
| fail_silently=True, | ||
| ) | ||
| ] | ||
| return all(checks) | ||
|
|
||
|
|
||
| def setup(bot: Bot) -> None: | ||
| """Load the DMRelay cog.""" | ||
| bot.add_cog(DMRelay(bot)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import logging | ||
| from typing import Optional | ||
|
|
||
| import discord | ||
| from discord import Embed | ||
|
|
||
| from bot.utils.messages import sub_clyde | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| async def send_webhook( | ||
| webhook: discord.Webhook, | ||
| content: Optional[str] = None, | ||
| username: Optional[str] = None, | ||
| avatar_url: Optional[str] = None, | ||
| embed: Optional[Embed] = None, | ||
| wait: Optional[bool] = False | ||
| ) -> discord.Message: | ||
|
lemonsaurus marked this conversation as resolved.
|
||
| """ | ||
| Send a message using the provided webhook. | ||
|
|
||
| This uses sub_clyde() and tries for an HTTPException to ensure it doesn't crash. | ||
| """ | ||
| try: | ||
| return await webhook.send( | ||
| content=content, | ||
| username=sub_clyde(username), | ||
| avatar_url=avatar_url, | ||
| embed=embed, | ||
| wait=wait, | ||
| ) | ||
| except discord.HTTPException: | ||
| log.exception("Failed to send a message to the webhook!") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.