@@ -168,27 +168,28 @@ Bots can make <DocsLink reference="disnake.ui.Button">buttons</DocsLink> using e
168
168
<TabItem value = " deny_reactions.py" label = " Deny a role using reactions" >
169
169
170
170
``` python
171
+ # Members with a restricted role are only allowed to react with 💙
172
+
171
173
allowed_emojis = [" 💙" ]
172
174
restricted_role_ids = [951263965235773480 , 1060778008039919616 ]
173
175
174
176
175
177
@bot.listen ()
176
178
async def on_raw_reaction_add (payload : disnake.RawReactionActionEvent):
177
-
178
179
if payload.user_id == bot.user.id:
179
180
return
180
181
if not payload.guild_id:
181
182
return # guild_id is None if its a DM
182
183
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)
186
192
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
192
193
await event_message.remove_reaction(emoji = payload.emoji, member = payload.member)
193
194
```
194
195
@@ -197,33 +198,30 @@ async def on_raw_reaction_add(payload: disnake.RawReactionActionEvent):
197
198
<TabItem value = " main2.py" label = " Simple reaction button" >
198
199
199
200
``` 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.
202
203
203
204
button_emojis = [" ✅" ] # What emojis to react to
204
205
reaction_messages = [1060797825417478154 ] # What messages to monitor
205
206
206
207
207
208
@bot.listen ()
208
209
async def on_raw_reaction_add (payload : disnake.RawReactionActionEvent):
209
-
210
210
if payload.user_id == bot.user.id:
211
211
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:
215
213
return
216
214
217
215
# Getting the channel, and fetching message as these will be useful
218
216
event_channel = bot.get_channel(payload.channel_id)
219
217
event_message = await event_channel.fetch_message(payload.message_id)
220
218
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)
224
221
awesome_function() # Do some stuff
225
- await event_channel.send( " Done! " , delete_after = 10.0 )
222
+
226
223
# 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 )
227
225
```
228
226
229
227
</TabItem >
@@ -242,23 +240,20 @@ reaction_roles = {
242
240
243
241
@bot.listen ()
244
242
async def on_raw_reaction_add (payload : disnake.RawReactionActionEvent):
245
-
246
243
# We usually don't want the bot to react to its own actions, nor DM's in this case
247
244
if payload.user_id == bot.user.id:
248
245
return
249
246
if not payload.guild_id:
250
247
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:
255
251
return
256
252
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)
262
257
263
258
264
259
@bot.listen ()
@@ -267,17 +262,15 @@ async def on_raw_reaction_remove(payload: disnake.RawReactionActionEvent):
267
262
return
268
263
if not payload.guild_id:
269
264
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:
274
268
return
275
269
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)
281
274
```
282
275
283
276
</TabItem >
0 commit comments