繁体   English   中英

如何在反应中添加投票数?

[英]How do I add a vote count to reaction?

嗨,我正在研究音乐齿轮,正在弄清楚如何进行简单的跳过投票。

我要达到的目标是, if control == 'skip': 4个成员做出跳跃反应if control == 'skip': ⏩将跳过这首歌。

这是我正在与您合作的内容,可让您更好地了解我在这里尝试做的事情。

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

我正在查看代码的这一部分, if control == 'skip':在响应时跳过歌曲播放,或者如果没有排队的歌曲则停止播放歌曲。

vc.stop()指示播放器停止播放,如果另一首歌曲排队,它将播放下一首歌曲,否则,如果没有更多歌曲排队,则停止播放。

我已经尝试过此功能,但对我来说不起作用。

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

如果有人可以告诉我我哪里出问题了,我将不胜感激。 一个例子或一些意见将是巨大的。

您对总票数的检查是检查错误的变量并返回一个常数:

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

ifcontrol中定义了'skip' if它在其中定义(因为否则control == 'skip'不会被视为true)。 因此, len(control)为4(字符串'skip'中的4个字符)。 要检查该类型的实际反应数,需要检查以下行中返回的反应对象

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

react是反应对象,API表示它保存了已发送反应次数的属性count 使用此方法,将total_votes = len(control)行替换为total_votes = react.count以获取发送反应的实际次数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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