我正在制作一个音乐机器人 discord.py 并且我已经能够让机器人加入频道并下载 YouTube 音频,但是当它尝试播放音频时,this error occurs. 请帮助我这样做,因为我已经尝试通过谷歌搜索和堆栈溢出,但我找不到任何东西。当我尝试运行播放命令时发生错误。
import discord
from discord.ext import commands
import youtube_dl
import os
client = commands.Bot(command_prefix="?")
@client.event
async def on_ready():
print("Bot is ready.")
@client.command()
async def play(ctx, url: str):
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
except PermissionError:
await ctx.send("Wait for the currently playing music to end or use the 'stop' command.")
channel = discord.utils.get(ctx.guild.voice_channels, name="testing")
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if not voice is None:
if not voice.is_connected():
await channel.connect()
else:
await channel.connect()
ydl_opts = {
'format': "bestaudio",
'postprocessors': [{
'key': "FFmpegExtractAudio",
'preferredcodec': "mp3",
'preferredquality': "192"
}]
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
for file in os.listdir("./"):
if file.endswith(".mp3"):
os.rename(file, "song.mp3")
voice.play(discord.FFmpegPCMAudio("song.mp3"))
@client.command()
async def leave(ctx):
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if not voice is None:
if voice.is_connected():
await voice.disconnect()
else:
await ctx.send("There is no channel to leave.")
else:
await ctx.send("There is no channel to leave.")
@client.command()
async def pause(ctx):
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if not voice is None:
if voice.is_playing():
voice.pause()
else:
await ctx.send("No audio is playing.")
else:
await ctx.send("I am currently not in a channel.")
@client.command()
async def resume(ctx):
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if not voice is None:
if voice.is_paused():
voice.resume()
else:
await ctx.send("Audio is already playing.")
else:
await ctx.send("I am currently not in a channel.")
@client.command()
async def stop(ctx):
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if not voice is None:
if voice.is_playing:
voice.stop()
else:
await ctx.send("No audio is playing.")
else:
await ctx.send("I am currently not in a channel.")
client.run(Token)
答案 0 :(得分:-1)
问题是当机器人在执行命令之前没有连接到语音通道时,语音是 None
。您可以通过将 await channel.connect()
更新为 voice = await channel.connect()
来解决此问题:
@client.command()
async def play(ctx, url: str):
song_there = os.path.isfile("song.mp3")
try:
if song_there:
os.remove("song.mp3")
except PermissionError:
await ctx.send("Wait for the currently playing music to end or use the 'stop' command.")
channel = discord.utils.get(ctx.guild.voice_channels, name="testing")
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if not voice is None:
if not voice.is_connected():
voice = await channel.connect()
else:
voice = await channel.connect()
ydl_opts = {
'format': "bestaudio",
'postprocessors': [{
'key': "FFmpegExtractAudio",
'preferredcodec': "mp3",
'preferredquality': "192"
}]
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
for file in os.listdir("./"):
if file.endswith(".mp3"):
os.rename(file, "song.mp3")
voice.play(discord.FFmpegPCMAudio("song.mp3"))