在对帖子做出反应时出现添加角色的问题 [英] Issue adding a role when reacting to a post

查看:39
本文介绍了在对帖子做出反应时出现添加角色的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户对帖子做出反应时,添加角色时遇到问题.

I'm having an issue adding a role when a user reacts to a post.

我希望它的功能是,当用户加入Discord服务器时,机器人将使用 on_join 事件发送消息(目前,我正在使用命令测试以进行测试).

How I'm wanting it to function is that when a user joins the Discord server the bot will send send a message using the on_join event (for now I'm using the command test for testing purposes).

下一步是 on_reaction_add 事件,当用户对此消息作出反应时,机器人将向该用户添加角色.

The next step is the on_reaction_add event, when the user reacts to this message the bot will add the role to that user.

这就是我正在使用的.我已经对此进行了测试,但没有得到理想的结果.

Here is what I'm working with. I've tested this however, I'm not getting the desired result.

(在discord.py上重写)

(on discord.py rewrite)

 @commands.command(pass_context=True)
async def test(self, user: discord.Member): #lets change this to the command 'test' for now and change on on_join event later. 
    guildName = user.guild.name
    embed = discord.Embed(colour=discord.Color(random.randint(0x000000, 0xFFFFFF)))
    embed.title = "Welcome to {} {}!".format(guildName, user)
    embed.description = "Head over to <#443133907102203912> to have a read up on our rules. Once you have read them please reaction to this post with a :thumbsup:"
    embed.set_image(url='')
    await user.send(embed=embed)

async def on_reaction_add(reaction, user): 
    channelid = '555844758778544160' #The text-channel where the react should happen
    role = discord.utils.get(user.guild.roles, name="Members") #The role applied when the post is reacted to

    if reaction.message.channel.id != channelid:
        return #So it only happens in the specified channel
    if str(reaction.emoji) == "<:thumbsup:555844758778544160>":#The reaction (not sure if this should be raw in discord.py rewrite)
        await user.add_roles(user, role)#Adds role when post is reacted to

推荐答案

如果需要,您可以等待对特定消息的反应.在这样的情况下这很方便,在这种情况下,您可能要等待反应,然后继续做协程.我将在您使用最新重写的情况下编写以下内容.您可以在此处找到所有文档:从discord.utils导入

You can wait for a reaction to a specific message if you need to. This is handy for times like this, where you maybe want to wait for a reaction then keep doing your coroutine. I will write the below assuming you're using the most up-to-date rewrite. You can find the documentation for all this here:

from discord.utils import get
from discord.ext.commands import Cog, command
listener = Cog.listener

thumbs_up = "\N{THUMBS UP SIGN}"

def react_check(user, msg, emoji):
    def check(reaction, usr):
        return usr==user and reaction.message.id==msg.id and reaction.emoji==emoji
    return check

class MyCog(Cog):
    def __init__(self, bot):
        self.bot = bot
    @listener()
    async def on_member_join(self, member):
        guildName = user.guild.name
        embed = discord.Embed(colour=discord.Color(random.randint(0x000000, 0xFFFFFF)))
        embed.title = "Welcome to {} {}!".format(guildName, user)
        embed.description = "Head over to <#443133907102203912> to have a read up on our rules. Once you have read them please reaction to this post with a :thumbsup:"
        embed.set_image(url='')
        msg = await user.send(embed=embed)
        await self.bot.wait_for('reaction_add', check=react_check(user, msg, thumbs_up))
        role = get(user.guild.roles, name="Members")
        await user.add_roles(role)

然后在您的主程序中,您需要显示一行

Then in your main bot, you need a line that says

from mycog import MyCog

bot.add_cog(MyCog(bot))

这篇关于在对帖子做出反应时出现添加角色的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆