aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Leon Sandøy <[email protected]>2020-07-13 21:09:41 +0200
committerGravatar Leon Sandøy <[email protected]>2020-07-13 21:09:41 +0200
commitea62b6bae85113be913101a41053c91497b23c9a (patch)
treeb7d9d6e2a89619697a65c4d1408d07315888dcca
parentRemove redundant clean_content variable. (diff)
Store last DM user in RedisCache.
Also now catches the exception if a user has disabled DMs, and adds a red cross reaction. https://github.com/python-discord/bot/issues/667
-rw-r--r--bot/cogs/dm_relay.py34
1 files changed, 22 insertions, 12 deletions
diff --git a/bot/cogs/dm_relay.py b/bot/cogs/dm_relay.py
index 494c71066..3fce52b93 100644
--- a/bot/cogs/dm_relay.py
+++ b/bot/cogs/dm_relay.py
@@ -9,6 +9,7 @@ from discord.ext.commands import Cog
from bot import constants
from bot.bot import Bot
from bot.constants import MODERATION_ROLES
+from bot.utils import RedisCache
from bot.utils.checks import with_role_check
from bot.utils.messages import send_attachments
from bot.utils.webhooks import send_webhook
@@ -19,12 +20,14 @@ log = logging.getLogger(__name__)
class DMRelay(Cog):
"""Relay direct messages to and from the bot."""
+ # RedisCache[str, t.Union[discord.User.id, discord.Member.id]]
+ dm_cache = RedisCache()
+
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())
- self.last_dm_user = None
@commands.command(aliases=("reply",))
async def send_dm(self, ctx: commands.Context, member: Optional[discord.Member], *, message: str) -> None:
@@ -39,16 +42,23 @@ class DMRelay(Cog):
NOTE: This feature will be removed if it is overused.
"""
- if member:
- await member.send(message)
- await ctx.message.add_reaction("✅")
- return
- elif self.last_dm_user:
- await self.last_dm_user.send(message)
- await ctx.message.add_reaction("✅")
- return
- else:
- log.debug("Unable to send a DM to the user.")
+ user_id = await self.dm_cache.get("last_user")
+ last_dm_user = ctx.guild.get_member(user_id) if user_id else None
+
+ try:
+ if member:
+ await member.send(message)
+ await ctx.message.add_reaction("✅")
+ return
+ elif last_dm_user:
+ await last_dm_user.send(message)
+ await ctx.message.add_reaction("✅")
+ return
+ else:
+ log.debug("This bot has never gotten a DM, or the RedisCache has been cleared.")
+ await ctx.message.add_reaction("❌")
+ except discord.errors.Forbidden:
+ log.debug("User has disabled DMs.")
await ctx.message.add_reaction("❌")
async def fetch_webhook(self) -> None:
@@ -74,7 +84,7 @@ class DMRelay(Cog):
username=message.author.display_name,
avatar_url=message.author.avatar_url
)
- self.last_dm_user = message.author
+ await self.dm_cache.set("last_user", message.author.id)
# Handle any attachments
if message.attachments: