Bot Radio Looping Audio discord py discord bot

时间:2021-03-18 06:55:29

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

我想一遍又一遍地播放文件,但出现错误。这是一个错误:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'is_playing'

这是我的机器人不和谐代码:

@client.command()
async def p(ctx):
   await ctx.channel.purge(limit=1)
   channel = ctx.author.voice.channel
   voice = discord.utils.get(client.voice_clients, guild=ctx.guild)

   def repeat(guild, voice, audio):
       voice.play(audio, after=lambda e: repeat(guild, voice, audio))
       voice.is_playing()

   if channel and not voice.is_playing():
       audio = discord.FFmpegPCMAudio('audio.mp3')
       voice.play(audio, after=lambda e: repeat(ctx.guild, voice, audio))
       voice.is_playing() 

有什么问题?

1 个答案:

答案 0 :(得分:0)

试试这个:

@client.command()
async def p(ctx):
   await ctx.channel.purge(limit=1)
   voice = ctx.voice_client

   if not voice:
       return await ctx.send("You are not in a voice channel")
   
   await voice.connect()

   def repeat(voice, audio):
       voice.play(audio, after=lambda e: repeat(voice, audio))
       voice.is_playing()

   if voice and not voice.is_playing():
       audio = discord.FFmpegPCMAudio('audio.mp3')
       voice.play(audio, after=lambda e: repeat(voice, audio))
       voice.is_playing()

查看您的评论后,您可以通过发出rejoin命令让机器人离开并加入语音频道,这将离开语音频道并再次加入:

@client.command()
async def rejoin(ctx):
    voice = ctx.voice_client
    if not voice:
        return await ctx.send("You're not connected to a voice channel")
    if voice.is_connected():
        await voice.disconnect()
        await voice.connect()
    else:
        await voice.connect()