【问题标题】:How to make a discord bot leave the voice channel after being inactive for x minutes?如何让不和谐机器人在不活动 x 分钟后离开语音频道?
【发布时间】:2020-12-18 20:26:52
【问题描述】:

我正在尝试使用 discord.py 在 discord 上制作音乐机器人。但是,我希望机器人在 5 分钟等一段时间内保持语音通道处于非活动状态。不活动是指机器人没有播放任何歌曲。

用于启动歌曲的命令是

voice.play(discord.FFmpegPCMAudio(audio))

voice.stop() 停止歌曲

voice objectdiscord.VoiceClient

【问题讨论】:

  • 你如何播放音乐?也许您可以使用它来启动time.sleep(...5 minutes..) 并离开频道的功能。或者,也许您将不得不使用thread 运行带有循环的代码,该循环将当前时间与音乐停止时的时间进行比较,然后断开连接或睡眠 1 秒以再次检查当前时间与音乐停止时的时间
  • 获得帮助的唯一方法是指定如何开始和停止音乐。在您编辑问题以更加专注之前,没有人会回答您的问题。

标签: python python-3.x discord.py discord.py-rewrite


【解决方案1】:

您可以使用is_playing() 方法检测您是否正在播放音频。因此,如果我要这样做,我会使用 asyncio 模块做类似的事情。

while True:
    await asyncio.sleep(5)

    if voice.is_playing() == False:
        await voice.disconnect()
        break

【讨论】:

  • 代码确实有效。但是,如果用户在 5 秒后开始播放歌曲,机器人仍然会断开连接。
  • 确实如此,但您可以让此代码仅在播放器启动后执行。
【解决方案2】:

我认为这应该可行。如果您共享代码,我可以检查这是否符合您的需求,但您可以尝试一下。

while voice.is_playing(): #Checks if voice is playing
    await asyncio.sleep(1) #While it's playing it sleeps for 1 second
else:
    await asyncio.sleep(15) #If it's not playing it waits 15 seconds
    while voice.is_playing(): #and checks once again if the bot is not playing
        break #if it's playing it breaks
    else:
        await voice.disconnect() #if not it disconnects

【讨论】:

    【解决方案3】:

    嘿,我知道这个帖子已经快一年了,但我认为我有一个代码值得发布给任何仍然有这个问题的人。

    @commands.Cog.listener()
    async def on_voice_state_update(self, member, before, after):
        
        if not member.id == self.bot.user.id:
            return
    
        elif before.channel is None:
            voice = after.channel.guild.voice_client
            time = 0
            while True:
                await asyncio.sleep(1)
                time = time + 1
                if voice.is_playing() and not voice.is_paused():
                    time = 0
                if time == 600:
                    await voice.disconnect()
                if not voice.is_connected():
                    break
    

    首先,我发现最好将它放在 on_voice_state_update 事件中,因为将它放在播放命令的 after 语句中会导致它在每次歌曲结束时运行该循环。

    (如果您了解此代码的工作原理,则无需阅读其余部分,我将对其进行解释。)

    好的,首先我们检查我们的机器人是否是触发事件的机器人:

    if not member.id == self.bot.user.id:
        return
    

    之后我们检查它是否是一个加入事件:

    elif before.channel is None:
    

    如果之前的频道没有,这意味着机器人不在语音频道中,那么它加入了我们正在寻找的频道。

    如果它首先进入 elif 语句,它会声明变量 voice。我发现获得它的最快方法是使用像 voice = after.channel.guild.voice_client 这样的 after 语句,它应该可以让您访问公会语音客户端!

    然后我们进入代码的主要部分!这是机器人将运行以检查机器人是否正在播放的循环:

        time = 0
        while True:
            await asyncio.sleep(1)
            time = time + 1
            if player.is_playing or player.is_paused:
                time = 0
            if time == 600:
                await voice.disconnect()
            if not player.is_connected:
                break
    

    所以我们创建了一个名为 time 的变量,它每秒会加 1,它检测到机器人没有播放并确认机器人实际上没有暂停......然后如果机器人再次开始播放,它会返回零。但如果它达到 600,即 10 分钟,它会断开机器人与频道的连接,然后打破循环。

    哦,不用担心@commands.Cog.listener() 这只是因为我在齿轮中使用它。如果不是,则应将其更改为 @client.event 并从函数语句中删除 self。

    对不起,我在教程模式下已经满了,我只是想向人们解释我的代码,哈哈。

    【讨论】:

      猜你喜欢
      • 2020-10-15
      • 1970-01-01
      • 2019-06-24
      • 1970-01-01
      • 2021-04-24
      • 2019-04-08
      • 2023-04-05
      • 2020-10-24
      • 2019-12-08
      相关资源
      最近更新 更多