Answer the question
In order to leave comments, you need to log in
UnboundLocalError: local variable 'role' referenced before assignment how to fix discord py error?
I'm trying to create a bot that will give out a role by reacting to an emoji. But when I click on react I get an error
line 28, in on_raw_reaction_add
if role is not None:
UnboundLocalError: local variable 'role' referenced before assignment
import logging
import discord
from discord.ext import commands
from discord.utils import get
client = commands.Bot(command_prefix = ".",intents = discord.Intents.all())
@client.event
async def on_ready():
print(discord.__version__)
@client.event
async def on_raw_reaction_add(payload):
guild = client.get_guild(payload.guild_id)
member = get(guild.members, id=payload.user_id)
#role = 0
Channel_ID = 815951876708892672
Message_ID = 825491143315030048
if payload.channel_id == Channel_ID and payload.message_id == Message_ID:
if str(payload.emoji) == ":pubg:":
role = get(payload.member.guild.roles, id = 817371069547020308)
if role is not None:
await payload.member.add_roles(role)
client.run("My token")
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question