forked from nh-server/Kurisu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Monkey patch member and user converters to not convert from usernames
- Loading branch information
1 parent
035909c
commit 5cce3da
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,51 @@ | ||
# Patched discord.py methods for monkey patching | ||
|
||
import discord | ||
import re | ||
|
||
from discord.ext import commands | ||
from discord.ext.commands import MemberNotFound, UserNotFound | ||
from discord.ext.commands._types import BotT | ||
from discord.ext.commands.converter import _get_from_guilds, _utils_get | ||
|
||
|
||
async def patched_member_convert(self, ctx: commands.Context[BotT], argument: str) -> discord.Member: | ||
bot = ctx.bot | ||
match = self._get_id_match(argument) or re.match(r'<@!?([0-9]{15,20})>$', argument) | ||
guild = ctx.guild | ||
if match is None: | ||
raise MemberNotFound(argument) | ||
else: | ||
user_id = int(match.group(1)) | ||
if guild: | ||
result = guild.get_member(user_id) or _utils_get(ctx.message.mentions, id=user_id) | ||
else: | ||
result = _get_from_guilds(bot, 'get_member', user_id) | ||
|
||
if not isinstance(result, discord.Member): | ||
if guild is None: | ||
raise MemberNotFound(argument) | ||
|
||
if user_id is not None: | ||
result = await self.query_member_by_id(bot, guild, user_id) | ||
|
||
if not result: | ||
raise MemberNotFound(argument) | ||
|
||
return result | ||
|
||
|
||
async def patched_user_convert(self, ctx: commands.Context[BotT], argument: str) -> discord.User: | ||
match = self._get_id_match(argument) or re.match(r'<@!?([0-9]{15,20})>$', argument) | ||
|
||
if match is not None: | ||
user_id = int(match.group(1)) | ||
result = ctx.bot.get_user(user_id) or _utils_get(ctx.message.mentions, id=user_id) | ||
if result is None: | ||
try: | ||
result = await ctx.bot.fetch_user(user_id) | ||
except discord.HTTPException: | ||
raise UserNotFound(argument) from None | ||
|
||
return result # type: ignore | ||
raise UserNotFound(argument) |