discord.py; Warn/Strike系统
discord.py; Warn/Strike system
我是 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
如有任何帮助,我们将不胜感激。
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")
我假设,如果频道是不同的频道,您不希望该循环迭代发生任何其他事情。因此,您需要使用 continue
明确跳过它。
更好的是,您可以先做一个循环,只是 找到正确的 channel
,然后在 之外继续执行其余逻辑 那个循环。我在这里假设 ctx.guild.channels
中的一个正是您想要的。也许你可以做得更好,即直接查找。尝试多阅读文档。
我是 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
如有任何帮助,我们将不胜感激。
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")
我假设,如果频道是不同的频道,您不希望该循环迭代发生任何其他事情。因此,您需要使用 continue
明确跳过它。
更好的是,您可以先做一个循环,只是 找到正确的 channel
,然后在 之外继续执行其余逻辑 那个循环。我在这里假设 ctx.guild.channels
中的一个正是您想要的。也许你可以做得更好,即直接查找。尝试多阅读文档。