Skip to content

Commit d944659

Browse files
committed
Merge remote-tracking branch 'origin/dev' into dev
2 parents 8df8216 + dff3399 commit d944659

File tree

1 file changed

+34
-41
lines changed

1 file changed

+34
-41
lines changed

guide/docs/popular-topics/reactions.mdx

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ In this guide we will be providing an example using the <DocsLink reference="dis
1818
:::info
1919
**Reaction limitations**
2020

21-
- To maintain a consistent reaction cache <DocsLink reference="disnake.Intents.reactions">Intents.reactions</DocsLink> is recommended to manipulate others reactions, and is required if you intend to utilize events.
21+
- To maintain a consistent reaction cache, <DocsLink reference="disnake.Intents.reactions">Intents.reactions</DocsLink> is recommended to manipulate others' reactions, and is required if you intend to utilize events.
2222
- A message can have a maximum of 20 unique reactions on it at one time.
2323
- 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 )
2424
- Dealing with reactions results in a fair amount of extra API calls, meaning it can have rate-limit implications on deployment scale.
25-
- Using Reactions as a UX interface was never a intended behavior, and is ultimately inferior to the newer component style interface.
25+
- Using Reactions for user interfaces was never intended behavior, and is ultimately inferior to the newer component interface.
2626

2727
:::
2828

@@ -168,27 +168,28 @@ Bots can make <DocsLink reference="disnake.ui.Button">buttons</DocsLink> using e
168168
<TabItem value="deny_reactions.py" label="Deny a role using reactions">
169169

170170
```python
171+
# Members with a restricted role are only allowed to react with 💙
172+
171173
allowed_emojis = ["💙"]
172174
restricted_role_ids = [951263965235773480, 1060778008039919616]
173175

174176

175177
@bot.listen()
176178
async def on_raw_reaction_add(payload: disnake.RawReactionActionEvent):
177-
178179
if payload.user_id == bot.user.id:
179180
return
180181
if not payload.guild_id:
181182
return # guild_id is None if its a DM
182183

183-
# Getting the channel, and fetching message as these will be useful
184-
event_channel = bot.get_channel(payload.channel_id)
185-
event_message = await event_channel.fetch_message(payload.message_id)
184+
# From the docs we know that str(PartialEmoji) returns either the codepoint or <:emoji:id>
185+
if (
186+
any(payload.member.get_role(role) for role in restricted_role_ids)
187+
and str(payload.emoji) not in allowed_emojis
188+
):
189+
# Getting the channel, and fetching message as these will be useful
190+
event_channel = bot.get_channel(payload.channel_id)
191+
event_message = await event_channel.fetch_message(payload.message_id)
186192

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>
188-
if [role for role in payload.member.roles if role.id in restricted_role_ids] and not str(
189-
payload.emoji
190-
) in allowed_emojis:
191-
# Since the list did not return empty and is not a allowed emoji, we remove it
192193
await event_message.remove_reaction(emoji=payload.emoji, member=payload.member)
193194
```
194195

@@ -197,33 +198,30 @@ async def on_raw_reaction_add(payload: disnake.RawReactionActionEvent):
197198
<TabItem value="main2.py" label="Simple reaction button">
198199

199200
```python
200-
# Since you can to un-react for the user we can emulate a button
201-
# This can be usefull if you want the functionality of buttons, but want a more compact look.
201+
# Since you can remove a user's reaction (given appropriate permissions), we can emulate a button.
202+
# This can be useful if you want the functionality of buttons, but want a more compact look.
202203

203204
button_emojis = [""] # What emojis to react to
204205
reaction_messages = [1060797825417478154] # What messages to monitor
205206

206207

207208
@bot.listen()
208209
async def on_raw_reaction_add(payload: disnake.RawReactionActionEvent):
209-
210210
if payload.user_id == bot.user.id:
211211
return
212-
if not payload.guild_id:
213-
return
214-
if payload.channel_id not in reaction_messages or str(payload.emoji) not in button_emojis:
212+
if payload.message_id not in reaction_messages or str(payload.emoji) not in button_emojis:
215213
return
216214

217215
# Getting the channel, and fetching message as these will be useful
218216
event_channel = bot.get_channel(payload.channel_id)
219217
event_message = await event_channel.fetch_message(payload.message_id)
220218

221-
await event_message.remove_reaction(
222-
emoji=payload.emoji, member=payload.member
223-
) # Remove the reaction
219+
# Remove the reaction
220+
await event_message.remove_reaction(emoji=payload.emoji, member=payload.member)
224221
awesome_function() # Do some stuff
225-
await event_channel.send("Done!", delete_after=10.0)
222+
226223
# Short message to let the user know it went ok. This is not an interaction so a message response is not strictly needed
224+
await event_channel.send("Done!", delete_after=10.0)
227225
```
228226

229227
</TabItem>
@@ -242,23 +240,20 @@ reaction_roles = {
242240

243241
@bot.listen()
244242
async def on_raw_reaction_add(payload: disnake.RawReactionActionEvent):
245-
246243
# We usually don't want the bot to react to its own actions, nor DM's in this case
247244
if payload.user_id == bot.user.id:
248245
return
249246
if not payload.guild_id:
250247
return # guild_id is None if its a DM
251-
if (
252-
str(payload.emoji) not in reaction_roles.keys()
253-
or payload.message_id not in reaction_messages
254-
):
248+
249+
role_id = reaction_roles.get(str(payload.emoji))
250+
if payload.message_id not in reaction_messages or not role_id:
255251
return
256252

257-
role_to_apply = bot.get_guild(payload.guild_id).get_role(reaction_roles[str(payload.emoji)])
258-
if (
259-
role_to_apply and not role_to_apply in payload.member.roles
260-
): # Check if we actually got a role, then check if the member already has it, if not add it
261-
await payload.member.add_roles(role_to_apply)
253+
role = bot.get_guild(payload.guild_id).get_role(role_id)
254+
# Check if we actually got a role, then check if the member already has it, if not add it
255+
if role and role not in payload.member.roles:
256+
await payload.member.add_roles(role)
262257

263258

264259
@bot.listen()
@@ -267,17 +262,15 @@ async def on_raw_reaction_remove(payload: disnake.RawReactionActionEvent):
267262
return
268263
if not payload.guild_id:
269264
return # guild_id is None if its a DM
270-
if (
271-
str(payload.emoji) not in reaction_roles.keys()
272-
or payload.message_id not in reaction_messages
273-
):
265+
266+
role_id = reaction_roles.get(str(payload.emoji))
267+
if payload.message_id not in reaction_messages or not role_id:
274268
return
275269

276-
role_to_remove = bot.get_guild(payload.guild_id).get_role(reaction_roles[str(payload.emoji)])
277-
if (
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)
270+
role = bot.get_guild(payload.guild_id).get_role(role_id)
271+
# Check if we actually got a role, then check if the member actually has it, then remove it
272+
if role and role in payload.member.roles:
273+
await payload.member.remove_roles(role)
281274
```
282275

283276
</TabItem>
@@ -310,7 +303,7 @@ async def list_reactions(self, inter: disnake.MessageCommandInteraction):
310303

311304
for reaction in reaction_list:
312305

313-
# Since the reactions are present on the message, the bot can react to it, even tho it does not have access to the custom emoji
306+
# Since the reactions are present on the message, the bot can react to it, even though it does not have access to the custom emoji
314307
await inter.target.add_reaction(reaction)
315308

316309
# However we still cannot add new reactions we don't have access to.

0 commit comments

Comments
 (0)