Rework the reaction roles example
This commit is contained in:
parent
36318bd45c
commit
e961fdeae0
@ -1,83 +1,85 @@
|
|||||||
"""Uses a messages to add and remove roles through reactions."""
|
# This example requires the 'members' privileged intents
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
|
|
||||||
class RoleReactClient(discord.Client):
|
class MyClient(discord.Client):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
self.role_message_id = 0 # ID of message that can be reacted to to add role
|
self.role_message_id = 0 # ID of the message that can be reacted to to add/remove a role.
|
||||||
self.emoji_to_role = {
|
self.emoji_to_role = {
|
||||||
partial_emoji_1: 0, # ID of role associated with partial emoji object 'partial_emoji_1'
|
discord.PartialEmoji(name='🔴'): 0, # ID of the role associated with unicode emoji '🔴'.
|
||||||
partial_emoji_2: 0 # ID of role associated with partial emoji object 'partial_emoji_2'
|
discord.PartialEmoji(name='🟡'): 0, # ID of the role associated with unicode emoji '🟡'.
|
||||||
|
discord.PartialEmoji(name='green', id=0): 0, # ID of the role associated with a partial emoji's ID.
|
||||||
}
|
}
|
||||||
|
|
||||||
async def on_raw_reaction_add(self, payload):
|
async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent):
|
||||||
"""Gives a role based on a reaction emoji."""
|
"""Gives a role based on a reaction emoji."""
|
||||||
# Make sure that the message the user is reacting to is the one we care about
|
# Make sure that the message the user is reacting to is the one we care about.
|
||||||
if payload.message_id != self.role_message_id:
|
if payload.message_id != self.role_message_id:
|
||||||
return
|
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
|
||||||
|
|
||||||
try:
|
try:
|
||||||
role_id = self.emoji_to_role[payload.emoji]
|
role_id = self.emoji_to_role[payload.emoji]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
# If the emoji isn't the one we care about then exit as well.
|
# If the emoji isn't the one we care about then exit as well.
|
||||||
return
|
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)
|
role = guild.get_role(role_id)
|
||||||
if role is None:
|
if role is None:
|
||||||
# Make sure the role still exists and is valid.
|
# Make sure the role still exists and is valid.
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Finally add the role
|
# Finally, add the role.
|
||||||
await payload.member.add_roles(role)
|
await payload.member.add_roles(role)
|
||||||
except discord.HTTPException:
|
except discord.HTTPException:
|
||||||
# If we want to do something in case of errors we'd do it here.
|
# If we want to do something in case of errors we'd do it here.
|
||||||
pass
|
pass
|
||||||
|
|
||||||
async def on_raw_reaction_remove(self, payload):
|
async def on_raw_reaction_remove(self, payload: discord.RawReactionActionEvent):
|
||||||
"""Removes a role based on a reaction emoji."""
|
"""Removes a role based on a reaction emoji."""
|
||||||
# Make sure that the message the user is reacting to is the one we care about
|
# Make sure that the message the user is reacting to is the one we care about.
|
||||||
if payload.message_id != self.role_message_id:
|
if payload.message_id != self.role_message_id:
|
||||||
return
|
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
|
||||||
|
|
||||||
try:
|
try:
|
||||||
role_id = self.emoji_to_role[payload.emoji]
|
role_id = self.emoji_to_role[payload.emoji]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
# If the emoji isn't the one we care about then exit as well.
|
# If the emoji isn't the one we care about then exit as well.
|
||||||
return
|
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)
|
role = guild.get_role(role_id)
|
||||||
if role is None:
|
if role is None:
|
||||||
# Make sure the role still exists and is valid.
|
# Make sure the role still exists and is valid.
|
||||||
return
|
return
|
||||||
|
|
||||||
|
# The payload for `on_raw_reaction_remove` does not provide `.member`
|
||||||
|
# so we must get the member ourselves from the payload's `.user_id`.
|
||||||
member = guild.get_member(payload.user_id)
|
member = guild.get_member(payload.user_id)
|
||||||
if member is None:
|
if member is None:
|
||||||
# Makes sure the member still exists and is valid
|
# Make sure the member still exists and is valid.
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Finally, remove the role
|
# Finally, remove the role.
|
||||||
await member.remove_roles(role)
|
await member.remove_roles(role)
|
||||||
except discord.HTTPException:
|
except discord.HTTPException:
|
||||||
# If we want to do something in case of errors we'd do it here.
|
# If we want to do something in case of errors we'd do it here.
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# This bot requires the members and reactions intents.
|
|
||||||
intents = discord.Intents.default()
|
intents = discord.Intents.default()
|
||||||
intents.members = True
|
intents.members = True
|
||||||
|
|
||||||
client = RoleReactClient(intents=intents)
|
client = MyClient(intents=intents)
|
||||||
client.run("token")
|
client.run('token')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user