每次我第一次在聊天中输入“!music song”时,它都会显示AttributeError: 'NoneType' object has no attribute 'play'
。但是如果我再次输入 "!music song" ,机器人正在播放音乐。我怎样才能解决这个问题?已经谢谢了。
我的代码在这里:
@client.command()
async def music(ctx,song : str):
channel = ctx.author.voice.channel
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if voice is None or not voice.is_connected():
await channel.connect()
song_there = os.path.isfile("Music/{}.mp3".format(song))
if song_there == True:
voice.play(discord.FFmpegPCMAudio(('Music/{}.mp3'.format(song))))
await ctx.send('{} playing! <:white_check_mark:853651360666877972>'.format(song))
else:
await ctx.send("This file does not exists")```
答案 0 :(得分:0)
尝试在 voice
语句中的 await channel.connect()
之后再次初始化 if
:
if voice is None or not voice.is_connected():
await channel.connect()
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
虽然,您可能应该在函数调用之前运行 channel.connect()
,这样可以避免重新尝试初始化 voice
。