From 3aff8261bf0465a71b1fa755cf0857258f225ae2 Mon Sep 17 00:00:00 2001 From: Joshua Arulsamy Date: Fri, 24 Jul 2020 00:41:59 -0600 Subject: [PATCH] :sparkles: Added majority of voice logic - Play / Pause / Stop - Queueing still needs some work --- PlexBot/bot.py | 70 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 8 deletions(-) diff --git a/PlexBot/bot.py b/PlexBot/bot.py index 7b4a789..8484647 100644 --- a/PlexBot/bot.py +++ b/PlexBot/bot.py @@ -1,4 +1,7 @@ +from queue import Queue + import discord +from discord import FFmpegPCMAudio from discord.ext import commands from discord.ext.commands import command from fuzzywuzzy import fuzz @@ -10,7 +13,7 @@ class General(commands.Cog): self.bot = bot @command() - async def stop(self, ctx): + async def kill(self, ctx): await ctx.send(f"Stopping upon the request of {ctx.author.mention}") await self.bot.close() @@ -25,30 +28,81 @@ class Plex(commands.Cog): self.pms = PlexServer(self.base_url, self.plex_token) 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): tracks = self.music.searchTracks() - score = [[None], -1] + score = [None, -1] for i in tracks: - # scores[i].append(fuzz.ratio(title.lower(), i.lower)) s = fuzz.ratio(title.lower(), i.title.lower()) if s > score[1]: - score[0] = [i] + score[0] = i score[1] = s elif s == score[1]: - score[0].append(i) + score[0] = i - return score + return score[0] @command() async def hello(self, ctx, *, member: discord.member = None): member = member or ctx.author 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() async def play(self, ctx, *args): title = " ".join(args) track = self._search_tracks(title) - if track[0][0]: - await ctx.send(track[0][0].title) + if track: + 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: 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()