1. <small id='Yb5Hi'></small><noframes id='Yb5Hi'>

      <i id='Yb5Hi'><tr id='Yb5Hi'><dt id='Yb5Hi'><q id='Yb5Hi'><span id='Yb5Hi'><b id='Yb5Hi'><form id='Yb5Hi'><ins id='Yb5Hi'></ins><ul id='Yb5Hi'></ul><sub id='Yb5Hi'></sub></form><legend id='Yb5Hi'></legend><bdo id='Yb5Hi'><pre id='Yb5Hi'><center id='Yb5Hi'></center></pre></bdo></b><th id='Yb5Hi'></th></span></q></dt></tr></i><div id='Yb5Hi'><tfoot id='Yb5Hi'></tfoot><dl id='Yb5Hi'><fieldset id='Yb5Hi'></fieldset></dl></div>
        <bdo id='Yb5Hi'></bdo><ul id='Yb5Hi'></ul>
      <legend id='Yb5Hi'><style id='Yb5Hi'><dir id='Yb5Hi'><q id='Yb5Hi'></q></dir></style></legend>

    1. <tfoot id='Yb5Hi'></tfoot>

      当用户对某个频道中的消息做出反应时分配不和

      时间:2023-10-11

          <i id='fQRyE'><tr id='fQRyE'><dt id='fQRyE'><q id='fQRyE'><span id='fQRyE'><b id='fQRyE'><form id='fQRyE'><ins id='fQRyE'></ins><ul id='fQRyE'></ul><sub id='fQRyE'></sub></form><legend id='fQRyE'></legend><bdo id='fQRyE'><pre id='fQRyE'><center id='fQRyE'></center></pre></bdo></b><th id='fQRyE'></th></span></q></dt></tr></i><div id='fQRyE'><tfoot id='fQRyE'></tfoot><dl id='fQRyE'><fieldset id='fQRyE'></fieldset></dl></div>

          <tfoot id='fQRyE'></tfoot>
            <tbody id='fQRyE'></tbody>
          <legend id='fQRyE'><style id='fQRyE'><dir id='fQRyE'><q id='fQRyE'></q></dir></style></legend>
            1. <small id='fQRyE'></small><noframes id='fQRyE'>

              • <bdo id='fQRyE'></bdo><ul id='fQRyE'></ul>
                本文介绍了当用户对某个频道中的消息做出反应时分配不和谐角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我希望用户在我的欢迎和角色不和谐频道中选择某个反应时被分配一个角色.我到处寻找,在 python 中找不到适用于最新版本 discord.py 的代码.这是我目前所拥有的:

                I want a user to be assigned a role when they choose a certain reaction in my welcome-and-roles discord channel. I've looked everywhere and can't find code in python that works with the most up-to-date version of discord.py. Here is what I have so far:

                import discord
                
                client = discord.Client()
                
                TOKEN = os.getenv('METABOT_DISCORD_TOKEN')
                
                
                @client.event
                async def on_reaction_add(reaction, user):
                    role_channel_id = '700895165665247325'
                    if reaction.message.channel.id != role_channel_id:
                        return
                    if str(reaction.emoji) == "<:WarThunder:745425772944162907>":
                        await client.add_roles(user, name='War Thunder')
                
                
                print("Server Running")
                
                client.run(TOKEN)
                

                推荐答案

                使用 on_raw_reaction_add 而不是 on_reaction_add,因为 on_reaction_add 只有在消息在 bot 的缓存中,而 on_raw_reaction_add 将工作,无论内部消息缓存的状态如何.

                Use on_raw_reaction_add instead of on_reaction_add, As on_reaction_add will only work if the message is in bot's cache while on_raw_reaction_add will work regardless of the state of the internal message cache.

                所有的 IDS、角色 ID、频道 ID、消息 ID...,都是 INTEGER 而不是 STRING,这就是您的代码无法正常工作的原因,因为它将 INT 与 STR 进行比较.

                All the IDS, Role IDs, Channel IDs, Message IDs..., are INTEGER not STRING, that is a reason why your code not works, as its comparing INT with STR.

                另外还要获取角色,不能只传入角色的名字

                Also you need to get the role, you can't just pass in the name of the role

                下面是工作代码

                @client.event
                async def on_raw_reaction_add(payload):
                    if payload.channel_id == 123131 and payload.message_id == 12121212: #channel and message IDs should be integer:
                        if str(payload.emoji) == "<:WarThunder:745425772944162907>":
                            role = discord.utils.get(payload.member.guild.roles, name='War Thunder')
                            await payload.member.add_roles(role)
                

                对于 on_raw_reaction_remove

                @client.event
                async def on_raw_reaction_remove(payload):
                    if payload.channel_id == 123131 and payload.message_id == 12121212: #channel and message IDs should be integer:
                        if str(payload.emoji) == "<:WarThunder:745425772944162907>":
                            #we can't use payload.member as its not a thing for on_raw_reaction_remove
                            guild = bot.get_guild(payload.guild_id)
                            member = guild.get_member(payload.user_id)
                            role = discord.utils.get(guild.roles, name='War Thunder')
                            await member.add_roles(role)
                

                这篇关于当用户对某个频道中的消息做出反应时分配不和谐角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:discord.py 机器人可以不区分大小写吗? 下一篇:在单独的线程中执行 run_coroutine_threadsafe

                相关文章

                最新文章

                <i id='sv8dw'><tr id='sv8dw'><dt id='sv8dw'><q id='sv8dw'><span id='sv8dw'><b id='sv8dw'><form id='sv8dw'><ins id='sv8dw'></ins><ul id='sv8dw'></ul><sub id='sv8dw'></sub></form><legend id='sv8dw'></legend><bdo id='sv8dw'><pre id='sv8dw'><center id='sv8dw'></center></pre></bdo></b><th id='sv8dw'></th></span></q></dt></tr></i><div id='sv8dw'><tfoot id='sv8dw'></tfoot><dl id='sv8dw'><fieldset id='sv8dw'></fieldset></dl></div>

                <small id='sv8dw'></small><noframes id='sv8dw'>

                • <bdo id='sv8dw'></bdo><ul id='sv8dw'></ul>
                <legend id='sv8dw'><style id='sv8dw'><dir id='sv8dw'><q id='sv8dw'></q></dir></style></legend>

                1. <tfoot id='sv8dw'></tfoot>