Added majority of voice logic

- Play / Pause / Stop
- Queueing still needs some work
This commit is contained in:
Joshua Arulsamy 2020-07-24 00:41:59 -06:00
parent f58a16ded0
commit 3aff8261bf

View File

@ -1,4 +1,7 @@
from queue import Queue
import discord import discord
from discord import FFmpegPCMAudio
from discord.ext import commands from discord.ext import commands
from discord.ext.commands import command from discord.ext.commands import command
from fuzzywuzzy import fuzz from fuzzywuzzy import fuzz
@ -10,7 +13,7 @@ class General(commands.Cog):
self.bot = bot self.bot = bot
@command() @command()
async def stop(self, ctx): async def kill(self, ctx):
await ctx.send(f"Stopping upon the request of {ctx.author.mention}") await ctx.send(f"Stopping upon the request of {ctx.author.mention}")
await self.bot.close() await self.bot.close()
@ -25,30 +28,81 @@ class Plex(commands.Cog):
self.pms = PlexServer(self.base_url, self.plex_token) self.pms = PlexServer(self.base_url, self.plex_token)
self.music = self.pms.library.section(self.library_name) self.music = self.pms.library.section(self.library_name)
self.vc = None
self.play_queue = Queue()
# self.callback_ctx = None
def _search_tracks(self, title): def _search_tracks(self, title):
tracks = self.music.searchTracks() tracks = self.music.searchTracks()
score = [[None], -1] score = [None, -1]
for i in tracks: for i in tracks:
# scores[i].append(fuzz.ratio(title.lower(), i.lower))
s = fuzz.ratio(title.lower(), i.title.lower()) s = fuzz.ratio(title.lower(), i.title.lower())
if s > score[1]: if s > score[1]:
score[0] = [i] score[0] = i
score[1] = s score[1] = s
elif s == score[1]: elif s == score[1]:
score[0].append(i) score[0] = i
return score return score[0]
@command() @command()
async def hello(self, ctx, *, member: discord.member = None): async def hello(self, ctx, *, member: discord.member = None):
member = member or ctx.author member = member or ctx.author
await ctx.send(f"Hello {member}") await ctx.send(f"Hello {member}")
async def _after_callback(self, error=None):
track = self.play_queue.get()
audio_stream = FFmpegPCMAudio(track.getStreamURL())
self.vc.play(audio_stream)
await self.callback_ctx.send(f"Playing {track.title}")
@command() @command()
async def play(self, ctx, *args): async def play(self, ctx, *args):
title = " ".join(args) title = " ".join(args)
track = self._search_tracks(title) track = self._search_tracks(title)
if track[0][0]: if track:
await ctx.send(track[0][0].title) track_url = track.getStreamURL()
if not ctx.author.voice:
await ctx.send("Join a voice channel first!")
return
if not self.vc:
self.vc = await ctx.author.voice.channel.connect()
if self.vc.is_playing():
self.play_queue.put(track)
self.callback_ctx = ctx
await ctx.send(f"Added {track.title} to queue.")
else:
audio_stream = FFmpegPCMAudio(track_url)
self.vc.play(audio_stream, after=self._after_callback)
await ctx.send(f"Playing {track.title}")
else: else:
await ctx.send("Song not found!") await ctx.send("Song not found!")
@command()
async def stop(self, ctx):
if self.vc:
self.vc.stop()
await self.vc.disconnect()
self.vc = None
await ctx.send("Stopped")
@command()
async def pause(self, ctx):
if self.vc:
await self.vc.pause()
await ctx.send("Paused")
@command()
async def resume(self, ctx):
if self.vc:
await self.vc.resume()
await ctx.send("Resumed")
@command()
async def skip(self, ctx):
if self.vc:
await self.vc.stop()
if not self.play_queue.empty():
await self._after_callback()