简体   繁体   中英

Discord.py warn command not sending embed

I am trying to make a warn command using discord.py and mongodb as the database. The database is used to store the punishment and find the channel the logging embed needs to be sent to.

This is my current code:

    @commands.command()
    @commands.has_permissions(manage_messages=True)
    async def warn(self, ctx, member: discord.Member, *, reason=None):
        ctx.logging = cluster["Logging"]["Punishments logs"]
        self.channel = cluster["Logging"]["logchannels"]
        embed = discord.Embed(title="\u200b", color=0x008000)
        warn_embed = discord.Embed(
            colour=discord.Color.magenta()
            )
        warn_embed.set_author(name='Punishment Notifcation')
        warn_embed.add_field(name=f'You have been warned in **{ctx.guild.name}**', value=f'For `{reason}.`', inline=True)
        if member == ctx.author:
            await ctx.channel.send("You can't warn yourself :person_facepalming:")
            return
        elif reason == None:
            reason = "No stated reason"
            await member.send(embed=warn_embed)
            await ctx.send(f'{member.mention} has been warned.')
            print(f'{ctx.author} warned {member.mention} for {reason}')
            ctx.logging.insert_one({"guild_id": ctx.guild.id, "action": "warn", "moderator": ctx.author.id, "user": member.id, "reason": reason})
            channel = self.setup.find_one({"guild_id": ctx.guild.id})
            if channel is not None:
                elif_embed = discord.Embed(
                    title=f'Warn | Case (case number in future)',
                    color=discord.Color.magenta()
                    )
                elif_embed.set_author(name=member, url=discord.Embed.Empty, icon_url=member.avatar_url)
                elif_embed.add_field(name=f'**Offender:**', value=f'{member} {member.mention}')
                elif_embed.add_field(name=f'**Reason:**', value=f'{reason}')
                elif_embed.add_field(name='**Responsible Moderator:**', value=f'{ctx.author} {ctx.author.mention}')
                await ctx.send(embed=elif_embed)
            else:
                await ctx.send('This command cannot be logged as moderation logs have not been enabled. To enable them do: **+setupmod <channel>**')

        else:
            await member.send(embed=warn_embed)
            await ctx.send(f'{member.mention} has been warned by {ctx.author.mention}')
            print(f'{ctx.author} warned {member} for {reason}')
            ctx.logging.insert_one({"guild_id": ctx.guild.id, "action": "warn", "moderator": ctx.author.id, "user": member.id, "reason": reason})
            channel = self.setup.find_one({"guild_id": ctx.guild.id})
            if channel is not None:
                else_embed = discord.Embed(
                    title=f'Warn | Case (case number in future)',
                    color=discord.Color.magenta()
                    )
                else_embed.set_author(name=member, url=discord.Embed.Empty, icon_url=member.avatar_url)
                else_embed.add_field(name=f'**Offender:**', value=f'{member} {member.mention}')
                else_embed.add_field(name=f'**Reason:**', value=f'`{reason}`')
                else_embed.add_field(name='**Responsible Moderator:**', value=f'{ctx.author} {ctx.author.mention}')
                await ctx.send(embed=else_embed)
            else:
                await ctx.send('This command cannot be logged as moderation logs have not been enabled. To enable them do: **+setupmod <channel>**')

The expected result is that it: In the channel the command was run it sends a message confirming it worked, It dms the user informing them they have been warned, And if a log channel has been set up it sends an embed to that channel

The actual result is that the first two parts work, however the embed does not send to the correct channel (It has been set up) And I get this error:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\name\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "c:\Users\name\Documents\Discord Bot\Testcode.py", line 98, in on_message
    guild = setup.find_one({"guild_id": message.guild.id})
AttributeError: 'NoneType' object has no attribute 'id'


Discord Username #1234 warned <@!759497745886085132> for No stated reason
Ignoring exception in on_command_error
Traceback (most recent call last):
  File "C:\Users\name\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:\Users\name\Documents\Discord Bot\cogs\moderation.py", line 100, in warn
    channel = self.setup.find_one({"guild_id": ctx.guild.id})
AttributeError: 'Moderation' object has no attribute 'setup'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\name\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "c:\Users\name\Documents\Discord Bot\Testcode.py", line 231, in on_command_error
    raise error
  File "C:\Users\name\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\name\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\name\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Moderation' object has no attribute 'setup'

I have tried using print commands to find the error however this has not worked, I have also attempted to use discord support servers where people have been unable to help with this issue. On top of this, I have looked through some of my working code and the discord.py docs and have found nothing that can help in this situation.

Any help with solutions for this would be appreciated!

Note: If anyone has knowledge of mongodb, could you also suggest a way I could make a system that assigns each punishment an ID. Thanks

EDIT: This is the part of the code the first part of the error is refering to:

@bot.event
async def on_message(message):
    setup = cluster["Atomic_Developer_Bot"]["prefix"]
    guild = setup.find_one({"guild_id": message.guild.id})
    prefix = guild["prefix"]
    if message.content.startswith('<@850794452364951554>'):
        await message.channel.send(f'The current prefix is **{prefix}**')
    else:
        print(prefix)
    await bot.process_commands(message)

I think the notification for the user (going through dm) is the one used in on_message based on your comment. Try this:

@bot.event
async def on_message(message):
    if message.author.id != your_bot_id_here:
        setup = cluster["Atomic_Developer_Bot"]["prefix"]
        guild = setup.find_one({"guild_id": message.guild.id})
        prefix = guild["prefix"]
        if message.content.startswith('<@850794452364951554>'):
        await message.channel.send(f'The current prefix is **{prefix}**')
        else:
            print(prefix)
        await bot.process_commands(message)

AttributeError: 'Moderation' object has no attribute 'setup' means there is no "setup" function for your cog. You have to add one so that the cog loads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM