根据用户反应发送消息

时间:2018-10-08 11:48:48

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

我希望我的机器人根据对用户添加的反应发送一条消息。

我有验证码

@commands.command(pass_context=True)
async def testhelp(self, ctx):
    try:
        Help = discord.Embed(title="Commands and Usage", description="Choose a catagory by adding a reaction", color=0x0072ff)
        Help.add_field(name='General Commands', value='')
        Help.add_field(name='Fun Commands', value='')
        Help.add_field(name='Math Commands', value='✏')
        Help.add_field(name='Anime Commands', value='')
        Help.add_field(name='NSFW Commands', value='')
        Help.add_field(name='Music Commands', value='')
        Help.add_field(name='Mod Commands', value='')
        Help.add_field(name='Admin Commands', value='⚒')
        Help.add_field(name='Owner Commands', value='⚙')
        message = await self.client.say(embed=Help)
        await self.client.add_reaction(message, emoji='')
        await self.client.add_reaction(message, emoji='')
        await self.client.add_reaction(message, emoji='✏')
        await self.client.add_reaction(message, emoji='')
        await self.client.add_reaction(message, emoji='')
        await self.client.add_reaction(message, emoji='')
        await self.client.add_reaction(message, emoji='')
        await self.client.add_reaction(message, emoji='⚒')
        await self.client.add_reaction(message, emoji='⚙')
    except Exception as error:
        await self.client.say('{}'.format(error))

效果很好,我只需要弄清楚当他们添加列出的反应时如何发送不同的消息。

1 个答案:

答案 0 :(得分:1)

为响应创建一个表情符号字典,然后根据该字典检查其反应。在下面,我使用Client.wait_for_reaction仅处理我们从调用命令的人那里知道的响应。

responses = {'': 'General Commands',
             '': 'Fun Commands'
             '✏': 'Math Commands',
             '': 'Anime Commands'}

@commands.command(pass_context=True)
async def testhelp(self, ctx):
    Help = discord.Embed(title="Commands and Usage", description="Choose a catagory by adding a reaction", color=0x0072ff)
    for emoji, name in responses.items():
        Help.add_field(name=name, value=emoji)
    message = await self.client.say(embed=Help)
    for emoji in responses:
        await self.client.add_reaction(message, emoji=emoji)
    res = await self.client.wait_for_reaction(list(responses), user=ctx.message.author)
    await self.client.say("{} you chose {}".format(res.user.mention, responses[res.reaction.emoji]))

如果您希望发送更有意义的消息作为答复,则可以编写接受上下文的协程并发送消息。

responses = {'': 'General Commands',
             '': 'Fun Commands'
             '✏': 'Math Commands',
             '': 'Anime Commands'}

async def general_commands(self, ctx):
    embed = ...
    await self.client.send_message(ctx.message.channel, embed=embed)

@commands.command(pass_context=True)
async def testhelp(self, ctx):
    Help = discord.Embed(title="Commands and Usage", description="Choose a catagory by adding a reaction", color=0x0072ff)
    for emoji, name in responses.items():
        Help.add_field(name=name, value=emoji)
    message = await self.client.say(embed=Help)
    for emoji in responses:
        await self.client.add_reaction(message, emoji=emoji)
    res = await self.client.wait_for_reaction(list(responses), user=ctx.message.author)
    coro = getattr(self, responses[res.reaction.emoji].lower().replace(' ', '_'))
    await coro(ctx)