🐛 Polished queue issues

Queue now actually works, async callback is properly awaited.
This commit is contained in:
Joshua Arulsamy 2020-08-04 03:59:48 -06:00
parent 7085349b19
commit 4c75d4c27f
2 changed files with 25 additions and 14 deletions

View File

@ -1,8 +1,9 @@
import yaml
from pathlib import Path
from typing import Dict
import logging import logging
import sys import sys
from pathlib import Path
from typing import Dict
import yaml
FORMAT = "%(asctime)s %(levelname)s: [%(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s" FORMAT = "%(asctime)s %(levelname)s: [%(filename)s:%(lineno)s - %(funcName)20s() ] %(message)s"

View File

@ -1,3 +1,4 @@
import logging
from queue import Queue from queue import Queue
import discord import discord
@ -5,10 +6,9 @@ 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
from plexapi.exceptions import Unauthorized
from plexapi.server import PlexServer from plexapi.server import PlexServer
import logging
logger = logging.getLogger("PlexBot") logger = logging.getLogger("PlexBot")
@ -30,7 +30,12 @@ class Plex(commands.Cog):
self.plex_token = plex_token self.plex_token = plex_token
self.library_name = lib_name self.library_name = lib_name
try:
self.pms = PlexServer(self.base_url, self.plex_token) self.pms = PlexServer(self.base_url, self.plex_token)
except Unauthorized:
logger.fatal("Invalid Plex token, stopping...")
raise Unauthorized("Invalid Plex token")
self.music = self.pms.library.section(self.library_name) self.music = self.pms.library.section(self.library_name)
self.vc = None self.vc = None
@ -39,8 +44,6 @@ class Plex(commands.Cog):
logger.info("Started bot successfully") logger.info("Started bot successfully")
# 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]
@ -60,17 +63,24 @@ class Plex(commands.Cog):
await ctx.send(f"Hello {member}") await ctx.send(f"Hello {member}")
async def _after_callback(self, error=None): async def _after_callback(self, error=None):
logger.debug("After callbacked")
if self.play_queue.empty(): if self.play_queue.empty():
self.current_track = None self.current_track = None
logger.debug("No tracks left in queue, returning")
else: else:
track = self.play_queue.get() track = self.play_queue.get()
audio_stream = FFmpegPCMAudio(track.getStreamURL()) audio_stream = FFmpegPCMAudio(track.getStreamURL())
self.vc.play(audio_stream)
self.current_track = track self.current_track = track
logger.debug(f"Started playing next song in queue: {track.title}")
self.vc.play(audio_stream)
await self.callback_ctx.send(f"Playing {track.title}") await self.callback_ctx.send(f"Playing {track.title}")
@command() @command()
async def play(self, ctx, *args): async def play(self, ctx, *args):
if not len(args):
await ctx.send(f"Usage: {BOT_PREFIX}play TITLE_OF_SONG")
return
title = " ".join(args) title = " ".join(args)
track = self._search_tracks(title) track = self._search_tracks(title)
if track: if track:
@ -80,7 +90,7 @@ class Plex(commands.Cog):
return return
if not self.vc: if not self.vc:
self.vc = await ctx.author.voice.channel.connect() self.vc = await ctx.author.voice.channel.connect()
logger.debug("Connected to vc") logger.debug("Connected to vc.")
if self.vc.is_playing(): if self.vc.is_playing():
self.play_queue.put(track) self.play_queue.put(track)
@ -95,7 +105,7 @@ class Plex(commands.Cog):
await ctx.send(f"Playing {track.title}") await ctx.send(f"Playing {track.title}")
else: else:
logger.debug(f"{title} was not found.") logger.debug(f"{title} was not found.")
await ctx.send("Song not found!") await ctx.send(f"{title} was not found.")
@command() @command()
async def stop(self, ctx): async def stop(self, ctx):
@ -119,9 +129,9 @@ class Plex(commands.Cog):
@command() @command()
async def skip(self, ctx): async def skip(self, ctx):
logger.debug("Skip")
if self.vc: if self.vc:
await self.vc.stop() self.vc.stop()
if not self.play_queue.empty():
await self._after_callback() await self._after_callback()
@command() @command()