【问题标题】:Discord bot reading reactionsDiscord 机器人阅读反应
【发布时间】:2019-01-15 04:17:32
【问题描述】:

我需要实现一些功能,其中一个功能是实现民意调查类型功能。由于某些政策,不能使用公共不和谐机器人,所以我们必须自己实施一些东西。昨天做了一些研究,能够使用 python3 和来自discord.extcommands api 制作基本机器人。现在我需要弄清楚的是:

  1. 阅读用户对消息添加的反应?
  2. 创建带有反应的消息(例如创建反应民意调查的机器人?)
  3. 置顶消息?
  4. 我相信从ctx 我可以得到user tags(管理员等)。有更好的方法吗?

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}\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

【问题讨论】:

  • usertags”是什么意思?

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


【解决方案1】:

我刚刚看到你被卡住了,我也被卡住了。解决方案是,反应实际上并不是“:one:”之类的东西,而是实际的表情符号,有不同的方法可以解决这个问题,最简单的方法是自己将它们添加到字典中。或者您可以使用python3 -m pip install discordhelp 并使用它的功能listed here

【讨论】:

  • 请注意,因为我是 discordhelp 库的创建者,请随时问我,也请告诉我它是否有效,因为我无法让它工作我的电脑。
【解决方案2】:

顺便说一句,鉴于您正在启动这个项目,并且已经在使用重写文档,请确保您使用的是重写版本。这里有一些关于如何确保的问题,以及如果你不是如何获得它,但它有更好的文档并且更易于使用。我在下面的回答假设您使用的是

  1. Message.reactionsReactions 的列表。您可以使用

    将反应映射到他们的计数
    {react.emoji: react.count for react in message.reactions}
    
  2. 您可以在发布消息后立即对其做出反应:

    @bot.command()
    async def poll(ctx, *, text):
        message = await ctx.send(text)
        for emoji in ('?', '?'):
            await message.add_reaction(emoji)
    
  3. 你可以使用Message.pin:await message.pin()

我不确定您所说的“usertags”是什么意思。你是指角色吗?

我会把你的命令写成

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

请注意,这仅适用于自定义表情符号,即您添加到自己服务器的表情符号(这是因为 discord.py 对待 unicode 表情符号和自定义表情符号的方式不同。)这将接受类似的命令

!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!

【讨论】:

  • 谢谢。你能告诉我如何添加对消息的反应吗?我已经更新了我的代码,当我尝试使用表情符号添加反应时,我收到错误消息,上面写着 Unknown Emoji
  • @EmAe 是的,这是(在我看来)discord.py 表示表情符号的最大失败之一。自定义 emoji 由 discord.Emoji 对象表示,但 unicode emoji 由其对应的 unicode 字符表示。因此,您最终会得到一个奇怪的灰色区域,在该区域中,Discord 客户端中的用户无法区分的表情符号必须由您的机器人以不同方式处理。如果您想在源代码中表示 unicode 表情符号,请使用 unicode 字符 '①' 或转义序列 '\u2460',而不是短代码。
猜你喜欢
  • 2021-04-23
  • 2021-10-04
  • 1970-01-01
  • 2022-10-30
  • 2020-04-16
  • 2020-08-12
  • 1970-01-01
  • 2019-08-10
  • 2020-03-13
相关资源
最近更新 更多