Skip to content

Commit b538de8

Browse files
committed
refactor: event dispatching
1 parent 9d2676c commit b538de8

File tree

4 files changed

+27
-92
lines changed

4 files changed

+27
-92
lines changed

discord_components/client.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,27 @@ async def on_socket_response(self, res):
4343
].get("channel_id"):
4444
res["d"]["message"]["message_reference"] = res["d"]["channel_id"]
4545

46+
interaction = self._get_interaction(res)
47+
self.bot.dispatch(f"raw_interaction", res["d"])
48+
self.bot.dispatch("interaction", interaction)
49+
50+
if self._components_callback.get(interaction.custom_id):
51+
callback_info = self._components_callback[interaction.custom_id]
52+
if callback_info["uses"] == 0:
53+
del self._components_callback[interaction.custom_id]
54+
return
55+
56+
if callback_info["uses"] is not None:
57+
self._components_callback[interaction.custom_id]["uses"] -= 1
58+
if not callback_info["filter"](interaction):
59+
return
60+
61+
await self._components_callback[interaction.custom_id]["callback"](interaction)
62+
4663
for _type in InteractionEventType:
4764
if _type.value == res["d"]["data"]["component_type"]:
4865
self.bot.dispatch(f"raw_{_type.name}", res["d"])
49-
self.bot.dispatch(f"raw_interaction", res["d"])
50-
51-
interaction = self._get_interaction(res)
5266
self.bot.dispatch(_type.name, interaction)
53-
self.bot.dispatch("interaction", interaction)
54-
if self._components_callback.get(interaction.custom_id):
55-
callback_info = self._components_callback[interaction.custom_id]
56-
if callback_info["uses"] == 0:
57-
del self._components_callback[interaction.custom_id]
58-
return
59-
60-
if callback_info["uses"] is not None:
61-
self._components_callback[interaction.custom_id]["uses"] -= 1
62-
if not callback_info["filter"](interaction):
63-
return
64-
65-
await self._components_callback[interaction.custom_id]["callback"](
66-
interaction
67-
)
6867
break
6968

7069
def _get_interaction(self, json: dict):
@@ -107,9 +106,7 @@ def check(interaction: Interaction):
107106

108107
return await self.bot.wait_for(event, check=check, timeout=timeout)
109108

110-
def add_callback(
111-
self, component: Component, callback, *, uses: int = None, filter=None
112-
):
109+
def add_callback(self, component: Component, callback, *, uses: int = None, filter=None):
113110
self._components_callback[component.custom_id] = {
114111
"callback": callback,
115112
"uses": uses,

discord_components/interaction.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ def __init__(
5858
state=state, guild=self.guild, data=raw_data["member"]
5959
)
6060
elif raw_data.get("member"):
61-
self.user: Union[User, Member] = User(
62-
state=state, data=raw_data["member"]["user"]
63-
)
61+
self.user: Union[User, Member] = User(state=state, data=raw_data["member"]["user"])
6462
else:
6563
self.user: Union[User, Member] = User(state=state, data=raw_data["user"])
6664
self.author: Union[User, Member] = self.user
@@ -113,6 +111,9 @@ async def respond(
113111
ephemeral: bool = True,
114112
components: List[Union[ActionRow, Component, List[Component]]] = None,
115113
) -> Optional[Union[ComponentMessage, dict]]:
114+
if self.responded:
115+
return
116+
116117
state = self.state
117118
data = {"tts": tts}
118119

@@ -130,9 +131,7 @@ async def respond(
130131

131132
if embeds is not None:
132133
if len(embeds) > 10:
133-
raise InvalidArgument(
134-
"embeds parameter must be a list of up to 10 elements"
135-
)
134+
raise InvalidArgument("embeds parameter must be a list of up to 10 elements")
136135
data["embeds"] = [embed.to_dict() for embed in embeds]
137136

138137
if suppress is not None:
@@ -144,15 +143,11 @@ async def respond(
144143

145144
if allowed_mentions is not None:
146145
if state.allowed_mentions is not None:
147-
data["allowed_mentions"] = state.allowed_mentions.merge(
148-
allowed_mentions
149-
).to_dict()
146+
data["allowed_mentions"] = state.allowed_mentions.merge(allowed_mentions).to_dict()
150147
else:
151148
data["allowed_mentions"] = allowed_mentions.to_dict()
152149
else:
153-
data["allowed_mentions"] = (
154-
state.allowed_mentions and state.allowed_mentions.to_dict()
155-
)
150+
data["allowed_mentions"] = state.allowed_mentions and state.allowed_mentions.to_dict()
156151

157152
if components is not None:
158153
data["components"] = _get_components_json(components)
@@ -164,9 +159,7 @@ async def respond(
164159
raise InvalidArgument("cannot pass both file and files parameter to send()")
165160
elif files is not None:
166161
if len(files) > 10:
167-
raise InvalidArgument(
168-
"files parameter must be a list of up to 10 elements"
169-
)
162+
raise InvalidArgument("files parameter must be a list of up to 10 elements")
170163
if file is not None:
171164
files = [file]
172165

@@ -188,6 +181,7 @@ async def respond(
188181
else:
189182
self.deferred = True
190183
except NotFound as e:
184+
self.responded = True
191185
raise NotFound(
192186
e.response,
193187
"Interaction is unknown (you have already responded to the interaction or responding took too long)",

examples/basic.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,4 @@ async def select(ctx):
4646
await interaction.send(content=f"{interaction.values[0]} selected!")
4747

4848

49-
@bot.command()
50-
async def interaction(ctx):
51-
await ctx.send(
52-
"Buttons and Selects!",
53-
components=[
54-
Select(
55-
placeholder="Select something!",
56-
options=[
57-
SelectOption(label="a", value="a"),
58-
SelectOption(label="b", value="b"),
59-
],
60-
custom_id="select1",
61-
),
62-
Button(label="Button", custom_id="button1"),
63-
],
64-
)
65-
66-
interaction = await bot.wait_for(
67-
"interaction", check=lambda inter: inter.custom_id in ["select1", "button1"]
68-
)
69-
70-
if isinstance(interaction.component, Button):
71-
await interaction.respond(content="Button clicked!")
72-
else:
73-
await interaction.respond(content=f"{interaction.values[0]} selected!")
74-
75-
7649
bot.run("your token")

examples/event.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -47,33 +47,4 @@ async def on_select_option(interaction):
4747
await interaction.respond(content=f"{interaction.values[0]} selected!")
4848

4949

50-
@bot.command()
51-
async def interaction(ctx):
52-
await ctx.send(
53-
"Buttons and Selects!",
54-
components=[
55-
Select(
56-
placeholder="Select something!",
57-
options=[
58-
SelectOption(label="a", value="a"),
59-
SelectOption(label="b", value="b"),
60-
],
61-
custom_id="select1",
62-
),
63-
Button(label="Button", custom_id="button1"),
64-
],
65-
)
66-
67-
68-
@bot.event
69-
async def on_interaction(interaction):
70-
if interaction.custom_id not in ["select1", "button1"]:
71-
return
72-
73-
if isinstance(interaction.component, Button):
74-
await interaction.respond(content="Button clicked!")
75-
else:
76-
await interaction.respond(content=f"{interaction.values[0]} selected!")
77-
78-
7950
bot.run("your token")

0 commit comments

Comments
 (0)