【发布时间】:2021-07-24 01:35:33
【问题描述】:
我正在制作一个音乐机器人是 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)
【问题讨论】:
-
NoneType= 某些东西不存在/没有返回某些东西。 (Contribution about it)
标签: python discord discord.py