如何計算表情符號並在不和諧的機器人反應中顯示它們? (How to count the emojis and display them in a discord bot reaction?)


問題描述

如何計算表情符號並在不和諧的機器人反應中顯示它們? (How to count the emojis and display them in a discord bot reaction?)

for second in range(60):
    message_id = await message.channel.fetch_message(message.id)
    for reaction in message_id.reactions:
        thumbs_up_count = 0
        thumbs_down_count = 0
        if str(reaction.emoji) == '👍':
            thumbs_up_count = reaction.emoji.count
        if str(reaction.emoji) == '👎':
            thumbs_down_count = reaction.emoji.count
        await message.edit(content=("**‑‑‑‑POLL‑‑‑‑** \n \n**Green Today?** :thumbsup: {} \n \n**Red Today?**   :thumbsdown: {}".format(thumbs_up_count,thumbs_down_count)))
        await asyncio.sleep(3)

這是我的代碼,它進行民意調查,人們的反應是豎起大拇指或豎起大拇指。然後它會在投票時不斷編輯消息。代碼效率不高,但我沒有看到邏輯錯誤在哪裡。它根本不計算表情符號並顯示 0。


參考解法

方法 1:

It's because you're always replacing the thumbs_up_count, thumbs_down_count values with what reaction.emoji.count brings, so what you can try is some like this:

for second in range(60):
    message_id = await message.channel.fetch_message(message.id)
    for reaction in message_id.reactions:
        thumbs_up_count = 0
        thumbs_down_count = 0
        if str(reaction.emoji) == '👍':
            thumbs_up_count += reaction.emoji.count
        if str(reaction.emoji) == '👎':
            thumbs_down_count += reaction.emoji.count
        await message.edit(content=("**‑‑‑‑POLL‑‑‑‑** \n \n**Green Today?** :thumbsup: {} \n \n**Red Today?**   :thumbsdown: {}".format(thumbs_up_count,thumbs_down_count)))
        await asyncio.sleep(3)

Note that I replaced = by += in both cases

Hope it could help you :)

(by J.C. GeeBrad Figueroa)

參考文件

  1. How to count the emojis and display them in a discord bot reaction? (CC BY‑SA 2.5/3.0/4.0)

#discord #Python






相關問題

如何讓不和諧的機器人提及用戶? (How to make a discord bot mention a user?)

如何計算表情符號並在不和諧的機器人反應中顯示它們? (How to count the emojis and display them in a discord bot reaction?)

每當機器人啟動時,它就會開始向控制台發送垃圾郵件,沒有明顯的問題 (Whenever the bot starts up, it just starts spamming the console with no visible issue)

Discord.js ReferenceError:嵌入未定義 (Discord.js ReferenceError: embed is not defined)

在 Discord.js 中查找所有具有角色的成員 (Finding all members with a role in Discord.js)

啟動兩個 CPU 阻塞偵聽器並等待其中一個完成 (Starting two cpu blocking listeners and wait until one of them finishes)

discord.py - 猜謎遊戲無響應(更新) (discord.py - Guessing game no response (updated))

\n 換行符不保留代碼塊文本樣式 (\n Line break does not preserve code block text style)

如何在對不同消息做出反應時讓不和諧機器人刪除以前的角色? (How to make discord bot remove the previous role when reacting to a different message?)

一段代碼阻礙了我在學校的 Discord 機器人 (One piece of code hindering my Discord bot for School)

如何使不和諧機器人隨機跳過響應 (How to make discord bot randomly skip a response)

我應該放什麼行,這樣當我寫東西時,Discord 機器人會以我想要的方式響應(discord.py)? (What lines should I put so that when I write something the Discord bot responds in a way that I want (discord.py)?)







留言討論