Skip to content

Commit 3b91595

Browse files
authored
Merge pull request #21 from eunwoo1104/fix-cache
Apply #18
2 parents e5b4311 + ae908ae commit 3b91595

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

discord_slash/client.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from . import http
66
from . import model
77
from .utils import manage_commands
8+
from inspect import iscoroutinefunction
89

910

1011
class SlashCommand:
@@ -231,7 +232,7 @@ def wrapper(cmd):
231232
return cmd
232233
return wrapper
233234

234-
def process_options(self, guild: discord.Guild, options: list, auto_convert: dict) -> list:
235+
async def process_options(self, guild: discord.Guild, options: list, auto_convert: dict) -> list:
235236
"""
236237
Processes Role, User, and Channel option types to discord.py's models.
237238
@@ -248,7 +249,11 @@ def process_options(self, guild: discord.Guild, options: list, auto_convert: dic
248249
return [x["value"] for x in options]
249250
if not auto_convert:
250251
return [x["value"] for x in options]
251-
converters = [guild.get_member, guild.get_channel, guild.get_role]
252+
converters = [
253+
[guild.get_member, guild.fetch_member],
254+
guild.get_channel,
255+
guild.get_role]
256+
252257
types = {
253258
"user": 0,
254259
"USER": 0,
@@ -273,7 +278,19 @@ def process_options(self, guild: discord.Guild, options: list, auto_convert: dic
273278
to_return.append(selected["value"])
274279
continue
275280
loaded_converter = converters[types[auto_convert[selected["name"]]]]
276-
to_return.append(loaded_converter(int(selected["value"])))
281+
if isinstance(loaded_converter, list):
282+
cache_first = loaded_converter[0](int(selected["value"]))
283+
if cache_first:
284+
to_return.append(cache_first)
285+
continue
286+
loaded_converter = loaded_converter[1]
287+
try:
288+
to_return.append(await loaded_converter(int(selected["value"]))) \
289+
if iscoroutinefunction(loaded_converter) else \
290+
to_return.append(loaded_converter(int(selected["value"])))
291+
except (discord.Forbidden, discord.HTTPException):
292+
self.logger.warning("Failed fetching user! Passing ID instead.")
293+
to_return.append(int(selected["value"]))
277294
return to_return
278295

279296
async def on_socket_response(self, msg):
@@ -296,7 +313,7 @@ async def on_socket_response(self, msg):
296313
return
297314
if selected_cmd["has_subcommands"]:
298315
return await self.handle_subcommand(ctx, to_use)
299-
args = self.process_options(ctx.guild, to_use["data"]["options"], selected_cmd["auto_convert"]) \
316+
args = await self.process_options(ctx.guild, to_use["data"]["options"], selected_cmd["auto_convert"]) \
300317
if "options" in to_use["data"] else []
301318
self.logger.debug(f"Command {to_use['data']['name']} invoked.")
302319
await selected_cmd["func"](ctx, *args)

0 commit comments

Comments
 (0)