不和谐的机器人阅读反应 [英] Discord bot reading reactions
本文介绍了不和谐的机器人阅读反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要实现一些功能,其中一个功能是实现投票类型功能。由于一些政策的原因,我们不能使用公共不和谐机器人,所以我们必须自己实施一些东西。昨天做了一些研究,能够使用discord.ext
中的python3和commands
API制作基本的机器人。现在我需要弄清楚的是:
- 阅读用户添加到邮件的反应?
- 创建带有反应的消息(就像创建反应民意调查的机器人一样?)
- 固定邮件?
- 我相信从
ctx
我可以得到user
tags
(admin等)。有没有更好的办法?
在Commands reference page上找不到任何有用的内容,或者我可能正在查看错误的文档。如有任何帮助,我们将不胜感激。
谢谢
更新:谢谢大家。现在我被如何添加表情卡住了,这是我的代码
poll_emojis = {0: ':zero:', 1: ':one:', 2: ':two:', 3: ':three:', 4: ':four:'}
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$create_poll'):
poll_content = message.content.split('"')
poll_text = poll_content[1]
poll_options = []
poll_option_text = ''
count = 0
for poll_option in poll_content[2:]:
if poll_option.strip() != '':
poll_options.append(poll_option)
poll_option_text += '{0}: {1} '.format(poll_emojis[count], poll_option)
count += 1
posted_message = await message.channel.send('**{0}**
{1}'.format(poll_text, poll_option_text))
count = 0
for poll_option in poll_options:
await posted_message.add_reaction(Emoji(poll_emojis[count]))
count += 1
推荐答案
顺便说一句,考虑到您正在开始此项目,并且已经在使用重写文档,请确保您使用的是重写版本。这里有一些关于如何确保以及如何在不确定的情况下获得它的问题,但它记录得更好,更容易使用。我下面的回答假定您正在使用discord.py-rewrite
获取反应与其计数的映射Message.reactions
是Reaction
的列表。您可以使用{react.emoji: react.count for react in message.reactions}
您可以在发布消息后立即对其作出反应:
@bot.command() async def poll(ctx, *, text): message = await ctx.send(text) for emoji in ('👍', '👎'): await message.add_reaction(emoji)
- 您可以使用
Message.pin
:await message.pin()
我不确定您所说的"user
tags
"是什么意思。你是说角色吗?
我会将您的命令写为
@bot.command()
async def create_poll(ctx, text, *emojis: discord.Emoji):
msg = await ctx.send(text)
for emoji in emojis:
await msg.add_reaction(emoji)
请注意,这将仅适用于您已添加到您自己的服务器的自定义emoji(这是因为discord.py
对Unicode emoji和自定义emoji的处理方式不同。)这将接受像这样的命令
!create_poll "Vote in the Primary!" :obamaemoji: :hillaryemoji:
假设这两个表情符号位于您发送命令的服务器上。
使用新的commands.Greedy
转换器,我将重写上述命令,如下所示:
@bot.command()
async def create_poll(ctx, emojis: Greedy[Emoji], *, text):
msg = await ctx.send(text)
for emoji in emojis:
await msg.add_reaction(emoji)
所以调用会更自然一些,没有引号:
!create_poll :obamaemoji: :hillaryemoji: Vote in the Primary!
这篇关于不和谐的机器人阅读反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文