回应帖子时出现添加角色的问题

时间:2019-03-18 15:41:01

标签: python python-3.x discord discord.py discord.py-rewrite

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

我希望它发挥作用的方式是,当用户加入Discord服务器时,机器人将使用on_join事件发送一条消息(目前,我使用命令test测试目的)。

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

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

(在discord.py上重写)

 @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

2 个答案:

答案 0 :(得分:1)

如果需要,您可以等待对特定消息的反应。在这样的情况下这很方便,在这种情况下,您可能要等待反应,然后继续做协程。如果您使用的是最新的重写,我将在下面编写。您可以找到有关所有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)

然后在您的主要漫游器中,需要显示一行

from mycog import MyCog

bot.add_cog(MyCog(bot))

答案 1 :(得分:0)

from discord.ext import commands
import discord
import random
from .utils import checks
from random import randint
from discord.utils import get


thumbs_up = "\N{THUMBS UP SIGN}"

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

class Welcome(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_member_join(self, user: discord.Member):
    guildName = user.guild.name
    channel = self.bot.get_channel(555844758778544160) 
    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 channel.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)
    await channel.send('test')# Check 

def setup(bot):
    bot.add_cog(Welcome(bot))