Basically I made a bot that just simply plays music, I made a command that plays the song so you do ?play [url of song] and it will play, now I'm working on a ?search command that you can enter the keyword and it will play the song, after alot of errors I got it to find the songs when you typed them. But it doesn't stream the sound over the audio (but it does though when you use the ?play command) This is my code:
import discord
from discord.ext import commands
import youtube_dl
import requests
class music(commands.Cog):
def __init__(self, client):
self.client = client
@commands.command()
async def join(self,ctx):
if ctx.author.voice is None:
await ctx.send("Join a voice channel dumb fuck")
voice_channel = ctx.author.voice.channel
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
@commands.command()
async def disconnect(self,ctx):
await ctx.voice_client.disconnect()
@commands.command()
async def play(self,ctx,url):
ctx.voice_client.stop()
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
YDL_OPTIONS = {'format':"bestaudio"}
vc = ctx.voice_client
with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(url, download=False)
url2 = info['formats'][0]['url']
source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
vc.play(source)
@commands.command()
async def search(self, ctx, arg):
ctx.voice_client.stop()
try:
requests.get("" + str(arg))
except: arg = " " + str(arg)
else: arg = "" + str(arg)
YDL_OPTIONS = {'format':"bestaudio"}
vc = ctx.voice_client
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(f"ytsearch:{arg}", download=False)['entries'][0]
url2 = info['formats'][0]['url']
return {'source': info['formats'][0]['url'], 'title': info['title']}
source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
vc.play(source)
@commands.command()
async def pause(self,ctx):
await ctx.voice_client.pause()
await ctx.send("PAUSED")
@commands.command()
async def resume(self,ctx):
await ctx.voice_client.resume()
await ctx.send("RESUMED")
def setup(client):
client.add_cog(music(client))
The console doesn't give any errors it just says this:
[download] Downloading playlist: superidol
[youtube:search] query " superidol": Downloading page 1
[youtube:search] playlist superidol: Downloading 1 videos
[download] Downloading video 1 of 1
[youtube] chY9p-XLHHk: Downloading webpage
[download] Finished downloading playlist: superidol