Skip to content

Commit f13b387

Browse files
committed
Added connector
1 parent 794f0db commit f13b387

File tree

3 files changed

+55
-15
lines changed

3 files changed

+55
-15
lines changed

discord_slash/client.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ def add_slash_command(self,
341341
description: str = None,
342342
guild_ids: typing.List[int] = None,
343343
options: list = None,
344+
connector: dict = None,
344345
has_subcommands: bool = False):
345346
"""
346347
Registers slash command to SlashCommand.
@@ -355,6 +356,8 @@ def add_slash_command(self,
355356
:type guild_ids: List[int]
356357
:param options: Options of the slash command. This will affect ``auto_convert`` and command data at Discord API. Default ``None``.
357358
:type options: list
359+
:param connector: Kwargs connector for the command. Default ``None``.
360+
:type connector: dict
358361
:param has_subcommands: Whether it has subcommand. Default ``False``.
359362
:type has_subcommands: bool
360363
"""
@@ -379,6 +382,7 @@ def add_slash_command(self,
379382
"description": description,
380383
"guild_ids": guild_ids,
381384
"api_options": options,
385+
"connector": connector or {},
382386
"has_subcommands": has_subcommands
383387
}
384388
self.commands[name] = model.CommandObject(name, _cmd)
@@ -393,7 +397,8 @@ def add_subcommand(self,
393397
base_description: str = None,
394398
subcommand_group_description: str = None,
395399
guild_ids: typing.List[int] = None,
396-
options: list = None):
400+
options: list = None,
401+
connector: dict = None):
397402
"""
398403
Registers subcommand to SlashCommand.
399404
@@ -415,6 +420,8 @@ def add_subcommand(self,
415420
:type guild_ids: List[int]
416421
:param options: Options of the subcommand. This will affect ``auto_convert`` and command data at Discord API. Default ``None``.
417422
:type options: list
423+
:param connector: Kwargs connector for the command. Default ``None``.
424+
:type connector: dict
418425
"""
419426
base = base.lower()
420427
subcommand_group = subcommand_group.lower() if subcommand_group else subcommand_group
@@ -436,6 +443,7 @@ def add_subcommand(self,
436443
"description": base_description,
437444
"guild_ids": guild_ids,
438445
"api_options": [],
446+
"connector": {},
439447
"has_subcommands": True
440448
}
441449
_sub = {
@@ -445,7 +453,8 @@ def add_subcommand(self,
445453
"base_desc": base_description,
446454
"sub_group_desc": subcommand_group_description,
447455
"guild_ids": guild_ids,
448-
"api_options": options
456+
"api_options": options,
457+
"connector": connector or {}
449458
}
450459
if base not in self.commands:
451460
self.commands[base] = model.CommandObject(base, _cmd)
@@ -474,7 +483,8 @@ def slash(self,
474483
description: str = None,
475484
guild_id: int = None,
476485
guild_ids: typing.List[int] = None,
477-
options: typing.List[dict] = None):
486+
options: typing.List[dict] = None,
487+
connector: dict = None):
478488
"""
479489
Decorator that registers coroutine as a slash command.\n
480490
All decorator args must be passed as keyword-only args.\n
@@ -502,6 +512,20 @@ async def _slash(ctx): # Normal usage.
502512
async def _pick(ctx, choice1, choice2): # Command with 1 or more args.
503513
await ctx.send(content=str(random.choice([choice1, choice2])))
504514
515+
To format the connector, follow this example.
516+
517+
.. code-block:: python
518+
519+
{
520+
"example-arg": "example_arg",
521+
"시간": "hour"
522+
# Formatting connector is required for
523+
# using other than english for option parameter name
524+
# for in case.
525+
}
526+
527+
Set discord UI's parameter name as key, and set command coroutine's arg name as value.
528+
505529
:param name: Name of the slash command. Default name of the coroutine.
506530
:type name: str
507531
:param description: Description of the slash command. Default ``None``.
@@ -512,13 +536,15 @@ async def _pick(ctx, choice1, choice2): # Command with 1 or more args.
512536
:type guild_ids: List[int]
513537
:param options: Options of the slash command. This will affect ``auto_convert`` and command data at Discord API. Default ``None``.
514538
:type options: List[dict]
539+
:param connector: Kwargs connector for the command. Default ``None``.
540+
:type connector: dict
515541
"""
516542
if guild_id:
517543
self.logger.warning("`guild_id` is deprecated! `Use guild_ids` instead.")
518544
guild_ids = [guild_id]
519545

520546
def wrapper(cmd):
521-
self.add_slash_command(cmd, name, description, guild_ids, options)
547+
self.add_slash_command(cmd, name, description, guild_ids, options, connector)
522548
return cmd
523549

524550
return wrapper
@@ -534,7 +560,8 @@ def subcommand(self,
534560
subcommand_group_description: str = None,
535561
sub_group_desc: str = None,
536562
guild_ids: typing.List[int] = None,
537-
options: typing.List[dict] = None):
563+
options: typing.List[dict] = None,
564+
connector: dict = None):
538565
"""
539566
Decorator that registers subcommand.\n
540567
Unlike discord.py, you don't need base command.\n
@@ -582,29 +609,32 @@ async def _group_kick_user(ctx, user):
582609
:type guild_ids: List[int]
583610
:param options: Options of the subcommand. This will affect ``auto_convert`` and command data at Discord API. Default ``None``.
584611
:type options: List[dict]
612+
:param connector: Kwargs connector for the command. Default ``None``.
613+
:type connector: dict
585614
"""
586615
base_description = base_description or base_desc
587616
subcommand_group_description = subcommand_group_description or sub_group_desc
588617

589618
def wrapper(cmd):
590-
self.add_subcommand(cmd, base, subcommand_group, name, description, base_description, subcommand_group_description, guild_ids, options)
619+
self.add_subcommand(cmd, base, subcommand_group, name, description, base_description, subcommand_group_description, guild_ids, options, connector)
591620
return cmd
592621

593622
return wrapper
594623

595-
async def process_options(self, guild: discord.Guild, options: list) -> dict:
624+
async def process_options(self, guild: discord.Guild, options: list, connector: dict) -> dict:
596625
"""
597626
Processes Role, User, and Channel option types to discord.py's models.
598627
599628
:param guild: Guild of the command message.
600629
:type guild: discord.Guild
601630
:param options: Dict of options.
602631
:type options: list
632+
:param connector: Kwarg connector.
603633
:return: Union[list, dict]
604634
"""
605635

606636
if not guild or not isinstance(guild, discord.Guild):
607-
return {x["name"]: x["value"] for x in options}
637+
return {connector.get(x["name"]) or x["name"]: x["value"] for x in options}
608638

609639
converters = [
610640
# If extra converters are added and some needs to fetch it,
@@ -655,7 +685,7 @@ async def process_options(self, guild: discord.Guild, options: list) -> dict:
655685
except (discord.Forbidden, discord.HTTPException, discord.NotFound): # Just in case.
656686
self.logger.warning("Failed fetching discord object! Passing ID instead.")
657687
processed = int(x["value"])
658-
to_return[x["name"]] = processed
688+
to_return[connector.get(x["name"]) or x["name"]] = processed
659689
return to_return
660690

661691
async def invoke_command(self, func, ctx, args):
@@ -719,7 +749,7 @@ async def on_socket_response(self, msg):
719749
if "value" not in x:
720750
return await self.handle_subcommand(ctx, to_use)
721751

722-
args = await self.process_options(ctx.guild, to_use["data"]["options"]) \
752+
args = await self.process_options(ctx.guild, to_use["data"]["options"], selected_cmd.connector) \
723753
if "options" in to_use["data"] else []
724754

725755
self._discord.dispatch("slash_command", ctx)
@@ -752,13 +782,13 @@ async def handle_subcommand(self, ctx: context.SlashContext, data: dict):
752782
return
753783
ctx.subcommand_group = sub_group
754784
selected = base[sub_name][sub_group]
755-
args = await self.process_options(ctx.guild, x["options"]) \
785+
args = await self.process_options(ctx.guild, x["options"], selected.connector) \
756786
if "options" in x else []
757787
self._discord.dispatch("slash_command", ctx)
758788
await self.invoke_command(selected, ctx, args)
759789
return
760790
selected = base[sub_name]
761-
args = await self.process_options(ctx.guild, sub_opts) \
791+
args = await self.process_options(ctx.guild, sub_opts, selected.connector) \
762792
if "options" in sub else []
763793
self._discord.dispatch("slash_command", ctx)
764794
await self.invoke_command(selected, ctx, args)

discord_slash/cog_ext.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ def cog_slash(*,
88
name: str = None,
99
description: str = None,
1010
guild_ids: typing.List[int] = None,
11-
options: typing.List[dict] = None):
11+
options: typing.List[dict] = None,
12+
connector: dict = None):
1213
"""
1314
Decorator for Cog to add slash command.\n
1415
Almost same as :func:`.client.SlashCommand.slash`.
@@ -33,6 +34,8 @@ async def ping(self, ctx: SlashContext):
3334
:type guild_ids: List[int]
3435
:param options: Options of the slash command. This will affect ``auto_convert`` and command data at Discord API. Default ``None``.
3536
:type options: List[dict]
37+
:param connector: Kwargs connector for the command. Default ``None``.
38+
:type connector: dict
3639
"""
3740
def wrapper(cmd):
3841
desc = description or inspect.getdoc(cmd)
@@ -46,6 +49,7 @@ def wrapper(cmd):
4649
"description": desc,
4750
"guild_ids": guild_ids,
4851
"api_options": opts,
52+
"connector": connector,
4953
"has_subcommands": False
5054
}
5155
return CogCommandObject(name or cmd.__name__, _cmd)
@@ -62,7 +66,8 @@ def cog_subcommand(*,
6266
subcommand_group_description: str = None,
6367
sub_group_desc: str = None,
6468
guild_ids: typing.List[int] = None,
65-
options: typing.List[dict] = None):
69+
options: typing.List[dict] = None,
70+
connector: dict = None):
6671
"""
6772
Decorator for Cog to add subcommand.\n
6873
Almost same as :func:`.client.SlashCommand.subcommand`.
@@ -97,6 +102,8 @@ async def group_say(self, ctx: SlashContext, text: str):
97102
:type guild_ids: List[int]
98103
:param options: Options of the subcommand. This will affect ``auto_convert`` and command data at Discord API. Default ``None``.
99104
:type options: List[dict]
105+
:param connector: Kwargs connector for the command. Default ``None``.
106+
:type connector: dict
100107
"""
101108
base_description = base_description or base_desc
102109
subcommand_group_description = subcommand_group_description or sub_group_desc
@@ -115,7 +122,8 @@ def wrapper(cmd):
115122
"base_desc": base_description,
116123
"sub_group_desc": subcommand_group_description,
117124
"guild_ids": guild_ids,
118-
"api_options": opts
125+
"api_options": opts,
126+
"connector": connector
119127
}
120128
return CogSubcommandObject(_sub, base, name or cmd.__name__, subcommand_group)
121129
return wrapper

discord_slash/model.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class CommandObject:
1919
:ivar description: Description of the command.
2020
:ivar allowed_guild_ids: List of the allowed guild id.
2121
:ivar options: List of the option of the command. Used for `auto_register`.
22+
:ivar connector: Kwargs connector of the command.
2223
:ivar __commands_checks__: Check of the command.
2324
"""
2425
def __init__(self, name, cmd): # Let's reuse old command formatting.
@@ -27,6 +28,7 @@ def __init__(self, name, cmd): # Let's reuse old command formatting.
2728
self.description = cmd["description"]
2829
self.allowed_guild_ids = cmd["guild_ids"] or []
2930
self.options = cmd["api_options"] or []
31+
self.connector = cmd["connector"] or {}
3032
self.has_subcommands = cmd["has_subcommands"]
3133
# Ref https://github.com/Rapptz/discord.py/blob/master/discord/ext/commands/core.py#L1447
3234
# Since this isn't inherited from `discord.ext.commands.Command`, discord.py's check decorator will

0 commit comments

Comments
 (0)