This repository was archived by the owner on Jan 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconverters.py
271 lines (241 loc) · 8.06 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
"""
converters
~~~~~~~~~~~~~~~~~~~~~
An extension to provide custom converters that ignore case or include a fallback to the Discord API if not in cache
"""
from discord import utils
from discord.ext.commands.converter import (
MemberConverter,
UserConverter,
RoleConverter,
TextChannelConverter,
VoiceChannelConverter,
CategoryChannelConverter
)
from discord.ext.commands import BadArgument
from fuzzywuzzy import fuzz
import json
class Member(MemberConverter):
"""
Member Converter
---------------------
Converts to a :class:`~discord.Member`.
All lookups are via the local guild. If in a DM context, then the lookup
is done by the global cache.
The lookup strategy is as follows (in order):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name#discrim
4. Lookup by name
5. Lookup by nickname
"""
async def convert(self, ctx, arg):
if not ctx.guild:
raise BadArgument('No guild == No member :c')
if arg == '^':
nextmsg = False
async for m in ctx.channel.history(limit=5):
if m.id == ctx.message.id:
nextmsg = True
elif nextmsg:
return m.author
aliases = json.loads((await ctx.bot.redis.get('aliases', encoding='utf-8')))
if arg.lower() in aliases:
ctx.bot.logger.info(f'$YELLOWFinding alias for $BLUE{arg}')
arg = str(aliases[arg.lower()])
ctx.bot.logger.info(f'$YELLOWAlias found, $BLUE{arg}')
if not ctx.guild.chunked:
try:
return (await ctx.guild.query_members(arg, limit=1))[0]
except Exception:
pass
try:
return await super().convert(ctx, arg)
except BadArgument as e:
if '#' in arg:
args = arg.split('#')
name = args[0]
discrim = args[1]
match = utils.find(lambda m: m.name.lower() == name.lower() and m.discriminator == discrim or m.display_name.lower() == name.lower() and m.discriminator == discrim, ctx.guild.members)
else:
match = utils.find(lambda m: m.name.lower() == arg.lower() or m.display_name.lower() == arg.lower(), ctx.guild.members)
if match == None:
raise BadArgument('Member not found :(')
return match
class User(UserConverter):
"""
User Converter
---------------------
Converts to a :class:`~discord.User`.
All lookups are via the global user cache.
The lookup strategy is as follows (in order):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name#discrim
4. Lookup by name
"""
async def convert(self, ctx, arg):
if arg == '^':
if not ctx.guild:
raise BadArgument('The above operator, ^, can only be used in servers.')
nextmsg = False
async for m in ctx.channel.history(limit=5):
if m.id == ctx.message.id:
nextmsg = True
elif nextmsg:
return await super().convert(ctx, str(m.author.id))
aliases = json.loads((await ctx.bot.redis.get('aliases', encoding='utf-8')))
if arg.lower() in aliases:
ctx.bot.logger.info(f'$YELLOWFinding alias for $BLUE{arg}')
arg = str(aliases[arg.lower()])
ctx.bot.logger.info(f'$YELLOWAlias found, $BLUE{arg}')
try:
return await super().convert(ctx, arg)
except BadArgument as e:
if '#' in arg:
args = arg.split('#')
name = args[0]
discrim = args[1]
match = utils.find(lambda m: m.name.lower() == name.lower() and m.discriminator == discrim or m.display_name.lower() == name.lower() and m.discriminator == discrim, ctx.bot.users)
else:
match = utils.find(lambda m: m.name.lower() == arg.lower() or m.display_name.lower() == arg.lower(), ctx.bot.users)
if match == None:
raise BadArgument('User not found :(')
return match
class Role(RoleConverter):
"""
Role Converter
---------------------
Converts to a :class:`~discord.Role`.
All lookups are via the local guild. If in a DM context, then the lookup
is done by the global cache.
The lookup strategy is as follows (in order):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name
"""
async def convert(self, ctx, arg):
try:
return await super().convert(ctx, arg)
except BadArgument as e:
roles = ctx.guild.roles if ctx.guild else []
for r in roles:
name = r.name
# Remove characters like emojis for better matching
for c in [c for c in name if not ctx.bot.isascii(c)]:
name = name.replace(c, '')
if fuzz.ratio(arg.lower(), name.strip().lower()) >= 80:
return r
# If we get here, either there's no role that matches it or fuzzy wuzzy wasn't a woman so let's just try utils.find
match = utils.find(lambda r: r.name.lower() == arg.lower(), ctx.guild.roles)
if match == None:
raise BadArgument('Role not found :(')
return match
class TextChannel(TextChannelConverter):
"""
Text Channel Converter
---------------------
Converts to a :class:`~discord.TextChannel`.
All lookups are via the local guild. If in a DM context, then the lookup
is done by the global cache.
The lookup strategy is as follows (in order):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name
"""
async def convert(self, ctx, arg):
try:
return await super().convert(ctx, arg)
except BadArgument as e:
match = utils.find(lambda c: c.name.lower() == arg.lower(), ctx.guild.text_channels)
if match == None:
raise BadArgument('Text channel not found :(')
return match
class VoiceChannel(VoiceChannelConverter):
"""
Voice Channel Converter
---------------------
Converts to a :class:`~discord.VoiceChannel`.
All lookups are via the local guild. If in a DM context, then the lookup
is done by the global cache.
The lookup strategy is as follows (in order):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name
"""
async def convert(self, ctx, arg):
try:
return await super().convert(ctx, arg)
except BadArgument as e:
match = utils.find(lambda c: c.name.lower() == arg.lower(), ctx.guild.voice_channels)
if match == None:
raise BadArgument('Voice channel not found :(')
return match
class Category(CategoryChannelConverter):
"""
Category Converter
---------------------
Converts to a :class:`~discord.CategoryChannel`.
All lookups are via the local guild. If in a DM context, then the lookup
is done by the global cache.
The lookup strategy is as follows (in order):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name
"""
async def convert(self, ctx, arg):
try:
return await super().convert(ctx, arg)
except BadArgument as e:
match = utils.find(lambda c: c.name.lower() == arg.lower(), ctx.guild.categories)
if match == None:
raise BadArgument('Category not found :(')
return match
class UserWithFallback(UserConverter):
"""
User Converter
---------------------
Converts to a :class:`~discord.User`.
The lookup strategy is as follows (in order):
1. Lookup by ID.
2. Lookup by mention.
3. Lookup by name#discrim
4. Lookup by name
5. Fallback to Discord API if an ID is provided.
"""
async def convert(self, ctx, arg):
if arg == '^':
if not ctx.guild:
raise BadArgument('The above operator, ^, can only be used in servers.')
nextmsg = False
async for m in ctx.channel.history(limit=5):
if m.id == ctx.message.id:
nextmsg = True
elif nextmsg:
return await super().convert(ctx, str(m.author.id))
aliases = json.loads((await ctx.bot.redis.get('aliases', encoding='utf-8')))
if arg.lower() in aliases:
ctx.bot.logger.info(f'$YELLOWFinding alias for $BLUE{arg}')
arg = str(aliases[arg.lower()])
ctx.bot.logger.info(f'$YELLOWAlias found, $BLUE{arg}')
try:
return await super().convert(ctx, arg)
except BadArgument as e:
if '#' in arg:
args = arg.split('#')
name = args[0]
discrim = args[1]
match = utils.find(lambda m: m.name.lower() == name.lower() and m.discriminator == discrim or m.display_name.lower() == name.lower() and m.discriminator == discrim, ctx.bot.users)
else:
match = utils.find(lambda m: m.name.lower() == arg.lower() or m.display_name.lower() == arg.lower(), ctx.bot.users)
if match == None:
try:
uid = int(arg)
except Exception:
raise BadArgument('User not found :(')
if uid:
try:
return await ctx.bot.fetch_user(uid)
except Exception:
raise BadArgument('User not found :(')
return match