forked from nh-server/Kurisu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverters.py
40 lines (36 loc) · 1.79 KB
/
converters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import discord
import re
from discord.ext import commands
class SafeMember(commands.Converter):
# A re-implementation of https://github.com/Rapptz/discord.py/blob/1863a1c6636f53592519320a173ec9573c090c0b/discord/ext/commands/converter.py
async def convert(self, ctx, argument) -> discord.Member:
match = re.match(r'([0-9]{15,21})$', argument) or re.match(r'<@!?([0-9]+)>$', argument)
guild = ctx.guild
if match is None:
# not a mention...
if guild and "#" in argument:
result = guild.get_member_named(argument)
if result is None:
raise commands.errors.BadArgument(f'Member "{argument}" not found. Search terms are case sensitive.')
else:
raise commands.errors.BadArgument('Matches by only nick/username are not allowed.')
else:
user_id = int(match.group(1))
result = guild.get_member(user_id)
if result is None:
raise commands.errors.BadArgument(f'Member "{user_id}" not found.')
return result
class FetchMember(commands.IDConverter):
# Based on MemberConverter to fetch userid in case the userid cant be found in the guild
async def convert(self, ctx, argument) -> discord.User:
bot = ctx.bot
match = self._get_id_match(argument) or re.match(r'<@!?([0-9]+)>$', argument)
guild = ctx.guild
if match is None and guild:
result = guild.get_member_named(argument)
else:
user_id = int(match.group(1))
result = guild.get_member(user_id) or await bot.fetch_user(user_id) if guild else await bot.fetch_user(user_id)
if result is None:
raise commands.errors.BadArgument('User "{}" not found'.format(argument))
return result