Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

python - UnboundLocalError: local variable 'member_id' referenced before assignment in discord.py

I don't understand what im doing wrong here

@commands.command()
async def level(self, ctx, member: discord.Member = None):
    member = ctx.author if not member else member
    member_id = str(member_id)
    guild_id = str(ctx.guild.id)
    
    user = await self.bot.pg_con.fetch("SELECT * FROM users WHERE user_id = $1 AND guild_id = $2", member_id, guild_id)

its giving me this error

UboundLocalError: local variable 'member_id' referenced before assignment

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From the code that you have posted on here, it seems that you are trying to use the variable member_id without having declared it. If member_id is not a global variable, it will have to be declared and assigned a value before it can be used or converted to a string. An example would be so:

@commands.command()
async def level(self, ctx, member: discord.Member = None):
    member = ctx.author if not member else member
    #Assuming member has the id
    member_id = member.id #declaring member_id here
    member_id = str(member_id)
    guild_id = str(ctx.guild.id)
    
    user = await self.bot.pg_con.fetch("SELECT * FROM users WHERE user_id = $1 AND guild_id = $2", member_id, guild_id)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...