首页 > 解决方案 > discord.py 错误,同时给一个人一个静音的角色

问题描述

我正在尝试制作一个具有某些功能的不和谐机器人。其中一个包括一个静音功能,由于某种原因对我来说不起作用

代码

@bot.command(pass_context=True)
@commands.has_role("gl3tche")
async def mute(ctx, member:discord.Member, *, reason = None):
    await ctx.channel.purge(limit=1)
    print(member)
    if not member:
        await ctx.channel.send(embed=Embed(title="Mute").add_field("*mute {user}", "Mutes the user"))
    else:
        #member = ctx.author
        try:
            role = discord.utils.get(ctx.guild.roles, name="muted")
        except:
            await ctx.guild.create_role(name="muted")
            role = discord.utils.get(ctx.guild.roles, name="muted")
        #perms = discord.Permissions()
        #perms.update(read_messages = True, read_message_history = True, connect = True, speak = False, send_messages = False)
        await member.add_roles(role)
        await member.add_roles(role)
        await ctx.channel.send(f"{member.mention} has been muted")

我得到的错误:

Ignoring exception in command mute:
Traceback (most recent call last):
  File "C:\Users\saketh\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\saketh\OneDrive\Documents\Code\Python\Bots\BU\NewBot.py", line 89, in mute
    await member.add_roles(role)
  File "C:\Users\saketh\AppData\Local\Programs\Python\Python39\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 "C:\Users\saketh\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\saketh\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\saketh\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: 'NoneType' object has no attribute 'id'

有人能帮助我吗?将不胜感激:D

标签: pythondiscord.py

解决方案


看用户端

成员:discord.Member


推荐阅读