【问题标题】:Is there anyway to count specific emojis under the message? (Python)无论如何要计算消息下的特定表情符号吗? (Python)
【发布时间】:2021-10-06 07:13:31
【问题描述】:

我正在为我的 discord 机器人创建一个投票命令。我使用表情符号为投票选项投票。

我希望我的机器人在一段时间后显示投票结果(我还没有实现超时)。但要做到这一点,我需要计算每个表情符号。有办法吗?

到目前为止我的代码:

@bot.command(name='poll')
async def poll(ctx, question, option1 = None, option2 = None):
    if option1 == None and option2 == None:
        embed = discord.Embed(
            title="Poll",
            description=f"{question}",
            color = discord.Color.blue()
        )
        msg = await ctx.channel.send(embed=embed)
        message = await ctx.channel.fetch_message(msg.id)
        await message.add_reaction("????")
        await message.add_reaction("????")
    embed = discord.Embed(
        title="Poll",
        description=f"{question}",
        color=discord.Color.blue()
    )
    embed.add_field(name=f"{option1}", inline=False)
    embed.add_field(name=f"{option2}", inline=False)
    msg = await ctx.channel.send(embed=embed)
    message = await ctx.channel.fetch_message(msg.id)
    await message.add_reaction("????")
    await message.add_reaction("????")

【问题讨论】:

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


    【解决方案1】:

    表情符号只是字符,就像其他任何东西一样:

    >>> st="The quick brown fox? jumped over the lazy dog?"
    >>> st.count("?")
    2
    

    如果你没有花哨的表情符号键盘(ord()chr() 的倒数):

    >>> ord("?")
    128077
    >>> ord("?")
    128078
    

    更新 - 尝试:

    for reaction in message.reactions:
      if reaction.emoji == "?":
        counts["?"] += reaction.count
    

    基于这些:

    【讨论】:

    • 也许我的观点错了,我需要计算反应的数量(出现在消息下方的那些表情符号)。我不知道如何访问它们
    • message.reactions 是对消息的反应,而不是它包含的表情符号。
    • 除非我读错了,否则文档不同意:message.reactions 应该返回Reaction 对象的列表,每个对象都包含.emoji.count 属性。 -> reactions Reactions to a message. Reactions can be either custom emoji or standard unicode emoji. Type List[Reaction]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-06
    相关资源
    最近更新 更多