【发布时间】:2020-09-21 10:46:19
【问题描述】:
我对 discord.py 很陌生,正在为个人服务器制作一个机器人,但我的警告命令一直不起作用。这是我目前拥有的:
@client.command()
async def warn(ctx, member: discord.Member, *, reason=None):
for channel in ctx.guild.channels:
if str(channel) == "????moderation-logs":
embed = discord.Embed(timestamp=ctx.message.created_at, color=0x00FFFF)
embed.set_footer(text="Olympia Gaming | Manual Moderation")
warnid = random.randint(9000000000, 12000000000000)
post = {
"guild": ctx.guild.id,
"member": member.id,
"warnid": warnid,
"moderator": ctx.message.author.name,
"reason": reason,
"date": str(ctx.message.created_at)
}
msg.author = ctx.message.author
if member.top_role < msg.author.top_role:
collection.insert_one(post)
embed.set_author(name=f"{member} has been succesfully warned!", icon_url="https://cdn.discordapp.com/icons/690937143522099220/34fbd058360c3d4696848592ff1c5191.webp?size=1024")
embed.add_field(name="Reason:", value=f"{reason}")
embed.add_field(name="Warn ID:", value=f"{warnid}")
await ctx.send(embed=embed)
await member.send(embed=embed)
embed.add_field(name="Staff Member:", value=f"{ctx.author.mention}({ctx.author.id})")
await channel.send(embed=embed)
elif msg.author.id == ctx.guild.owner_id:
if msg.author.id == member.id:
await ctx.send(f" You need your role higher than {member.name}'s to execute this command. ")
else:
collection.insert_one(post)
embed.set_author(name=f"{member} has been succesfully warned!", icon_url="https://cdn.discordapp.com/icons/690937143522099220/34fbd058360c3d4696848592ff1c5191.webp?size=1024")
embed.add_field(name="Reason:", value=f"{reason}")
embed.add_field(name="Warn ID:", value=f"{warnid}")
await ctx.send(embed=embed)
await member.send(embed=embed)
embed.add_field(name="Staff Member:", value=f"{ctx.author.mention}({ctx.author.id})")
await channel.send(embed=embed)
else:
await ctx.send(f" You need your role higher than {member.name}'s to execute this command. ")
这是我得到的错误:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: UnboundLocalError: local variable 'embed' referenced before assignment
任何帮助将不胜感激。
【问题讨论】:
-
如果频道不是审核频道,
embed不存在。您必须将embed = blah移到 for 循环之外,以便函数的其余部分可以使用它。