Willwell
Willwell

Reputation: 166

discord.py; Warn/Strike system

I am pretty new to discord.py, and making a bot for a personal server, and my warn command hasn't been working. This is what I currently have:


@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. ")

This is the error I am getting:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: UnboundLocalError: local variable 'embed' referenced before assignment

Any help would be appreciated.

Upvotes: 0

Views: 1293

Answers (1)

Karl Knechtel
Karl Knechtel

Reputation: 61515

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")

I assume that, if the channel is a different one, you desire for nothing else to happen for that loop iteration. You need, therefore, to use continue to skip it explicitly.

Better yet, you could first do a loop that just finds the correct channel, and then proceed with the rest of the logic outside that loop. I am assuming here that exactly one of the ctx.guild.channels is the one you want. Maybe you can do even better, i.e., directly look it up. Try reading the documentation some more.

Upvotes: 1

Related Questions