Discord bot阅读反应

Em *_* Ae 7 python python-3.x discord discord.py

我需要实现一些功能,其中一个功能是实现轮询类型功能.由于某些政策,不能使用公共不和谐机器人,所以我们必须自己实施一些东西.昨天进行了一些研究,并且能够使用python3commandsapi 制作基本的机器人discord.ext.现在我需要弄清楚的是:

  1. 读取用户添加到消息中的反应?
  2. 创建一个带有反应的消息(比如创建反应民意调查的机器人?)
  3. 固定信息?
  4. 我相信ctx我可以得到user tags(管理员等).有没有更好的方法呢?

命令参考页面上找不到任何有用的东西,或者我正在查看错误的文档.任何帮助,将不胜感激.

谢谢


更新:谢谢你们.现在我被困在如何添加表情符号,这是我的代码

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}\t'.format(poll_emojis[count], poll_option)
                count += 1

        posted_message = await message.channel.send('**{0}**\n{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
Run Code Online (Sandbox Code Playgroud)

Pat*_*ugh 5

顺便说一句,假设您正在启动该项目,并且已经在使用重写文档,请确保您使用的是重写版本。这里有一些问题,涉及如何确定以及如何获得(如果没有的话),但是记录得更好并且更易于使用。我在下面的回答假设您正在使用

  1. Message.reactions是的列表Reaction。您可以使用以下命令将反应映射到其计数

    {react.emoji: react.count for react in message.reactions}
    
    Run Code Online (Sandbox Code Playgroud)
  2. 您可以在发布消息后立即对消息作出反应:

    @bot.command()
    async def poll(ctx, *, text):
        message = await ctx.send(text)
        for emoji in ('', ''):
            await message.add_reaction(emoji)
    
    Run Code Online (Sandbox Code Playgroud)
  3. 您可以使用Message.pinawait message.pin()

我不确定“ user tags”的意思。你是说角色吗?

编辑1:

我会把你的命令写成

@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)
Run Code Online (Sandbox Code Playgroud)

请注意,这仅适用于自定义表情符号,即您已添加到自己服务器中的discord.py表情符号(这是因为对Unicode表情符号和自定义表情符号的处理不同。)

!create_poll "Vote in the Primary!" :obamaemoji: :hillaryemoji:
Run Code Online (Sandbox Code Playgroud)

假设这两个表情符号在您发送命令的服务器上。

编辑2:

使用新的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)
Run Code Online (Sandbox Code Playgroud)

因此,如果没有引号,调用会更加自然:

!create_poll :obamaemoji: :hillaryemoji: Vote in the Primary!
Run Code Online (Sandbox Code Playgroud)