简体   繁体   中英

How do I add a vote count to reaction?

Hi I'm working on a music cog and I'm figuring out how to do a simple skip vote.

What I'm trying to achieve is that when 4 members react with the skip reaction if control == 'skip': ⏩ it will skip the song.

here is what I'm working with to give you a better look on what I'm trying to do here.

async def buttons_controller(self, guild, current, source, channel, context):
    vc = guild.voice_client
    vctwo = context.voice_client

    for react in self.buttons:
        await current.add_reaction(str(react))

    def check(r, u):
        if not current:
            return False
        elif str(r) not in self.buttons.keys():
            return False
        elif u.id == self.bot.user.id or r.message.id != current.id:
            return False
        elif u not in vc.channel.members:
            return False
        elif u.bot:
            return False
        return True

    while current:
        if vc is None:
            return False

        react, user = await self.bot.wait_for('reaction_add', check=check)
        control = self.buttons.get(str(react))

        if control == 'rp':
            if vc.is_paused():
                vc.resume()
            else:
                vc.pause()

        if control == 'skip':
            total_votes = len(control)
            if total_votes >= 3:
                vc.stop()
                await channel.send('Skip vote passed, skipping song...')

        if control == 'stop':
            mods = get(guild.roles, name="Mods")
            for member in list(guild.members):
                if mods in member.roles:
                    await context.invoke(self.bot.get_command("stop"))
                    return
            else:
                await channel.send('Only a mod can stop and clear the queue. Try skipping the song instead.', delete_after=5)

        if control == 'vol_up':
            player = self._cog.get_player(context)
            vctwo.source.volume += 2.5

        if control == 'vol_down':
            player = self._cog.get_player(context)
            vctwo.source.volume -= 2.5

        if control == 'thumbnail':
            await channel.send(embed=discord.Embed(color=0x17FD6E).set_image(url=source.thumbnail).set_footer(text=f"Requested By: {source.requester} | Video Thumbnail: {source.title}", icon_url=source.requester.avatar_url), delete_after=10)

        if control == 'tutorial':
            await channel.send(embed=discord.Embed(color=0x17FD6E).add_field(name="How to use the music controller?", value="⏯ - Pause\n⏭ - Skip\n➕ - Increase Volume\n➖ - Increase Volume\n🖼 - Get Thumbnail\n⏹ - Stop & Leave\nℹ - Queue\n❔ - Display help for music controls"), delete_after=10)

        if control == 'queue':
            await self._cog.queue_info(context)

        try:
            await current.remove_reaction(react, user)
        except discord.HTTPException:
            pass

I'm looking at this part of the code if control == 'skip': which on reaction skips the song playing or if no song is queued stops to song playing.

vc.stop() this instructs the player to stop and if another song is queued it will play the next song, otherwise stops if no more songs are queued.

I've tried this function but this is not working for me.

if control == 'skip':
        total_votes = len(control)
        if total_votes >= 3:
            vc.stop()
            await channel.send('Skip vote passed, skipping song...')

If anyone can tell me where I'm going wrong here I'd appreciated it. An example or some input on this would be greatful.

Your check for the total votes is checking the wrong variable and returning a constant value:

if control == 'skip':
    total_votes = len(control)
    if total_votes >= 3:
        vc.stop()
        await channel.send('Skip vote passed, skipping song...')

control is defined inside this if as being 'skip' (because otherwise control == 'skip' wouldn't evaluate as true). Therefore, the len(control) is 4 (4 characters in the string 'skip' ). To check the actual number of reactions of that type, you need to check the reaction object returned in the following line:

react, user = await self.bot.wait_for('reaction_add', check=check)

react is the reaction object, and the API states it holds the attribute count for the amount of times the reaction has been sent. Using this, replace the line total_votes = len(control) with total_votes = react.count to get the actual number of times the reaction has been sent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM