Created
September 12, 2022 15:34
-
-
Save Hunter87ff/95b13620e68c355b95925eac92f382bc to your computer and use it in GitHub Desktop.
Reaction Role (support custom emoji) with discord.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Note: You can use database to store values | |
# Only Custom emoji Supported | |
@bot.command() | |
async def rr_add(ctx, channel : discord.TextChannel, role :discord.Role, message_id : int, emoji:discord.Emoji): | |
message = await channel.fetch_message(message_id) | |
await message.add_reaction(emoji) | |
if emch.is_emoji(emoji): | |
return await ctx.reply("Defabult Emoji Not allowed") | |
#emoj = emoji | |
if emch.is_emoji(emoji) == False: | |
emoj = emoji.id | |
data = [{ | |
"channel" : channel.id, | |
"role": role.id, | |
"message" : message_id, | |
"emoji": emoj | |
}] | |
with open('dta.json', 'w') as f: | |
json.dump(data, f) | |
@bot.event | |
async def on_raw_reaction_add(payload): | |
if payload.member.bot: | |
return | |
with open("dta.json", "r") as f: | |
dta = json.load(f) | |
for dt in dta: | |
#print(dt) | |
if payload.channel_id == dt["channel"]: | |
if payload.message_id == dt["message"]: | |
if payload.emoji.id == dt["emoji"]: | |
role = discord.utils.get(bot.get_guild(payload.guild_id).roles, id=dt["role"]) | |
await payload.member.add_roles(role, reason="Role added By Reaction") | |
@bot.event | |
async def on_raw_reaction_remove(payload): | |
with open("dta.json", "r") as f: | |
dta = json.load(f) | |
for dt in dta: | |
#print(dt) | |
if payload.channel_id == dt["channel"]: | |
if payload.message_id == dt["message"]: | |
if payload.emoji.id == dt["emoji"]: | |
role = discord.utils.get(bot.get_guild(payload.guild_id).roles, id=dt["role"]) | |
guild = bot.get_guild(payload.guild_id) | |
member = await guild.fetch_member(payload.user_id) | |
await member.remove_roles(role) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment