|
1 |
| -# This example requires the 'members' privileged intent for access to .get_member. |
2 |
| - |
3 | 1 | import discord
|
4 |
| - |
5 |
| - |
6 |
| -class MyClient(discord.Client): |
7 |
| - def __init__(self, *args, **kwargs): |
8 |
| - super().__init__(*args, **kwargs) |
9 |
| - |
10 |
| - self.role_message_id = 0 # ID of the message that can be reacted to for adding/removing a role. |
11 |
| - self.emoji_to_role = { |
12 |
| - discord.PartialEmoji(name="🔴"): 0, # ID of the role associated with unicode emoji '🔴'. |
13 |
| - discord.PartialEmoji(name="🟡"): 0, # ID of the role associated with unicode emoji '🟡'. |
14 |
| - discord.PartialEmoji(name="green", id=0): 0, # ID of the role associated with a partial emoji's ID. |
15 |
| - } |
16 |
| - |
17 |
| - async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent): |
18 |
| - """Gives a role based on a reaction emoji.""" |
19 |
| - # Make sure that the message the user is reacting to is the one we care about. |
20 |
| - if payload.message_id != self.role_message_id: |
21 |
| - return |
22 |
| - |
23 |
| - guild = self.get_guild(payload.guild_id) |
24 |
| - if guild is None: |
25 |
| - # Make sure we're still in the guild, and it's cached. |
26 |
| - return |
27 |
| - |
28 |
| - try: |
29 |
| - role_id = self.emoji_to_role[payload.emoji] |
30 |
| - except KeyError: |
31 |
| - # If the emoji isn't the one we care about then exit as well. |
32 |
| - return |
33 |
| - |
34 |
| - role = guild.get_role(role_id) |
35 |
| - if role is None: |
36 |
| - # Make sure the role still exists and is valid. |
37 |
| - return |
38 |
| - |
39 |
| - try: |
40 |
| - # Finally, add the role. |
41 |
| - await payload.member.add_roles(role) |
42 |
| - except discord.HTTPException: |
43 |
| - # If we want to do something in case of errors we'd do it here. |
44 |
| - pass |
45 |
| - |
46 |
| - async def on_raw_reaction_remove(self, payload: discord.RawReactionActionEvent): |
47 |
| - """Removes a role based on a reaction emoji.""" |
48 |
| - # Make sure that the message the user is reacting to is the one we care about. |
49 |
| - if payload.message_id != self.role_message_id: |
50 |
| - return |
51 |
| - |
52 |
| - guild = self.get_guild(payload.guild_id) |
53 |
| - if guild is None: |
54 |
| - # Make sure we're still in the guild, and it's cached. |
55 |
| - return |
56 |
| - |
57 |
| - try: |
58 |
| - role_id = self.emoji_to_role[payload.emoji] |
59 |
| - except KeyError: |
60 |
| - # If the emoji isn't the one we care about then exit as well. |
61 |
| - return |
62 |
| - |
63 |
| - role = guild.get_role(role_id) |
64 |
| - if role is None: |
65 |
| - # Make sure the role still exists and is valid. |
66 |
| - return |
67 |
| - |
68 |
| - # The payload for `on_raw_reaction_remove` does not provide `.member` |
69 |
| - # so we must get the member ourselves from the payload's `.user_id`. |
70 |
| - member = guild.get_member(payload.user_id) |
71 |
| - if member is None: |
72 |
| - # Make sure the member still exists and is valid. |
73 |
| - return |
74 |
| - |
75 |
| - try: |
76 |
| - # Finally, remove the role. |
77 |
| - await member.remove_roles(role) |
78 |
| - except discord.HTTPException: |
79 |
| - # If we want to do something in case of errors we'd do it here. |
80 |
| - pass |
81 |
| - |
| 2 | +import asyncio |
82 | 3 |
|
83 | 4 | intents = discord.Intents.default()
|
84 |
| -intents.members = True |
85 |
| - |
86 |
| -client = MyClient(intents=intents) |
87 |
| -client.run("TOKEN") |
| 5 | +intents.members = True # < This may give you `read-only` warning, just ignore it. |
| 6 | +# This intent requires "Server Members Intent" to be enabled at https://discord.com/developers |
| 7 | + |
| 8 | + |
| 9 | +bot = discord.Bot(intents=intents) |
| 10 | + |
| 11 | +role_message_id = 0 # ID of the message that can be reacted to for adding/removing a role. |
| 12 | + |
| 13 | +emoji_to_role = { |
| 14 | + discord.PartialEmoji(name="🔴"): 0, # ID of the role associated with unicode emoji '🔴'. |
| 15 | + discord.PartialEmoji(name="🟡"): 0, # ID of the role associated with unicode emoji '🟡'. |
| 16 | + discord.PartialEmoji(name="green", id=0): 0, # ID of the role associated with a partial emoji's ID. |
| 17 | +} |
| 18 | + |
| 19 | + |
| 20 | +@bot.event |
| 21 | +async def on_ready(): |
| 22 | + print('Ready!') |
| 23 | + |
| 24 | + |
| 25 | +@bot.event |
| 26 | +async def on_raw_reaction_add(payload: discord.RawReactionActionEvent): |
| 27 | + """Gives a role based on a reaction emoji.""" |
| 28 | + # Make sure that the message the user is reacting to is the one we care about. |
| 29 | + if payload.message_id != role_message_id: |
| 30 | + return |
| 31 | + |
| 32 | + guild = bot.get_guild(payload.guild_id) |
| 33 | + if guild is None: |
| 34 | + # Make sure we're still in the guild, and it's cached. |
| 35 | + return |
| 36 | + |
| 37 | + try: |
| 38 | + role_id = emoji_to_role[payload.emoji] |
| 39 | + except KeyError: |
| 40 | + # If the emoji isn't the one we care about then exit as well. |
| 41 | + return |
| 42 | + |
| 43 | + role = guild.get_role(role_id) |
| 44 | + if role is None: |
| 45 | + # Make sure the role still exists and is valid. |
| 46 | + return |
| 47 | + |
| 48 | + try: |
| 49 | + # Finally, add the role. |
| 50 | + await payload.member.add_roles(role) |
| 51 | + except discord.HTTPException: |
| 52 | + # If we want to do something in case of errors we'd do it here. |
| 53 | + pass |
| 54 | + |
| 55 | + |
| 56 | +@bot.event |
| 57 | +async def on_raw_reaction_remove(payload: discord.RawReactionActionEvent): |
| 58 | + """Removes a role based on a reaction emoji.""" |
| 59 | + # Make sure that the message the user is reacting to is the one we care about. |
| 60 | + if payload.message_id != role_message_id: |
| 61 | + return |
| 62 | + |
| 63 | + guild = bot.get_guild(payload.guild_id) |
| 64 | + if guild is None: |
| 65 | + # Make sure we're still in the guild, and it's cached. |
| 66 | + return |
| 67 | + |
| 68 | + try: |
| 69 | + role_id = emoji_to_role[payload.emoji] |
| 70 | + except KeyError: |
| 71 | + # If the emoji isn't the one we care about then exit as well. |
| 72 | + return |
| 73 | + |
| 74 | + role = guild.get_role(role_id) |
| 75 | + if role is None: |
| 76 | + # Make sure the role still exists and is valid. |
| 77 | + return |
| 78 | + |
| 79 | + # The payload for `on_raw_reaction_remove` does not provide `.member` |
| 80 | + # so we must get the member ourselves from the payload's `.user_id`. |
| 81 | + member = guild.get_member(payload.user_id) |
| 82 | + if member is None: |
| 83 | + # Make sure the member still exists and is valid. |
| 84 | + return |
| 85 | + |
| 86 | + try: |
| 87 | + # Finally, remove the role. |
| 88 | + await member.remove_roles(role) |
| 89 | + except discord.HTTPException: |
| 90 | + # If we want to do something in case of errors we'd do it here. |
| 91 | + pass |
| 92 | + |
| 93 | + |
| 94 | +bot.run("TOKEN") |
0 commit comments