0

这是我用于角色管理的代码,用户可以使用 addrole 命令为成员分配角色。

@commands.group(name="addrole", invoke_without_command=True)
@commands.has_permissions(administrator=True, manage_roles=True)
async def addRole(self, ctx, member: discord.Member = None, role: BetterRoles=None):
    # Check if the user is specified
    if member is None:
        return await ctx.send("You need to specify the member.")
    # Checks if the role is specified
    if role is None:
        return await ctx.send("You need to specify a role")
    # Checks if the user is not in the specified role
    if role not in member.roles:
        try:
            await member.add_roles(role)
            await ctx.send(f"@everyone {member.name} is now part of {role.name}.")
        except: ctx.send("Something went wrong.")
    else:
        await ctx.send(f"{member.name} is already in {role.name}")

@addRole.error
async def role_error(self, error, ctx):
    if isinstance(error, commands.CheckFailure):
        await ctx.send("You do not have role management permissions.")
    elif isinstance(error, commands.BadArgument):
        await ctx.send("Could not identify target")
    else: raise error

不幸的是,我收到了这个错误:

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\Frank Liang\Desktop\frankitacker bot\env\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\Frank Liang\Desktop\frankitacker bot\main.py", line 41, in on_message
    await self.process_commands(message)
  File "C:\Users\Frank Liang\Desktop\frankitacker bot\env\lib\site-packages\discord\ext\commands\bot.py", line 939, in process_commands
    await self.invoke(ctx)
  File "C:\Users\Frank Liang\Desktop\frankitacker bot\env\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\Frank Liang\Desktop\frankitacker bot\env\lib\site-packages\discord\ext\commands\core.py", line 1345, in invoke
    await super().invoke(ctx)
  File "C:\Users\Frank Liang\Desktop\frankitacker bot\env\lib\site-packages\discord\ext\commands\core.py", line 856, in invoke
    await self.prepare(ctx)
  File "C:\Users\Frank Liang\Desktop\frankitacker bot\env\lib\site-packages\discord\ext\commands\core.py", line 778, in prepare
    if not await self.can_run(ctx):
  File "C:\Users\Frank Liang\Desktop\frankitacker bot\env\lib\site-packages\discord\ext\commands\core.py", line 1081, in can_run
    return await discord.utils.async_all(predicate(ctx) for predicate in predicates)
  File "C:\Users\Frank Liang\Desktop\frankitacker bot\env\lib\site-packages\discord\utils.py", line 336, in async_all
    for elem in gen:
  File "C:\Users\Frank Liang\Desktop\frankitacker bot\env\lib\site-packages\discord\ext\commands\core.py", line 1081, in <genexpr>
    return await discord.utils.async_all(predicate(ctx) for predicate in predicates)
  File "C:\Users\Frank Liang\Desktop\frankitacker bot\env\lib\site-packages\discord\ext\commands\core.py", line 1773, in predicate
    permissions = ch.permissions_for(ctx.author)
AttributeError: 'Object' object has no attribute 'permissions_for'

有人可以解释为什么以及如何解决这个问题吗?

4

0 回答 0