問題描述
如何計算表情符號並在不和諧的機器人反應中顯示它們? (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. Gee、Brad Figueroa)