Skip to content

Commit

Permalink
Move OptionChoice and add MessageCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Middledot authored Aug 30, 2021
1 parent a2a285e commit 064b07f
Showing 1 changed file with 46 additions and 10 deletions.
56 changes: 46 additions & 10 deletions discord/app/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ def to_dict(self) -> Dict:
"choices": [c.to_dict() for c in self.choices],
}

class OptionChoice:
def __init__(self, name: str, value: Optional[str] = None):
self.name = name
self.value = value or name

def to_dict(self) -> Dict[str, str]:
return {"name": self.name, "value": self.value}

class SubCommandGroup(Option):
type = 1
Expand Down Expand Up @@ -182,16 +189,6 @@ async def invoke(self, interaction: Interaction) -> None:
interaction.data = option
await command.invoke(interaction)


class OptionChoice:
def __init__(self, name: str, value: Optional[str] = None):
self.name = name
self.value = value or name

def to_dict(self) -> Dict[str, str]:
return {"name": self.name, "value": self.value}


class UserCommand:
type = 2

Expand Down Expand Up @@ -244,3 +241,42 @@ async def invoke(self, interaction: Interaction) -> None:

class MessageCommand:
type = 3

def __new__(cls, *args, **kwargs):
self = super().__new__(cls)

self.__original_kwargs__ = kwargs.copy()
return self

def __init__(self, func, *args, **kwargs):
if not asyncio.iscoroutinefunction(func):
raise TypeError("Callback must be a coroutine.")
self.callback = func

self.guild_ids = kwargs.get("guild_ids", None)

self.description = ""
self.name = kwargs.pop("name", func.__name__)
if not isinstance(self.name, str):
raise TypeError("Name of a command must be a string.")

def to_dict(self):
return {
"name":self.name,
"description":self.description,
"type":self.type
}

async def invoke(self, interaction):
_data = interaction.data["resolved"]["messages"]
for i, v in _data.items():
v["id"] = int(i)
message = v
channel = interaction._state.get_channel(int(message["channel_id"]))
if channel == None:
data = await interaction._state.http.start_private_message(int(message["author"]["id"]))
channel = interaction._state.add_dm_channel(data)

target = Message(state=interaction._state, channel=channel, data=message)
ctx = InteractionContext(interaction)
await self.callback(ctx, target)

0 comments on commit 064b07f

Please sign in to comment.