【问题标题】:(Discord.py) How to check whether bot is already playing audio in a voice channel?(Discord.py) 如何检查机器人是否已经在语音通道中播放音频?
【发布时间】:2021-11-27 00:34:27
【问题描述】:

使用 Discord.py 库创建了一个 Discord 机器人。 它使用 !sound 根据给定命令播放声音 但是当它忙于播放一个声音,并且调用了一个新的声音时,我希望它停止,并播放下一个声音。但我不知道检查机器人是否已经在播放声音的功能? 有人给点建议吗?

这是我播放声音的代码:

@client.command(pass_context= True)
async def sound(ctx, message):
  serverid = ctx.message.guild.id
  newname = str(serverid) + "_" + message + ".mp3" 
  print(newname)
  print(serverid)
  mp3 = message+'.mp3' #take message (soundname) and add .mp3
  mp3 = 'sounds/'+mp3  #Add /sounds folder
  print(mp3)
  if(path.exists(mp3)):#check if file exists
    source = FFmpegPCMAudio(mp3) #set FFMPEG source
    if(ctx.author.voice is None): #check if author is in voice channel
      await ctx.send("Please join a voice channel first!")
      print("Author not in voicechat") 
    else:               #otherwist, get authors voice channel UID
      print("Author in voicechat") 
      channel = ctx.author.voice.channel
      if(ctx.voice_client is None): #check if bot is already in voicechannel
        print("Bot is not connected, connecting...")
        voice = await channel.connect()   #if not -> Connect
        voice.play(source)                #and Play
      else:                               #if already connected
        voice = ctx.voice_client          #get UID of voice channel
        print("Bot is connected to voicechat")
        voice.play(source)                #play without Reconnecting
  else:
    await ctx.send("Uh Oh, That chip does not exist")#if file doesn't exist
  print("-----------------------------------")

【问题讨论】:

  • "但是我不知道有一个函数可以检查机器人是否已经在播放声音?"为什么不只是记住机器人正在播放声音?你知道它什么时候开始播放声音(因为这是导致这种情况发生的代码!)并且你大概知道声音持续了多长时间。是吗?

标签: python audio discord


【解决方案1】:

只需运行ctx.voice_client.is_playing(),如果机器人当前正在播放音频,它将返回 True。

@client.command()
async def playingaudio(ctx):
    if ctx.voice_client.is_playing():
        await ctx.reply("Playing audio :)")
    else:
        await ctx.reply("Nothing is playing")

【讨论】:

    【解决方案2】:

    我认为你可以这样阻止它:

    try:
        voice_channel = ctx.message.guild.voice_client
        voice_channel.stop()
        #run code 
    except:
        #run code
    

    或检查音频是否正在播放:

    import asyncio
    
    async def duration():
        global is_playing
        is_playing = true
        await asyncio.sleep(duration)
        is_playing = false
    

    在开始播放音频后立即运行该函数,然后使用 is_playing 检查歌曲是否正在播放。 希望这行得通。

    【讨论】:

      【解决方案3】:

      我用pytube下载歌曲和播放,删除,我有这个想法

      for time in range(yt.length + 1):
      print(f"{time} >> {yt.length}")
      await sleep(1)
      if time == yt.length:
          print("Deletando o arquivo musical")
          os.remove(new_file)
      

      他是做什么的? 它计算视频有多少秒,每次播放歌曲时循环等待1秒,歌曲结束后,它会删除文件

      【讨论】:

        猜你喜欢
        • 2021-12-13
        • 2022-01-04
        • 2019-05-05
        • 2021-12-02
        • 2021-08-18
        • 2021-03-30
        • 2018-06-16
        • 2020-09-06
        • 2021-08-05
        相关资源
        最近更新 更多