aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Leon Sandøy <[email protected]>2020-07-15 10:39:07 +0200
committerGravatar Leon Sandøy <[email protected]>2020-07-15 10:39:31 +0200
commit403572b83cf3faea9068a25cb09e809d993c1514 (patch)
treeef2cb0d49d383a4fd19d883cf3934f68ef2c4654
parentVes' refactor (diff)
Create a UserMentionOrID converter.
When we're using the !reply command, using a regular UserConverter is somewhat problematic. For example, if I wanted to send the message "lemon loves you", then I'd try to write `!reply lemon loves you` - however, the optional User converter would then try to convert `lemon` into a User, which it would successfully do since there's like 60 lemons on our server. As a result, the message "loves you" would be sent to a user called lemon.. god knows which one. To solve this bit of ambiguity, I introduce a new converter which only converts user mentions or user IDs into User, not strings that may be intended as part of the message you are sending. https://github.com/python-discord/bot/issues/1041
-rw-r--r--bot/cogs/dm_relay.py3
-rw-r--r--bot/converters.py22
2 files changed, 24 insertions, 1 deletions
diff --git a/bot/cogs/dm_relay.py b/bot/cogs/dm_relay.py
index 0c3eddf42..c5a3dba22 100644
--- a/bot/cogs/dm_relay.py
+++ b/bot/cogs/dm_relay.py
@@ -8,6 +8,7 @@ from discord.ext.commands import Cog
from bot import constants
from bot.bot import Bot
+from bot.converters import UserMentionOrID
from bot.utils import RedisCache
from bot.utils.checks import in_whitelist_check, with_role_check
from bot.utils.messages import send_attachments
@@ -29,7 +30,7 @@ class DMRelay(Cog):
self.bot.loop.create_task(self.fetch_webhook())
@commands.command(aliases=("reply",))
- async def send_dm(self, ctx: commands.Context, member: Optional[discord.Member], *, message: str) -> None:
+ async def send_dm(self, ctx: commands.Context, member: Optional[UserMentionOrID], *, message: str) -> None:
"""
Allows you to send a DM to a user from the bot.
diff --git a/bot/converters.py b/bot/converters.py
index 898822165..7c62f92dd 100644
--- a/bot/converters.py
+++ b/bot/converters.py
@@ -330,6 +330,28 @@ def proxy_user(user_id: str) -> discord.Object:
return user
+class UserMentionOrID(UserConverter):
+ """
+ Converts to a `discord.User`, but only if a mention or userID is provided.
+
+ Unlike the default `UserConverter`, it does allow conversion from name, or name#descrim.
+
+ This is useful in cases where that lookup strategy would lead to ambiguity.
+ """
+
+ async def convert(self, ctx: Context, argument: str) -> discord.User:
+ """Convert the `arg` to a `discord.User`."""
+ print(argument)
+ match = self._get_id_match(argument) or re.match(r'<@!?([0-9]+)>$', argument)
+
+ print(match)
+
+ if match is not None:
+ return await super().convert(ctx, argument)
+ else:
+ raise BadArgument(f"`{argument}` is not a User mention or a User ID.")
+
+
class FetchedUser(UserConverter):
"""
Converts to a `discord.User` or, if it fails, a `discord.Object`.