不和谐警告/罢工系统

时间:2020-09-21 02:10:59

标签: python discord.py-rewrite

我对discord.py很陌生,并且为个人服务器制作了一个机器人,但我的warn命令一直没有起作用。这是我目前拥有的:


@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

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

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来显式跳过它。

更好的是,您可以先执行一个循环,使 just 找到正确的channel,然后继续执行该循环的其余逻辑 。我在这里假设ctx.guild.channels之一就是您想要的那个。也许您可以做得更好,即直接查找它。尝试再阅读一些文档。