【问题标题】:Discord.py Reaction bot that gives a roleDiscord.py 提供角色的反应机器人
【发布时间】:2019-04-20 19:29:21
【问题描述】:

如何制作一个机器人,让人们在对特定事物做出反应时赋予他们角色?到目前为止我有这个,但它不起作用

@client.event
async def on_ready():
    channel = client.get_channel('513546504481406979')
    role = discord.utils.get(user.server.roles, name="testrole")
    message = await bot.send_message(channel, "React to me!")
    while True:
        reaction = await bot.wait_for_reaction(emoji="????", message=message)
        await bot.add_roles(reaction.message.author, role)

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    wait_for_reaction 返回一个 (reaction, user) 元组。您只需要 user 部分来分配角色:

    reaction, reactor = await bot.wait_for_reaction(emoji="?", message=message)
    await bot.add_roles(reactor, role)
    

    【讨论】:

    • 我不断收到错误忽略 on_ready Traceback 中的异常(最近一次调用最后一次):文件“/home/runner/.site-packages/discord/client.py”,第 307 行,在 _run_event yield来自 getattr(self, event)(*args, **kwargs) 文件“python”,第 11 行,on_ready UnboundLocalError: local variable 'user' referenced before assignment
    • 您是否有一些全局变量user 用于获取角色? (您可以只使用角色 ID)。您可以将我的代码中的名称 user 替换为任何其他名称
    • 我没有全局用户变量 user.server.roles 由于某种原因无法正常工作
    • 原因是没有定义user。请改用channel.server.roles
    猜你喜欢
    • 1970-01-01
    • 2021-05-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2022-10-30
    • 2021-06-12
    • 1970-01-01
    • 2021-04-02
    相关资源
    最近更新 更多