discord.py;警告/打击系统

2024-05-15 03:29:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚接触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

任何帮助都将不胜感激


Tags: namesendaddidfieldvaluechannelembed
1条回答
网友
1楼 · 发布于 2024-05-15 03:29:48
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中正好有一个是您想要的。也许你可以做得更好,也就是直接查找。试着多读一些文档

相关问题 更多 >

    热门问题