Skip to content

Commit 73063ee

Browse files
committed
Implemented process_options
1 parent 03ed29d commit 73063ee

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

discord_slash/client.py

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,30 +55,60 @@ async def _slash(ctx): # Normal usage.
5555
async def _pick(ctx, choice1, choice2): # Command with 1 or more args.
5656
await ctx.send(text=str(random.choice([choice1, choice2])))
5757
58+
Example of formatting ``auto_convert``:
59+
60+
.. code-block:: python
61+
62+
{"option_role": "role", # For key put name of the option and for value put type of the option.
63+
"option_user": 6, # Also can use number for type
64+
"option_channel": "CHANNEL"} # and all upper case.
65+
5866
:param name: Name of the slash command.
59-
:param auto_convert: Not implemented.
67+
:param auto_convert: Dictionary of how to convert option values.
6068
:type auto_convert: dict
6169
"""
6270
def wrapper(cmd):
63-
self.commands[cmd.__name__ if not name else name] = cmd
71+
self.commands[cmd.__name__ if not name else name] = [cmd, auto_convert]
6472
self.logger.debug(f"Added command `{cmd.__name__ if not name else name}`")
6573
return cmd
6674
return wrapper
6775

68-
def process_options(self, options: dict) -> list:
76+
def process_options(self, guild: discord.Guild, options: list, auto_convert: dict) -> list:
6977
"""
70-
Not ready.
78+
Processes Role, User, and Channel option types to discord.py's models.
7179
80+
:param guild: Guild of the command message.
81+
:type guild: discord.Guild
7282
:param options: Dict of options.
73-
:type options: dict
83+
:type options: list
84+
:param auto_convert: Dictionary of how to convert option values.
85+
:type auto_convert: dict
7486
:return: list
75-
:raises: :class:`NotImplementedError` - This is still not implemented.
7687
"""
77-
raise NotImplementedError
88+
converters = [guild.get_member, guild.get_role, guild.get_role]
89+
types = {
90+
"user": 0,
91+
"USER": 0,
92+
6: 0,
93+
"6": 0,
94+
"channel": 1,
95+
"CHANNEL": 1,
96+
7: 1,
97+
"7": 1,
98+
"role": 2,
99+
"ROLE": 2,
100+
8: 2,
101+
"8": 2
102+
}
103+
104+
to_return = []
78105

79106
for x in options:
80-
pass
81-
return []
107+
selected = x
108+
if selected["name"] in auto_convert.keys():
109+
loaded_converter = converters[types[auto_convert[selected["name"]]]]
110+
to_return.append(loaded_converter(int(selected["value"])))
111+
return to_return
82112

83113
async def on_socket_response(self, msg):
84114
"""
@@ -93,6 +123,7 @@ async def on_socket_response(self, msg):
93123
return
94124
to_use = msg["d"]
95125
if to_use["data"]["name"] in self.commands.keys():
96-
args = [x["value"] for x in to_use["data"]["options"]] if "options" in to_use["data"] else []
126+
selected_cmd = self.commands[to_use["data"]["name"]]
97127
ctx = model.SlashContext(self.req, to_use, self._discord)
98-
await self.commands[to_use["data"]["name"]](ctx, *args)
128+
args = self.process_options(ctx.guild, to_use["data"]["options"], selected_cmd[1]) if "options" in to_use["data"] else []
129+
await selected_cmd[0](ctx, *args)

0 commit comments

Comments
 (0)