|
6 | 6 | from . import http |
7 | 7 | from . import error |
8 | 8 |
|
| 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 | + |
9 | 97 |
|
10 | 98 | class CommandObject: |
11 | 99 | """ |
|
0 commit comments