aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorKippiii <[email protected]>2020-12-03 18:16:39 -0500
committerGitHub <[email protected]>2020-12-03 18:16:39 -0500
commit96059e5365203db9023b3a04e7394d2e5380bbce (patch)
tree449d1ecb3bec6ad67f309f1b309e6f8154b1fd67 /examples
parentFix Team.icon_url_as format argument default value (diff)
downloaddiscord.py-96059e5365203db9023b3a04e7394d2e5380bbce.tar.xz
discord.py-96059e5365203db9023b3a04e7394d2e5380bbce.zip
Add reaction roles example
Diffstat (limited to 'examples')
-rw-r--r--examples/reaction_roles.py83
1 files changed, 83 insertions, 0 deletions
diff --git a/examples/reaction_roles.py b/examples/reaction_roles.py
new file mode 100644
index 00000000..1a668561
--- /dev/null
+++ b/examples/reaction_roles.py
@@ -0,0 +1,83 @@
+"""Uses a messages to add and remove roles through reactions."""
+
+import discord
+
+class RoleReactClient(discord.Client):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+
+ self.role_message_id = 0 # ID of message that can be reacted to to add role
+ self.emoji_to_role = {
+ partial_emoji_1: 0, # ID of role associated with partial emoji object 'partial_emoji_1'
+ partial_emoji_2: 0 # ID of role associated with partial emoji object 'partial_emoji_2'
+ }
+
+ async def on_raw_reaction_add(self, payload):
+ """Gives a role based on a reaction emoji."""
+ # Make sure that the message the user is reacting to is the one we care about
+ if payload.message_id != self.role_message_id:
+ return
+
+ try:
+ role_id = self.emoji_to_role[payload.emoji]
+ except KeyError:
+ # If the emoji isn't the one we care about then exit as well.
+ return
+
+ guild = self.get_guild(payload.guild_id)
+ if guild is None:
+ # Check if we're still in the guild and it's cached.
+ return
+
+ role = guild.get_role(role_id)
+ if role is None:
+ # Make sure the role still exists and is valid.
+ return
+
+ try:
+ # Finally add the role
+ await payload.member.add_roles(role)
+ except discord.HTTPException:
+ # If we want to do something in case of errors we'd do it here.
+ pass
+
+ async def on_raw_reaction_remove(self, payload):
+ """Removes a role based on a reaction emoji."""
+ # Make sure that the message the user is reacting to is the one we care about
+ if payload.message_id == self.role_message_id:
+ return
+
+ try:
+ role_id = self.emoji_to_role[payload.emoji]
+ except KeyError:
+ # If the emoji isn't the one we care about then exit as well.
+ return
+
+ guild = self.get_guild(payload.guild_id)
+ if guild is None:
+ # Check if we're still in the guild and it's cached.
+ return
+
+ role = guild.get_role(role_id)
+ if role is None:
+ # Make sure the role still exists and is valid.
+ return
+
+ member = guild.get_member(payload.user_id)
+ if member is None:
+ # Makes sure the member still exists and is valid
+ return
+
+ try:
+ # Finally, remove the role
+ await member.remove_roles(role)
+ except discord.HTTPException:
+ # If we want to do something in case of errors we'd do it here.
+ pass
+
+# This bot requires the members and reactions intents.
+intents = discord.Intents.default()
+intents.members = True
+
+client = RoleReactClient(intents=intents)
+client.run("token")