gpt4 book ai didi

python - 根据用户 react 发送消息

转载 作者:太空宇宙 更新时间:2023-11-03 21:43:04 27 4
gpt4 key购买 nike

我希望我的机器人根据对用户添加的 react 发送消息。

我有代码

@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))

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

最佳答案

制作一个表情符号响应字典,然后根据该字典检查他们的 react 。下面我用Client.wait_for_reaction仅处理我们从调用该命令的人那里了解到的 react 。

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)

关于python - 根据用户 react 发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52701597/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com