Skip to content

Commit 249d84f

Browse files
Add classes for command data
These classes are to easily allow equality comparison of commands
1 parent 9bc77cd commit 249d84f

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

discord_slash/error.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,8 @@ class IncorrectType(SlashCommandError):
5454
"""
5555
Type passed was incorrect
5656
"""
57+
58+
class IncorrectCommandData(SlashCommandError):
59+
"""
60+
Incorrect data was passed to a slash command data object
61+
"""

discord_slash/model.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,94 @@
66
from . import http
77
from . import error
88

9+
class ChoiceData:
10+
"""
11+
Command choice data object
12+
13+
:ivar name: Name of the choice, this is what the user will see
14+
:ivar value: Values of the choice, this is what discord will return to you
15+
"""
16+
def __init__(self, name, value):
17+
self.name = name
18+
self.value = value
19+
20+
def __eq__(self, other):
21+
return isinstance(other, ChoiceData) and self.__dict__ == other.__dict__
22+
23+
24+
class OptionData:
25+
"""
26+
Command option data object
27+
28+
:ivar name: Name of the option.
29+
:ivar description: Description of the option.
30+
:ivar required: If the option is required.
31+
:ivar choices: A list of :class:`ChoiceData`, cannot be present on subcommand groups
32+
:ivar options: List of :class:`OptionData`, this will be present if it's a subcommand group
33+
"""
34+
def __init__(
35+
self, name, description, required=False, choices=None, options=None, **kwargs
36+
):
37+
self.name = name
38+
self.description = description
39+
self.type = kwargs.get("type")
40+
if self.type is None:
41+
raise error.IncorrectCommandData("type is required for options")
42+
self.required = required
43+
if choices is not None:
44+
self.choices = []
45+
for choice in choices:
46+
self.choices.append(ChoiceData(**choice))
47+
else:
48+
self.choices = None
49+
50+
if self.type in (1, 2):
51+
self.options = []
52+
if options is not None:
53+
for option in options:
54+
self.options.append(OptionData(**option))
55+
elif self.type == 2:
56+
raise error.IncorrectCommandData(
57+
"Options are required for subcommands / subcommand groups"
58+
)
59+
60+
def __eq__(self, other):
61+
return isinstance(other, OptionData) and self.__dict__ == other.__dict__
62+
63+
64+
class CommandData:
65+
"""
66+
Slash command data object
67+
68+
:ivar name: Name of the command.
69+
:ivar description: Description of the command.
70+
:ivar options: List of :class:`OptionData`.
71+
:ivar id: Command id, this is received from discord so may not be present
72+
"""
73+
74+
def __init__(
75+
self, name, description, options, id=None, **kwargs
76+
):
77+
self.name = name
78+
self.description = description
79+
self.id = id
80+
if options is not None:
81+
self.options = []
82+
for option in options:
83+
self.options.append(OptionData(**option))
84+
else:
85+
self.options = None
86+
87+
def __eq__(self, other):
88+
if isinstance(other, CommandData):
89+
return (
90+
self.name == other.name
91+
and self.description == other.description
92+
and self.options == other.options
93+
)
94+
else:
95+
return False
96+
997

1098
class CommandObject:
1199
"""

0 commit comments

Comments
 (0)