K
K
KirillqqMenn2021-10-10 14:05:59
Python
KirillqqMenn, 2021-10-10 14:05:59

How to fix this error when issuing a role?

Error code:

Traceback (most recent call last):
  File "D:\Bot\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "D:\Bot\bot\cogs\moder.py", line 148, in mute
    await member.add_roles(role)
  File "D:\Bot\venv\lib\site-packages\discord\member.py", line 777, in add_roles
    await req(guild_id, user_id, role.id, reason=reason)
AttributeError: 'NoneType' object has no attribute 'id'

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

Traceback (most recent call last):
  File "D:\Bot\venv\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "D:\Bot\venv\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "D:\Bot\venv\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: 'NoneType' object has no attribute 'id'


My code:
def get_role(self, ctx:commands.Context):
        cur.execute(f'SELECT role_id FROM mutes WHERE guild_id = {ctx.guild.id}')
        muteRole = cur.fetchone()
        db.commit()
        return muteRole

@commands.command(name='set_mute_role')
    @commands.has_permissions(manage_guild = True)
    async def set_mute_role(self, ctx, role: discord.Role, role_id: int = None):
        if role:
            cur.execute(f'UPDATE mutes SET role_id = ? WHERE guild_id = ?', (role.id, ctx.guild.id))
            db.commit()
        else:
            cur.execute(f'UPDATE mutes SET role_id = ? WHERE guild_id = ?', (role_id, ctx.guild.id))
            db.commit()
        
        emb = discord.Embed(title = 'Успешно!', description = f'{ctx.message.author.mention}, роль для мута изменена на {role.mention}', color = 0x82c617)
        await ctx.send(embed=emb)

@commands.command(name='mute')
    @commands.has_permissions(ban_members = True)
    async def mute(self, ctx, member : discord.Member, *, reason = 'Причина не указана.'):
        role = discord.utils.get(ctx.guild.roles, id = Moder.get_role)
        await member.add_roles(role)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-10-10
@sergey-gornostaev

First, making blocking database calls from asynchronous code is a bad idea. Secondly, why do you pass a reference to the class method to the id parameter when you get the role, and not the result of calling the object's method?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question