我正在用discord.py制作一个不和谐的机器人,我已经差不多完成了。我正在努力使它,以便您可以只放在一个视频的标题,它会搜索它。所以我尝试使用yt-search库,因为它看起来很容易使用。但是当我尝试运行这个机器人时,我得到了this error,请告诉我下一步我应该做什么。
下面是我的代码:
import discord
from discord.ext import commands
import youtube_dl
import yt_search
import os
client = commands.Bot(command_prefix="?")
@client.event
async def on_ready():
print("Bot is ready.")
game = discord.Game("Music")
await client.change_presence(activity=game)
@client.command()
async def play(ctx, url: str = None):
if url is None:
await ctx.send("Put YouTube video's link after the 'play' command.")
return
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="music lounge")
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"
}]
}
try:
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
await ctx.send("Downloading music...")
ydl.download([url])
except:
yt = yt_search.build("API Key")
search_result = yt.search(url, sMax=10, sType=["video"])
print(search_result.title)
print(search_result.videoId)
print(search_result.channelTitle)
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("The bot is not connected to a voice channel.")
@ 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("Currently no audio is playing.")
else:
await ctx.send("The bot is not connected to a voice 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("The audio is not paused.")
else:
await ctx.send("The bot is not connected to a voice 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("Currently no audio is playing.")
else:
await ctx.send("The bot is not connected to a voice channel.")
client.run('TOKEN')发布于 2021-05-09 20:25:10
你可以更容易地搜索Youtube视频,例如,使用以下简短代码:
with youtube_dl.YoutubeDL(ytdl_format_options) as ydl:
urlprev = ydl.extract_info(f"ytsearch:url", download=True)https://stackoverflow.com/questions/67457233
复制相似问题