You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reactions are Discord's way of adding emojis to other messages. Early on, before Discord introduced [components](../interactions/buttons.mdx), this system was largely used to make interactive messages and apps.
8
+
Reactions are Discord's way of adding emojis to other messages. Early on, before Discord introduced [components](../interactions/buttons.mdx), this system was largely used to make interactive messages and apps.
9
9
Having bots react to messages is less common now, and is somewhat considered legacy behaviour.
10
10
This guide will teach you the basics of how they work, since they still have their use cases, like reaction role systems and polling.
11
11
12
12
In Disnake, reactions are represented with <DocsLinkreference="disnake.Reaction">Reaction</DocsLink> objects. Whenever you operate on a <DocsLinkreference="disnake.Message">Message</DocsLink> you can access a list of reactions attached to that message.
13
13
In this guide we will be providing an example using the <DocsLinkreference="disnake.on_raw_reaction_add">on_raw_reaction_add / remove</DocsLink> events and a <DocsLinkext="commands"reference="disnake.ext.commands.Bot.message_command">message_command</DocsLink>'s <DocsLinkreference="disnake.MessageCommandInteraction">interaction</DocsLink> to demonstrate.
14
14
15
+
- <DocsLinkreference="disnake.Reactions.users">disnake.Reactions.users</DocsLink> won't be covered here since the docs
16
+
demonstrate its use elegantly.
17
+
15
18
:::info
16
19
**Reaction limitations**
17
20
18
-
- Removing reactions that are not owned by the bot requires <DocsLinkreference="disnake.Intents.reactions">Intents.reactions</DocsLink> to be set
19
-
- Therefore <DocsLinkreference="disnake.Intents.messages">Intents.messages</DocsLink> is indirectly required if you want to manipulate reactions
21
+
- To maintain a consistent reaction cache <DocsLinkreference="disnake.Intents.reactions">Intents.reactions</DocsLink> is recommended to manipulate others reactions, and is required if you intend to utilize events.
20
22
- A message can have a maximum of 20 unique reactions on it at one time.
21
-
- Reactions are inherently linked to emojis, and your bot will not have access to resend all emojis used by Discord users.
23
+
- Reactions are inherently linked to emojis, and your bot will not have access to resend all emojis used by Discord users. ( The bot can always react to others reactions )
22
24
- Dealing with reactions results in a fair amount of extra API calls, meaning it can have rate-limit implications on deployment scale.
23
25
- Using Reactions as a UX interface was never a intended behavior, and is ultimately inferior to the newer component style interface.
26
+
24
27
:::
25
28
26
29
<DiscordMessages>
@@ -49,17 +52,14 @@ In this guide we will be providing an example using the <DocsLink reference="dis
49
52
### Emojis
50
53
51
54
Since reactions utilize Emojis this guide will also include a quick primer on how disnake handles emojis
- <DocsLinkreference="disnake.PartialEmoji">PartialEmoji</DocsLink> Stripped down version of Emoji
56
-
-[`string`](https://docs.python.org/3/library/string.html) String containing one or more emoji unicodepoints (Emoji modifiers complicates things but thats out of scope)
57
-
58
-
**Which one you get is circumstancial:**
59
-
Emoji class: is primarely returned when custom emojis are grabbed from the guild/bot
60
-
PartialEmoji: are most often custom emojis too, but will usually represent custom emojis the bot can't access
61
-
Strings: are normally returned when Unicode CodePoints are used. These are the standard emojis most are familiar with (✅🎮💛💫)
62
-
but these can also come as a PartialEmoji
57
+
- <DocsLinkreference="disnake.Emoji">Emoji</DocsLink> Custom emojis are primarely returned when custom emojis are grabbed
58
+
from the guild/bot
59
+
- <DocsLinkreference="disnake.PartialEmoji">PartialEmoji</DocsLink> Stripped down version of Emoji. Which appears in raw
60
+
events or when the bot cannot access the custom emoji
61
+
-[`string`](https://docs.python.org/3/library/string.html) Strings: are normally returned when unicode emojis are used. These are the standard emojis most are familiar with (✅🎮💛💫)
62
+
but these will also come as a PartialEmoji in raw events
63
63
64
64
There is also a small write up about this [here](../faq/general.mdx#how-can-i-add-a-reaction-to-a-message).
65
65
@@ -71,21 +71,23 @@ Some examples are also available in the [GitHub repository](https://github.com/D
71
71
72
72
### Example using on_reaction events
73
73
74
-
There are a few reaction related events we can listen/subscribe to:
74
+
There are a few reaction related [events](https://docs.disnake.dev/en/stable/api.html#event-reference) we can listen/subscribe to:
75
75
76
76
- <DocsLinkreference="disnake.on_raw_reaction_add">on_raw_reaction_add</DocsLink>, called when a user adds a reaction
77
-
- <DocsLinkreference="disnake.on_raw_reaction_remove">on_raw_reaction_remove</DocsLink>, called when a user's reaction is removed
78
-
- <DocsLinkreference="disnake.on_raw_reaction_clear">on_raw_reaction_clear</DocsLink>, called when a message has all reactions removed
79
-
- <DocsLinkreference="disnake.on_raw_reaction_clear_emoji">on_raw_reaction_clear_emoji</DocsLink>, called when all reactions with a specific emoji are removed from a message
77
+
- <DocsLinkreference="disnake.on_raw_reaction_remove">on_raw_reaction_remove</DocsLink>, called when a user's
78
+
reaction is removed
79
+
- <DocsLinkreference="disnake.on_raw_reaction_clear">on_raw_reaction_clear</DocsLink>, called when a message has all
80
+
reactions removed
81
+
- <DocsLinkreference="disnake.on_raw_reaction_clear_emoji">on_raw_reaction_clear_emoji</DocsLink>, called when all
82
+
reactions with a specific emoji are removed from a message
80
83
81
84
There are non-raw equivalents, but they rely on the cache. If the message is not found in the internal cache, then the event is not called.
82
85
For this reason raw events are preferred, and you are only giving up on an included User/Member object that you can easily fetch if you need it.
83
86
84
-
- More information about events can be found in the docs, [`here`](https://docs.disnake.dev/en/stable/api.html#event-reference)
85
-
86
87
One important thing about raw_reaction events is that all the payloads are only populated with <DocsLinkreference="disnake.PartialEmoji">PartialEmojis</DocsLink>
87
88
This is generally not an issue since it contains everything we need, but its something you should be aware of.
88
-
Raw reaction events come a <DocsLinkreference="disnake.RawReactionActionEvent">RawReactionActionEvent</DocsLink> which is called `payload` in the examples.
89
+
Raw reaction add/remove events come as <DocsLinkreference="disnake.RawReactionActionEvent">RawReactionActionEvent</DocsLink> which is called `payload` in the examples.
90
+
The raw clearing events each have their own event payloads.
89
91
90
92
```python title="on_raw_reaction_add.py"
91
93
@bot.listen()
@@ -151,75 +153,112 @@ Notice how second emoji resolved into **:disnake:** because the emoji is on a se
We can only use custom emojis from servers the bot has joined, but we can use them interchangably on those servers.
158
160
Bots can make <DocsLinkreference="disnake.ui.Button">buttons</DocsLink> using emojis from servers they're not members of, this may or may not be intended behaviour by Discord and should not be relied on.
159
161
:::
160
162
161
-
Here's a few ways you could filter on reactions to do various things
163
+
<br />
162
164
163
-
```python title=react_actions.py
164
-
import disnake
165
+
**Here are a few examples on how reactions can be implemented:**
165
166
166
-
# These lists are arbitrary and is just to provide context. Using static lists like this can be ok in small bots, but should really be supplied by a db.
167
+
<Tabs>
168
+
<TabItemvalue="deny_reactions.py"label="Deny a role using reactions">
# Members with a restricted role, are only allowed to react with💙 -- From the docs we know that str(PartialEmoji) returns either the codepoint or <:emoji:id>
187
+
# Members with a restricted role, are only allowed to react with💙 -- From the docs we know that str(PartialEmoji) returns either the codepoint or <:emoji:id>
194
188
if [role for role in payload.member.roles if role.id in restricted_role_ids] andnotstr(
195
189
payload.emoji
196
190
) in allowed_emojis:
197
191
# Since the list did not return empty and is not a allowed emoji, we remove it
role_to_apply and role_to_apply in payload.member.roles
241
-
): # Check if we actually got a role, then check if the member actually has it, then remove it
242
-
await payload.member.remove_roles(role_to_apply)
278
+
role_to_apply and role_to_apply in payload.member.roles
279
+
): # Check if we actually got a role, then check if the member actually has it, then remove it
280
+
await payload.member.remove_roles(role_to_apply)
243
281
```
244
282
245
-
### Example using message_command
283
+
</TabItem>
246
284
247
-
We could go with a `slash_command` here, but since we will be targeting other messages, it adds a complication because if the message is in a different channel from where the command is executed; the retrieved message will be `None`.
248
-
Using a `message_command` instead side-steps this issue, since the targeted message will be present in the interaction object.
285
+
</Tabs>
249
286
250
-
- <DocsLinkreference="disnake.Reactions.users">disnake.Reactions.users</DocsLink> won't be covered here since the docs
251
-
demonstrate its use elegantly.
287
+
### Example using an ApplicationCommandInteraction
252
288
289
+
Using a `message_command` here because the message object is always included in the message commands interaction instance.
253
290
This example is purely to demonstrate using the Reaction object since events deal with a similar but different class
0 commit comments