What's the best way to tally and show emoji reactions in a Discord bot poll?

for minute in range(1):
    poll_msg = await channel.fetch_message(msg.id)
    positive_votes = 0
    negative_votes = 0
    for react in poll_msg.reactions:
        if react.emoji == '✅':
            positive_votes = react.count
        elif react.emoji == '❌':
            negative_votes = react.count
    updated_content = f"""**QUICK POLL**

Yes: ✅ {positive_votes}

No: ❌ {negative_votes}"""
    await msg.edit(content=updated_content)
    await asyncio.sleep(5)

I made a poll bot for Discord. It’s supposed to count yes and no votes using emojis. The bot should update the message with new vote counts. But it’s not working right. The counts always show zero. Can someone help me figure out what’s wrong? I think there might be a problem with how I’m counting the reactions. Thanks!

The issue you’re facing is likely due to rate limiting and caching. Discord’s API doesn’t always return real-time data, especially for reaction counts. To improve your poll bot, consider implementing a longer delay between updates and using the force parameter when fetching the message.

Here’s a modified approach:

import asyncio

async def update_poll(channel, msg_id, duration):
    end_time = asyncio.get_event_loop().time() + duration
    while asyncio.get_event_loop().time() < end_time:
        poll_msg = await channel.fetch_message(msg_id, force=True)
        pos_votes = sum(r.count for r in poll_msg.reactions if str(r.emoji) == '✅')
        neg_votes = sum(r.count for r in poll_msg.reactions if str(r.emoji) == '❌')
        
        await poll_msg.edit(content=f'**QUICK POLL**\n\nYes: ✅ {pos_votes}\n\nNo: ❌ {neg_votes}')
        await asyncio.sleep(30)  # Update every 30 seconds

    await poll_msg.edit(content=f'{poll_msg.content}\n\nPoll Ended!')

This should provide more accurate results. Remember to handle exceptions and consider adding a way to end the poll early if needed.

hey mate, ur code looks good but the issue might be with timing. try adding a longer delay between updates, like 30 secs or so. also, use force=True when fetching the message to bypass cache. that should help with getting accurate reaction counts. good luck!

I’ve encountered similar issues when working with Discord bot polls. The problem likely stems from the fact that you’re fetching the message immediately after sending it, before users have had a chance to react. To fix this, you’ll want to implement a loop that continuously checks for reactions over a longer period.

Here’s a modified version that should work better:

import asyncio

async def run_poll(channel, duration_minutes):
    poll_msg = await channel.send("**QUICK POLL**\n\nYes: ✅ 0\n\nNo: ❌ 0")
    
    end_time = asyncio.get_event_loop().time() + (duration_minutes * 60)
    
    while asyncio.get_event_loop().time() < end_time:
        await asyncio.sleep(5)  # Check every 5 seconds
        
        poll_msg = await channel.fetch_message(poll_msg.id)
        positive_votes = next((react.count for react in poll_msg.reactions if str(react.emoji) == '✅'), 0)
        negative_votes = next((react.count for react in poll_msg.reactions if str(react.emoji) == '❌'), 0)
        
        updated_content = f"**QUICK POLL**\n\nYes: ✅ {positive_votes}\n\nNo: ❌ {negative_votes}"
        await poll_msg.edit(content=updated_content)

    await poll_msg.edit(content=f"{updated_content}\n\nPoll ended!")

This version runs for a specified duration and updates more frequently. It should accurately reflect the reaction counts as they come in.