Py-Cord开发报错:UnboundLocalError: embed变量未赋值即引用
Py-Cord机器人
redeem命令UnboundLocalError问题解决 我在开发Discord机器人的redeem斜杠命令时遇到了UnboundLocalError: local variable 'embed' referenced before assignment错误。用普通文本回复功能正常,但使用Embed就触发该错误,猜测是缩进问题但不知道具体解决方法。
相关代码
@bot.slash_command(name = "redeem", description = "Redeem your Key") async def redeem(ctx, key): with open('order_data.txt', 'r') as file: lines = file.readlines() for line in lines: product, order_key = line.strip().split(':') if order_key == key: with open('consumed_keys.txt', 'r') as consumed_file: consumed_keys = consumed_file.readlines() if key in consumed_keys: await ctx.send(f"Duplicate key sent by {ctx.author.mention}: {key}") else: with open('consumed_keys.txt', 'a') as consumed_file: consumed_file.write(f'{key}\n') print(f'Successfully verified {ctx.author.name}\''s key: {key}') role = discord.utils.get(ctx.guild.roles, name='Customer') await ctx.author.add_roles(role) ## EMBED++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ embed = discord.Embed( title="Order Verification", description=f"{ctx.author.mention}, Key Verified", color=discord.Colour.green(), ) embed.add_field(name="Product Name", value=product, inline=True) embed.add_field(name="Product Key", value=key, inline=True) embed.set_footer(text="Test Footer") await ctx.respond(embed=embed) break else: print(f'Verification failed by {ctx.author.name}') await ctx.send(f"Invalid order id, {ctx.author.mention}")
完整报错信息
Traceback (most recent call last): File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\commands\core.py", line 124, in wrapped ret = await coro(arg) File "C:\Users\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\commands\core.py", line 982, in _invoke await self.callback(ctx, **kwargs) File "C:\Users\Desktop\dc bot\redeem bot rewrite\bot.py", line 38, in redeem embed.add_field(name="Product Name", value=product, inline=True) UnboundLocalError: local variable 'embed' referenced before assignment
问题原因及解决方法
错误根源是缩进错误:
embed = discord.Embed(...)只在key未被消耗的else分支里定义,但embed.add_field、embed.set_footer这些操作却缩进在if order_key == key:外面。- 当key匹配但已经被消耗时(进入
if key in consumed_keys:分支),embed根本没被定义,却要执行embed.add_field,直接触发UnboundLocalError。
修正方法:把所有操作embed的代码(包括add_field、set_footer、ctx.respond)都缩进到创建embed的那个else分支里,确保只有在embed被定义后才调用它。同时调整break的位置,确保处理完key的匹配逻辑后再跳出循环。
修正后的代码
@bot.slash_command(name = "redeem", description = "Redeem your Key") async def redeem(ctx, key): with open('order_data.txt', 'r') as file: lines = file.readlines() for line in lines: product, order_key = line.strip().split(':') if order_key == key: with open('consumed_keys.txt', 'r') as consumed_file: consumed_keys = consumed_file.readlines() if key in consumed_keys: await ctx.send(f"Duplicate key sent by {ctx.author.mention}: {key}") else: with open('consumed_keys.txt', 'a') as consumed_file: consumed_file.write(f'{key}\n') print(f'Successfully verified {ctx.author.name}\''s key: {key}') role = discord.utils.get(ctx.guild.roles, name='Customer') await ctx.author.add_roles(role) # 创建Embed并完成所有配置 embed = discord.Embed( title="Order Verification", description=f"{ctx.author.mention}, Key Verified", color=discord.Colour.green(), ) embed.add_field(name="Product Name", value=product, inline=True) embed.add_field(name="Product Key", value=key, inline=True) embed.set_footer(text="Test Footer") await ctx.respond(embed=embed) break # 匹配到key后跳出循环 else: print(f'Verification failed by {ctx.author.name}') await ctx.send(f"Invalid order id, {ctx.author.mention}")
内容的提问来源于stack exchange,提问作者ilija
相关产品推荐
相关产品推荐

