如何在给定命令时让机器人分配角色

时间:2021-01-11 17:50:36

标签: discord.py

我试图让机器人赋予“商场”角色并删除“学习大厅”角色。但是,它不断返回错误

<块引用>

忽略 on_message 中的异常 回溯(最近一次调用最后一次): 文件“C:\Users\frost\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py”,第 333 行,在 _run_event 中 等待 coro(*args, **kwargs) 文件“D:\GalaxyConflux Master\gcclient.py”,第 89 行,在 on_message 角色.id = 798193741403127818 AttributeError: 'str' 对象没有属性 'id'

正在运行的代码是这样的

@client.event
async def on_message(message):
    if message.content.startswith(gccmd.cmd_prfx + 'mall'):
        role = 'Mall'
        role.id = 798193741403127818
        await message.author.add_roles(role.id)

我尝试了许多不同的方法,但每种方法都会导致类似的错误。 我只是让它脱离了角色 = 'Mall' 部分,但这也不起作用。

如何将角色 ID 转换为实际 ID?

非常感谢! ~微光

1 个答案:

答案 0 :(得分:0)

这是一个示例,向您展示了如何添加和删除角色。

@client.command()
async def mall(ctx):
    # if prefix is '-' you'd type: -mall
    await user.remove_roles(@ROLE_ID)
    await user.add_roles(@ROLE_ID)
    await ctx.send("Mall role added!")

@client.command()
async def home(ctx):
    # if prefix is '-' you'd type: -home
    await user.remove_roles(@ROLE_ID)
    await user.add_roles(@ROLE_ID)
    await ctx.send("Home role added!") 

如果您想删除多个角色,您只需添加要删除的所有角色,然后添加您想要的角色。

例如,如果您从家中前往商场,它会删除所有角色,包括房子角色,并添加商场角色。