Skip to content

Instantly share code, notes, and snippets.

@noaione
Last active March 26, 2022 05:24
Show Gist options
  • Select an option

  • Save noaione/cb5cab1a77e1f1ea34ff71e836de535b to your computer and use it in GitHub Desktop.

Select an option

Save noaione/cb5cab1a77e1f1ea34ff71e836de535b to your computer and use it in GitHub Desktop.
from typing import TYPE_CHECKING, Optional
import disnake.state
from disnake import ApplicationCommandInteraction, ComponentType, MessageInteraction, ModalInteraction
if TYPE_CHECKING:
from disnake.types.interactions import (
ApplicationCommandInteraction as ApplicationCommandInteractionPayload,
)
class CustomAppContext(ApplicationCommandInteraction):
client: naoTimesBot
@property
def cog(self) -> CogT:
return self.application_command.cog
@property
def bot(self) -> naoTimesBot:
return self.client
def get_cog(self, name: str) -> Optional[CogT]:
"""Shortcut to get a cog from the bot."""
return self.client.get_cog(name)
async def defer(self, *, ephemeral: bool = False, with_message: bool = False) -> None:
"""|coro|
A shortcut to Interaction.response.defer
"""
await self.response.defer(ephemeral=ephemeral, with_message=with_message)
def monkeypatch_interaction_create():
def _parse_interaction_create(
self: disnake.state.ConnectionState, data: ApplicationCommandInteractionPayload
) -> None:
# A modified version of interaction create to use
# naoTimesAppContext to dispatch the slash_command!
interaction_type = data["type"]
if interaction_type == 1:
# PING interaction should never be received
return
elif interaction_type == 2:
interaction = CustomAppContext(data=data, state=self)
self.dispatch("application_command", interaction)
elif interaction_type == 3:
interaction = MessageInteraction(data=data, state=self)
self._view_store.dispatch(interaction)
self.dispatch("message_interaction", interaction)
if interaction.data.component_type is ComponentType.button:
self.dispatch("button_click", interaction)
elif interaction.data.component_type is ComponentType.select:
self.dispatch("dropdown", interaction)
elif interaction_type == 4:
interaction = CustomAppContext(data=data, state=self)
self.dispatch("application_command_autocomplete", interaction)
elif interaction_type == 5:
interaction = ModalInteraction(data=data, state=self)
self._modal_store.dispatch(interaction)
self.dispatch("modal_submit", interaction)
else:
return
self.dispatch("interaction", interaction)
disnake.state.ConnectionState.parse_interaction_create = _parse_interaction_create
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment