2020-08-10 10:26:38 +02:00
|
|
|
"""All discord bot and Plex api interactions."""
|
2020-08-05 02:07:29 +02:00
|
|
|
import asyncio
|
|
|
|
import io
|
2020-08-04 11:59:48 +02:00
|
|
|
import logging
|
2020-08-05 02:07:29 +02:00
|
|
|
from urllib.request import urlopen
|
2021-05-28 00:39:50 +02:00
|
|
|
import requests
|
2020-07-24 08:41:59 +02:00
|
|
|
|
2020-07-23 06:21:41 +02:00
|
|
|
import discord
|
2020-08-09 01:07:21 +02:00
|
|
|
from async_timeout import timeout
|
2020-07-24 08:41:59 +02:00
|
|
|
from discord import FFmpegPCMAudio
|
2020-07-23 06:21:41 +02:00
|
|
|
from discord.ext import commands
|
|
|
|
from discord.ext.commands import command
|
2020-08-04 11:59:48 +02:00
|
|
|
from plexapi.exceptions import Unauthorized
|
2021-05-26 18:28:57 +02:00
|
|
|
from plexapi.exceptions import NotFound
|
2020-07-23 06:21:41 +02:00
|
|
|
from plexapi.server import PlexServer
|
|
|
|
|
2020-08-13 10:25:15 +02:00
|
|
|
from .exceptions import MediaNotFoundError
|
|
|
|
from .exceptions import VoiceChannelError
|
2020-08-13 09:58:12 +02:00
|
|
|
|
2020-08-09 08:28:14 +02:00
|
|
|
root_log = logging.getLogger()
|
|
|
|
plex_log = logging.getLogger("Plex")
|
|
|
|
bot_log = logging.getLogger("Bot")
|
2020-07-27 06:08:00 +02:00
|
|
|
|
2020-08-13 10:52:22 +02:00
|
|
|
help_text = """
|
|
|
|
General:
|
|
|
|
kill [silent] - Halt the bot [silently].
|
|
|
|
help - Print this help message.
|
|
|
|
cleanup - Delete old messages from the bot.
|
|
|
|
|
|
|
|
Plex:
|
|
|
|
play <SONG_NAME> - Play a song from the plex server.
|
|
|
|
album <ALBUM_NAME> - Queue an entire album to play.
|
2021-05-27 17:11:02 +02:00
|
|
|
playlist <PLAYLIST_NAME> - Queue an entire playlist to play.
|
2022-02-13 13:12:35 +01:00
|
|
|
show_playlists <ARG> <ARG> - Query for playlists with a name matching any of the arguments.
|
2020-09-06 23:32:40 +02:00
|
|
|
lyrics - Print the lyrics of the song (Requires Genius API)
|
2020-08-13 10:52:22 +02:00
|
|
|
np - Print the current playing song.
|
2022-04-15 11:53:43 +02:00
|
|
|
q - Print the current queue (This can take very long!)
|
2020-08-13 10:52:22 +02:00
|
|
|
stop - Halt playback and leave vc.
|
2022-04-15 15:06:30 +02:00
|
|
|
loop - Loop the current song.
|
2022-04-15 15:41:50 +02:00
|
|
|
loopq - Loop the current queue.
|
|
|
|
unloop - Disable looping the current track.
|
|
|
|
unloopq - Disable looping the current queue.
|
2020-08-13 10:52:22 +02:00
|
|
|
pause - Pause playback.
|
|
|
|
resume - Resume playback.
|
2022-04-15 11:53:43 +02:00
|
|
|
skip - Skip the current song. Give a number as argument to skip more than 1.
|
2020-08-13 10:52:22 +02:00
|
|
|
clear - Clear play queue.
|
|
|
|
|
|
|
|
[] - Optional args.
|
|
|
|
"""
|
|
|
|
|
2020-07-23 06:21:41 +02:00
|
|
|
|
|
|
|
class General(commands.Cog):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
General commands
|
2020-08-09 22:47:00 +02:00
|
|
|
|
|
|
|
Manage general bot behavior
|
|
|
|
"""
|
|
|
|
|
2020-07-23 06:21:41 +02:00
|
|
|
def __init__(self, bot):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
Initialize commands
|
2020-08-09 22:47:00 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
bot: discord.ext.command.Bot, bind for cogs
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
2020-07-23 06:21:41 +02:00
|
|
|
self.bot = bot
|
|
|
|
|
|
|
|
@command()
|
2020-08-09 02:08:51 +02:00
|
|
|
async def kill(self, ctx, *args):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
Kill the bot
|
2020-08-09 22:47:00 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
*args: optional flags
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
2020-08-09 02:08:51 +02:00
|
|
|
if "silent" not in args:
|
|
|
|
await ctx.send(f"Stopping upon the request of {ctx.author.mention}")
|
|
|
|
|
2020-07-23 06:21:41 +02:00
|
|
|
await self.bot.close()
|
2020-08-09 22:47:00 +02:00
|
|
|
bot_log.info("Stopping upon the request of %s", ctx.author.mention)
|
2020-07-23 06:21:41 +02:00
|
|
|
|
2020-08-13 10:52:22 +02:00
|
|
|
@command(name="help")
|
|
|
|
async def help(self, ctx):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
Prints command help
|
2020-08-13 10:52:22 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raise:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
|
|
|
|
await ctx.send(f"```{help_text}```")
|
|
|
|
|
2020-08-09 02:08:51 +02:00
|
|
|
@command()
|
|
|
|
async def cleanup(self, ctx, limit=250):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
Delete old messages from bot
|
2020-08-09 22:47:00 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
limit: int number of messages to go back by to delete. Default 250
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
"""
|
2020-08-09 02:08:51 +02:00
|
|
|
channel = ctx.message.channel
|
|
|
|
|
|
|
|
try:
|
|
|
|
async for i in channel.history(limit=limit):
|
|
|
|
# Only delete messages sent by self
|
|
|
|
if i.author == self.bot.user:
|
|
|
|
try:
|
|
|
|
await i.delete()
|
|
|
|
except (discord.Forbidden, discord.NotFound, discord.HTTPException):
|
|
|
|
pass
|
|
|
|
|
2020-09-06 23:32:40 +02:00
|
|
|
async for i in channel.history(limit=limit):
|
|
|
|
if i.author == ctx.message.author and i.content.startswith(
|
|
|
|
self.bot.command_prefix
|
|
|
|
):
|
|
|
|
try:
|
|
|
|
await i.delete()
|
|
|
|
except (discord.Forbidden, discord.NotFound, discord.HTTPException):
|
|
|
|
pass
|
|
|
|
|
2020-08-09 02:08:51 +02:00
|
|
|
except discord.Forbidden:
|
2020-08-09 22:47:00 +02:00
|
|
|
bot_log.info("Unable to delete messages, insufficient permissions.")
|
2020-08-09 02:08:51 +02:00
|
|
|
await ctx.send("I don't have the necessary permissions to delete messages.")
|
|
|
|
|
2020-07-23 06:21:41 +02:00
|
|
|
|
|
|
|
class Plex(commands.Cog):
|
2020-08-13 10:52:22 +02:00
|
|
|
"""
|
|
|
|
Discord commands pertinent to interacting with Plex
|
2020-08-09 22:47:00 +02:00
|
|
|
|
2020-08-13 10:52:22 +02:00
|
|
|
Contains user commands such as play, pause, resume, stop, etc.
|
|
|
|
Grabs, and parses all data from plex database.
|
2020-08-09 22:47:00 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
# pylint: disable=too-many-instance-attributes
|
|
|
|
# All are necessary to detect global interactions
|
|
|
|
# within the bot.
|
|
|
|
|
2020-09-06 23:32:40 +02:00
|
|
|
def __init__(self, bot, **kwargs):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
Initializes Plex resources
|
2020-08-09 22:47:00 +02:00
|
|
|
|
|
|
|
Connects to Plex library and sets up
|
|
|
|
all asyncronous communications.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
bot: discord.ext.command.Bot, bind for cogs
|
|
|
|
base_url: str url to Plex server
|
|
|
|
plex_token: str X-Token of Plex server
|
|
|
|
lib_name: str name of Plex library to search through
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
plexapi.exceptions.Unauthorized: Invalid Plex token
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
|
2020-07-23 06:21:41 +02:00
|
|
|
self.bot = bot
|
2020-09-06 23:32:40 +02:00
|
|
|
self.base_url = kwargs["base_url"]
|
|
|
|
self.plex_token = kwargs["plex_token"]
|
|
|
|
self.library_name = kwargs["lib_name"]
|
|
|
|
self.bot_prefix = bot.command_prefix
|
|
|
|
|
|
|
|
if kwargs["lyrics_token"]:
|
2022-02-13 12:02:49 +01:00
|
|
|
import lyricsgenius
|
2020-09-06 23:32:40 +02:00
|
|
|
self.genius = lyricsgenius.Genius(kwargs["lyrics_token"])
|
|
|
|
else:
|
|
|
|
plex_log.warning("No lyrics token specified, lyrics disabled")
|
|
|
|
self.genius = None
|
2020-07-23 06:21:41 +02:00
|
|
|
|
2020-08-09 22:47:00 +02:00
|
|
|
# Log fatal invalid plex token
|
2020-08-04 11:59:48 +02:00
|
|
|
try:
|
|
|
|
self.pms = PlexServer(self.base_url, self.plex_token)
|
|
|
|
except Unauthorized:
|
2020-08-09 08:28:14 +02:00
|
|
|
plex_log.fatal("Invalid Plex token, stopping...")
|
2020-08-04 11:59:48 +02:00
|
|
|
raise Unauthorized("Invalid Plex token")
|
|
|
|
|
2020-07-23 06:21:41 +02:00
|
|
|
self.music = self.pms.library.section(self.library_name)
|
2020-08-09 22:47:00 +02:00
|
|
|
plex_log.debug("Connected to plex library: %s", self.library_name)
|
2020-07-23 06:21:41 +02:00
|
|
|
|
2020-08-09 22:47:00 +02:00
|
|
|
# Initialize necessary vars
|
|
|
|
self.voice_channel = None
|
2020-07-27 06:08:00 +02:00
|
|
|
self.current_track = None
|
2022-04-15 12:43:55 +02:00
|
|
|
self.is_looping = False
|
2022-04-15 15:41:50 +02:00
|
|
|
self.loop_queue = None
|
2020-08-09 02:08:51 +02:00
|
|
|
self.np_message_id = None
|
2022-04-15 11:53:43 +02:00
|
|
|
self.show_queue_message_ids = []
|
2020-08-09 22:47:00 +02:00
|
|
|
self.ctx = None
|
2020-08-09 02:08:51 +02:00
|
|
|
|
2020-08-09 22:47:00 +02:00
|
|
|
# Initialize events
|
2020-08-05 02:07:29 +02:00
|
|
|
self.play_queue = asyncio.Queue()
|
|
|
|
self.play_next_event = asyncio.Event()
|
|
|
|
|
2020-08-09 22:47:00 +02:00
|
|
|
bot_log.info("Started bot successfully")
|
2020-08-05 02:07:29 +02:00
|
|
|
self.bot.loop.create_task(self._audio_player_task())
|
2020-07-27 06:08:00 +02:00
|
|
|
|
2020-08-09 22:47:00 +02:00
|
|
|
def _search_tracks(self, title: str):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
Search the Plex music db for track
|
2020-07-27 06:08:00 +02:00
|
|
|
|
2020-08-09 22:47:00 +02:00
|
|
|
Args:
|
|
|
|
title: str title of song to search for
|
|
|
|
|
|
|
|
Returns:
|
2020-08-13 10:25:15 +02:00
|
|
|
plexapi.audio.Track pointing to best matching title
|
2020-08-09 22:47:00 +02:00
|
|
|
|
|
|
|
Raises:
|
2020-08-13 10:25:15 +02:00
|
|
|
MediaNotFoundError: Title of track can't be found in plex db
|
2020-08-09 22:47:00 +02:00
|
|
|
"""
|
2020-08-13 09:58:12 +02:00
|
|
|
results = self.music.searchTracks(title=title, maxresults=1)
|
|
|
|
try:
|
|
|
|
return results[0]
|
|
|
|
except IndexError:
|
2020-08-13 10:25:15 +02:00
|
|
|
raise MediaNotFoundError("Track cannot be found")
|
|
|
|
|
|
|
|
def _search_albums(self, title: str):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
Search the Plex music db for album
|
2020-08-13 10:25:15 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
title: str title of album to search for
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
plexapi.audio.Album pointing to best matching title
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
MediaNotFoundError: Title of album can't be found in plex db
|
|
|
|
"""
|
|
|
|
results = self.music.searchAlbums(title=title, maxresults=1)
|
|
|
|
try:
|
|
|
|
return results[0]
|
|
|
|
except IndexError:
|
|
|
|
raise MediaNotFoundError("Album cannot be found")
|
2021-05-27 23:33:24 +02:00
|
|
|
|
2021-05-26 18:28:57 +02:00
|
|
|
def _search_playlists(self, title: str):
|
2021-05-27 23:33:24 +02:00
|
|
|
"""
|
|
|
|
Search the Plex music db for playlist
|
2021-05-26 18:28:57 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
title: str title of playlist to search for
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
plexapi.playlist pointing to best matching title
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
MediaNotFoundError: Title of playlist can't be found in plex db
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return self.pms.playlist(title)
|
|
|
|
except NotFound:
|
|
|
|
raise MediaNotFoundError("Playlist cannot be found")
|
2020-07-23 06:21:41 +02:00
|
|
|
|
2022-02-13 13:12:35 +01:00
|
|
|
def _get_playlists(self):
|
|
|
|
"""
|
|
|
|
Search the Plex music db for playlist
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
List of plexapi.playlist
|
|
|
|
"""
|
|
|
|
return self.pms.playlists()
|
|
|
|
|
2020-08-05 02:07:29 +02:00
|
|
|
async def _play(self):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
Heavy lifting of playing songs
|
2020-08-09 22:47:00 +02:00
|
|
|
|
|
|
|
Grabs the appropiate streaming URL, sends the `now playing`
|
|
|
|
message, and initiates playback in the vc.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
None
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
2020-08-05 02:07:29 +02:00
|
|
|
track_url = self.current_track.getStreamURL()
|
|
|
|
audio_stream = FFmpegPCMAudio(track_url)
|
2020-08-05 02:17:22 +02:00
|
|
|
|
2022-04-15 11:53:43 +02:00
|
|
|
while self.voice_channel and self.voice_channel.is_playing():
|
2022-04-15 12:42:49 +02:00
|
|
|
bot_log.debug("waiting for track to finish")
|
2022-04-15 11:53:43 +02:00
|
|
|
await asyncio.sleep(2)
|
2022-04-15 12:42:49 +02:00
|
|
|
bot_log.debug("track finished")
|
2020-08-05 02:17:22 +02:00
|
|
|
|
2022-04-15 12:42:49 +02:00
|
|
|
if self.voice_channel:
|
|
|
|
self.voice_channel.play(audio_stream, after=self._toggle_next)
|
|
|
|
|
|
|
|
plex_log.debug("%s - URL: %s", self.current_track, track_url)
|
|
|
|
|
|
|
|
embed, img = self._build_embed_track(self.current_track)
|
|
|
|
self.np_message_id = await self.ctx.send(embed=embed, file=img)
|
2020-08-05 01:20:13 +02:00
|
|
|
|
2022-04-15 12:42:49 +02:00
|
|
|
async def _play_next(self):
|
2022-04-15 12:43:55 +02:00
|
|
|
if self.is_looping:
|
|
|
|
self.current_track = self.is_looping
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
self.current_track = await self.play_queue.get()
|
|
|
|
except asyncio.exceptions.CancelledError:
|
|
|
|
bot_log.debug("failed to pop queue")
|
2022-04-15 15:41:50 +02:00
|
|
|
|
|
|
|
if not self.current_track and self.loop_queue:
|
|
|
|
try:
|
|
|
|
self.current_track = await self.loop_queue.get()
|
|
|
|
except asyncio.exceptions.CancelledError:
|
|
|
|
bot_log.debug("failed to pop queue")
|
|
|
|
self.play_queue = self.loop_queue
|
|
|
|
self.loop_queue = asyncio.Queue()
|
2020-08-05 02:07:29 +02:00
|
|
|
|
2022-04-15 12:42:49 +02:00
|
|
|
|
2020-08-05 02:07:29 +02:00
|
|
|
async def _audio_player_task(self):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
Coroutine to handle playback and queuing
|
2020-08-09 22:47:00 +02:00
|
|
|
|
|
|
|
Always-running function awaiting new songs to be added.
|
|
|
|
Auto disconnects from VC if idle for > 15 seconds.
|
|
|
|
Handles auto deletion of now playing song notifications.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
None
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
2020-08-05 02:07:29 +02:00
|
|
|
while True:
|
|
|
|
self.play_next_event.clear()
|
2020-08-09 22:47:00 +02:00
|
|
|
if self.voice_channel:
|
2020-08-09 01:07:21 +02:00
|
|
|
try:
|
|
|
|
# Disconnect after 15 seconds idle
|
|
|
|
async with timeout(15):
|
2022-04-15 12:42:49 +02:00
|
|
|
await self._play_next()
|
2020-08-09 01:07:21 +02:00
|
|
|
except asyncio.TimeoutError:
|
2022-04-15 12:42:49 +02:00
|
|
|
bot_log("timeout - disconnecting")
|
2020-08-09 22:47:00 +02:00
|
|
|
await self.voice_channel.disconnect()
|
|
|
|
self.voice_channel = None
|
2020-08-09 01:07:21 +02:00
|
|
|
|
|
|
|
if not self.current_track:
|
2022-04-15 12:42:49 +02:00
|
|
|
await self._play_next()
|
2020-08-09 01:07:21 +02:00
|
|
|
|
2020-08-05 02:07:29 +02:00
|
|
|
await self._play()
|
|
|
|
await self.play_next_event.wait()
|
2022-04-15 11:53:43 +02:00
|
|
|
if self.np_message_id:
|
|
|
|
await self.np_message_id.delete()
|
2020-08-05 02:07:29 +02:00
|
|
|
|
|
|
|
def _toggle_next(self, error=None):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
Callback for vc playback
|
2020-08-09 22:47:00 +02:00
|
|
|
|
|
|
|
Clears current track, then activates _audio_player_task
|
|
|
|
to play next in queue or disconnect.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
error: Optional parameter required for discord.py callback
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
2022-04-15 15:41:50 +02:00
|
|
|
if self.loop_queue:
|
|
|
|
self.bot.loop.call_soon_threadsafe(self.loop_queue.put,self.current_track)
|
2020-08-09 01:07:21 +02:00
|
|
|
self.current_track = None
|
2020-08-05 02:07:29 +02:00
|
|
|
self.bot.loop.call_soon_threadsafe(self.play_next_event.set)
|
2020-08-05 01:20:13 +02:00
|
|
|
|
2020-08-13 11:20:41 +02:00
|
|
|
@staticmethod
|
|
|
|
def _build_embed_track(track, type_="play"):
|
2021-05-27 23:33:24 +02:00
|
|
|
"""
|
|
|
|
Creates a pretty embed card for tracks
|
2020-08-09 22:47:00 +02:00
|
|
|
|
|
|
|
Builds a helpful status embed with the following info:
|
2020-08-13 10:25:15 +02:00
|
|
|
Status, song title, album, artist and album art. All
|
2020-08-09 22:47:00 +02:00
|
|
|
pertitent information is grabbed dynamically from the Plex db.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
track: plexapi.audio.Track object of song
|
|
|
|
type_: Type of card to make (play, queue).
|
|
|
|
|
|
|
|
Returns:
|
2020-08-13 10:25:15 +02:00
|
|
|
embed: discord.embed fully constructed payload.
|
2020-08-09 22:47:00 +02:00
|
|
|
thumb_art: io.BytesIO of album thumbnail img.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
ValueError: Unsupported type of embed {type_}
|
2020-08-05 02:07:29 +02:00
|
|
|
"""
|
2020-08-05 01:20:13 +02:00
|
|
|
# Grab the relevant thumbnail
|
2022-04-15 14:27:45 +02:00
|
|
|
if track.thumbUrl:
|
|
|
|
img_stream = requests.get(track.thumbUrl, stream=True).raw
|
|
|
|
img = io.BytesIO(img_stream.read())
|
2020-08-05 01:20:13 +02:00
|
|
|
|
2022-04-15 14:27:45 +02:00
|
|
|
# Attach to discord embed
|
|
|
|
art_file = discord.File(img, filename="image0.png")
|
|
|
|
else:
|
|
|
|
art_file = None
|
|
|
|
|
2020-08-09 01:07:21 +02:00
|
|
|
# Get appropiate status message
|
2020-08-09 22:47:00 +02:00
|
|
|
if type_ == "play":
|
2020-08-05 02:07:29 +02:00
|
|
|
title = f"Now Playing - {track.title}"
|
2020-08-09 22:47:00 +02:00
|
|
|
elif type_ == "queue":
|
2020-08-05 02:07:29 +02:00
|
|
|
title = f"Added to queue - {track.title}"
|
2022-04-15 11:53:43 +02:00
|
|
|
elif type_ == "queued":
|
|
|
|
title = f"Next in line - {track.title}"
|
2020-08-05 02:07:29 +02:00
|
|
|
else:
|
2020-08-09 22:47:00 +02:00
|
|
|
raise ValueError(f"Unsupported type of embed {type_}")
|
2020-08-05 01:20:13 +02:00
|
|
|
|
2020-08-09 01:07:21 +02:00
|
|
|
# Include song details
|
2020-08-05 02:07:29 +02:00
|
|
|
descrip = f"{track.album().title} - {track.artist().title}"
|
2020-08-05 01:20:13 +02:00
|
|
|
|
|
|
|
# Build the actual embed
|
|
|
|
embed = discord.Embed(
|
2020-08-05 02:07:29 +02:00
|
|
|
title=title, description=descrip, colour=discord.Color.red()
|
2020-08-05 01:20:13 +02:00
|
|
|
)
|
|
|
|
embed.set_author(name="Plex")
|
2020-08-05 02:07:29 +02:00
|
|
|
# Point to file attached with ctx object.
|
|
|
|
embed.set_thumbnail(url="attachment://image0.png")
|
2020-08-05 01:20:13 +02:00
|
|
|
|
2020-08-13 10:25:15 +02:00
|
|
|
bot_log.debug("Built embed for track - %s", track.title)
|
|
|
|
|
|
|
|
return embed, art_file
|
|
|
|
|
2020-08-13 11:20:41 +02:00
|
|
|
@staticmethod
|
|
|
|
def _build_embed_album(album):
|
2021-05-27 23:33:24 +02:00
|
|
|
"""
|
|
|
|
Creates a pretty embed card for albums
|
2020-08-13 10:25:15 +02:00
|
|
|
|
|
|
|
Builds a helpful status embed with the following info:
|
|
|
|
album, artist, and album art. All pertitent information
|
|
|
|
is grabbed dynamically from the Plex db.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
album: plexapi.audio.Album object of album
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
embed: discord.embed fully constructed payload.
|
|
|
|
thumb_art: io.BytesIO of album thumbnail img.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
# Grab the relevant thumbnail
|
2021-05-28 00:35:34 +02:00
|
|
|
img_stream = requests.get(album.thumbUrl, stream=True).raw
|
2020-08-13 10:25:15 +02:00
|
|
|
img = io.BytesIO(img_stream.read())
|
|
|
|
|
|
|
|
# Attach to discord embed
|
|
|
|
art_file = discord.File(img, filename="image0.png")
|
|
|
|
title = "Added album to queue"
|
|
|
|
descrip = f"{album.title} - {album.artist().title}"
|
|
|
|
|
|
|
|
embed = discord.Embed(
|
|
|
|
title=title, description=descrip, colour=discord.Color.red()
|
|
|
|
)
|
|
|
|
embed.set_author(name="Plex")
|
|
|
|
embed.set_thumbnail(url="attachment://image0.png")
|
|
|
|
bot_log.debug("Built embed for album - %s", album.title)
|
2020-08-09 08:28:14 +02:00
|
|
|
|
2020-08-09 22:47:00 +02:00
|
|
|
return embed, art_file
|
2021-05-28 00:35:34 +02:00
|
|
|
|
2021-05-26 18:28:57 +02:00
|
|
|
@staticmethod
|
2022-02-13 13:10:59 +01:00
|
|
|
def _build_embed_playlist(self, playlist, title, descrip):
|
2021-05-27 23:33:24 +02:00
|
|
|
"""
|
|
|
|
Creates a pretty embed card for playlists
|
2021-05-26 18:28:57 +02:00
|
|
|
|
|
|
|
Builds a helpful status embed with the following info:
|
|
|
|
playlist art. All pertitent information
|
|
|
|
is grabbed dynamically from the Plex db.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
playlist: plexapi.playlist object of playlist
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
embed: discord.embed fully constructed payload.
|
|
|
|
thumb_art: io.BytesIO of playlist thumbnail img.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
# Grab the relevant thumbnail
|
2022-02-13 13:10:59 +01:00
|
|
|
try:
|
|
|
|
img_stream = requests.get(self.pms.url(playlist.composite, True), stream=True).raw
|
|
|
|
img = io.BytesIO(img_stream.read())
|
|
|
|
except:
|
|
|
|
raise MediaNotFoundError("no image available")
|
2021-05-26 18:28:57 +02:00
|
|
|
|
|
|
|
# Attach to discord embed
|
|
|
|
art_file = discord.File(img, filename="image0.png")
|
|
|
|
|
|
|
|
embed = discord.Embed(
|
|
|
|
title=title, description=descrip, colour=discord.Color.red()
|
|
|
|
)
|
|
|
|
embed.set_author(name="Plex")
|
|
|
|
embed.set_thumbnail(url="attachment://image0.png")
|
|
|
|
bot_log.debug("Built embed for playlist - %s", playlist.title)
|
|
|
|
|
|
|
|
return embed, art_file
|
2020-07-24 08:41:59 +02:00
|
|
|
|
2020-08-13 10:25:15 +02:00
|
|
|
async def _validate(self, ctx):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
Ensures user is in a vc
|
2020-08-13 10:25:15 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
VoiceChannelError: Author not in voice channel
|
|
|
|
"""
|
|
|
|
# Fail if user not in vc
|
|
|
|
if not ctx.author.voice:
|
|
|
|
await ctx.send("Join a voice channel first!")
|
|
|
|
bot_log.debug("Failed to play, requester not in voice channel")
|
|
|
|
raise VoiceChannelError
|
|
|
|
|
|
|
|
# Connect to voice if not already
|
|
|
|
if not self.voice_channel:
|
2022-04-15 12:42:49 +02:00
|
|
|
try:
|
|
|
|
self.voice_channel = await ctx.author.voice.channel.connect()
|
|
|
|
bot_log.debug("Connected to vc.")
|
|
|
|
except asyncio.exceptions.TimeoutError:
|
|
|
|
bot_log.debug("Cannot connect to vc - timeout")
|
2020-08-13 10:25:15 +02:00
|
|
|
|
2020-07-23 06:21:41 +02:00
|
|
|
@command()
|
|
|
|
async def play(self, ctx, *args):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
User command to play song
|
2020-08-09 22:47:00 +02:00
|
|
|
|
|
|
|
Searchs plex db and either, initiates playback, or
|
|
|
|
adds to queue. Handles invalid usage from the user.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
*args: Title of song to play
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
2020-08-09 02:08:51 +02:00
|
|
|
# Save the context to use with async callbacks
|
|
|
|
self.ctx = ctx
|
2020-07-23 06:21:41 +02:00
|
|
|
title = " ".join(args)
|
2020-08-13 10:25:15 +02:00
|
|
|
|
2020-08-13 09:58:12 +02:00
|
|
|
try:
|
|
|
|
track = self._search_tracks(title)
|
2020-08-13 10:25:15 +02:00
|
|
|
except MediaNotFoundError:
|
2020-08-05 02:07:29 +02:00
|
|
|
await ctx.send(f"Can't find song: {title}")
|
2020-08-09 22:47:00 +02:00
|
|
|
bot_log.debug("Failed to play, can't find song - %s", title)
|
2020-08-05 02:07:29 +02:00
|
|
|
return
|
2020-08-09 08:28:14 +02:00
|
|
|
|
2020-08-13 10:25:15 +02:00
|
|
|
try:
|
|
|
|
await self._validate(ctx)
|
|
|
|
except VoiceChannelError:
|
|
|
|
pass
|
2020-08-05 02:07:29 +02:00
|
|
|
|
|
|
|
# Specific add to queue message
|
2022-04-15 12:42:49 +02:00
|
|
|
if self.voice_channel and self.voice_channel.is_playing():
|
2020-08-09 22:47:00 +02:00
|
|
|
bot_log.debug("Added to queue - %s", title)
|
2020-08-13 10:25:15 +02:00
|
|
|
embed, img = self._build_embed_track(track, type_="queue")
|
2020-08-09 22:47:00 +02:00
|
|
|
await ctx.send(embed=embed, file=img)
|
2020-08-05 02:07:29 +02:00
|
|
|
|
|
|
|
# Add the song to the async queue
|
|
|
|
await self.play_queue.put(track)
|
2020-07-24 08:41:59 +02:00
|
|
|
|
2020-08-13 10:25:15 +02:00
|
|
|
@command()
|
|
|
|
async def album(self, ctx, *args):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
User command to play song
|
2020-08-13 10:25:15 +02:00
|
|
|
|
|
|
|
Searchs plex db and either, initiates playback, or
|
|
|
|
adds to queue. Handles invalid usage from the user.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
*args: Title of song to play
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
# Save the context to use with async callbacks
|
|
|
|
self.ctx = ctx
|
|
|
|
title = " ".join(args)
|
|
|
|
|
|
|
|
try:
|
|
|
|
album = self._search_albums(title)
|
|
|
|
except MediaNotFoundError:
|
|
|
|
await ctx.send(f"Can't find album: {title}")
|
|
|
|
bot_log.debug("Failed to queue album, can't find - %s", title)
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
await self._validate(ctx)
|
|
|
|
except VoiceChannelError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
bot_log.debug("Added to queue - %s", title)
|
|
|
|
embed, img = self._build_embed_album(album)
|
|
|
|
await ctx.send(embed=embed, file=img)
|
|
|
|
|
|
|
|
for track in album.tracks():
|
|
|
|
await self.play_queue.put(track)
|
|
|
|
|
2022-04-15 15:05:37 +02:00
|
|
|
async def play_playlist(self, title, shuffle=False):
|
|
|
|
try:
|
|
|
|
playlist = self._search_playlists(title)
|
|
|
|
except MediaNotFoundError:
|
|
|
|
await self.ctx.send(f"Can't find playlist: {title}")
|
|
|
|
bot_log.debug("Failed to queue playlist, can't find - %s", title)
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
await self._validate(self.ctx)
|
|
|
|
except VoiceChannelError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
embed, img = self._build_embed_playlist(self, playlist, "Added playlist to queue", playlist.title)
|
|
|
|
await self.ctx.send(embed=embed, file=img)
|
|
|
|
|
|
|
|
items = [ item for item in playlist.items() if item.TYPE == "track" ]
|
|
|
|
if shuffle:
|
|
|
|
from random import shuffle
|
|
|
|
shuffle(items)
|
|
|
|
|
|
|
|
for item in items:
|
|
|
|
await self.play_queue.put(item)
|
|
|
|
|
|
|
|
bot_log.debug("Added to queue - %s", title)
|
|
|
|
|
|
|
|
except MediaNotFoundError:
|
|
|
|
await self.ctx.send(message="Playlist "+title+" seems to be empty!")
|
|
|
|
bot_log.debug("Playlist empty - %s", title)
|
|
|
|
|
2021-05-26 18:28:57 +02:00
|
|
|
@command()
|
|
|
|
async def playlist(self, ctx, *args):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
User command to play playlist
|
2021-05-26 18:28:57 +02:00
|
|
|
|
|
|
|
Searchs plex db and either, initiates playback, or
|
|
|
|
adds to queue. Handles invalid usage from the user.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
*args: Title of playlist to play
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
# Save the context to use with async callbacks
|
|
|
|
self.ctx = ctx
|
|
|
|
title = " ".join(args)
|
2022-04-15 15:05:37 +02:00
|
|
|
await self.play_playlist(title)
|
2021-05-26 18:28:57 +02:00
|
|
|
|
2022-04-15 15:05:37 +02:00
|
|
|
@command()
|
|
|
|
async def playlist_shuffle(self, ctx, *args):
|
|
|
|
"""
|
|
|
|
User command to play playlist in shuffle mode
|
2021-05-26 18:28:57 +02:00
|
|
|
|
2022-04-15 15:05:37 +02:00
|
|
|
Searchs plex db and either, initiates playback, or
|
|
|
|
adds to queue. Handles invalid usage from the user.
|
2021-05-26 18:28:57 +02:00
|
|
|
|
2022-04-15 15:05:37 +02:00
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
*args: Title of playlist to play
|
2021-05-26 18:28:57 +02:00
|
|
|
|
2022-04-15 15:05:37 +02:00
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
# Save the context to use with async callbacks
|
|
|
|
self.ctx = ctx
|
|
|
|
title = " ".join(args)
|
|
|
|
await self.play_playlist(title,shuffle=True)
|
|
|
|
|
|
|
|
|
2022-02-13 13:12:35 +01:00
|
|
|
@command()
|
|
|
|
async def show_playlists(self, ctx, *args):
|
|
|
|
"""
|
|
|
|
User command to show playlists
|
|
|
|
|
|
|
|
Searchs plex db and shows playlists matching.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
*args: String filter for playlist names
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
# Save the context to use with async callbacks
|
|
|
|
self.ctx = ctx
|
|
|
|
|
|
|
|
playlists = self._get_playlists()
|
|
|
|
|
|
|
|
try:
|
|
|
|
await self._validate(ctx)
|
|
|
|
except VoiceChannelError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
for playlist in playlists:
|
|
|
|
if args and not any(arg in playlist.title for arg in args):
|
|
|
|
continue
|
|
|
|
from datetime import timedelta
|
|
|
|
if playlist.duration:
|
|
|
|
seconds = playlist.duration / 1000
|
|
|
|
embed, img = self._build_embed_playlist(self, playlist, playlist.title, "{:0>8}".format(str(timedelta(seconds=seconds))))
|
|
|
|
await ctx.send(embed=embed, file=img)
|
2021-05-26 18:28:57 +02:00
|
|
|
|
2020-07-24 08:41:59 +02:00
|
|
|
@command()
|
|
|
|
async def stop(self, ctx):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
User command to stop playback
|
2020-08-09 22:47:00 +02:00
|
|
|
|
|
|
|
Stops playback and disconnects from vc.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
if self.voice_channel:
|
|
|
|
self.voice_channel.stop()
|
|
|
|
await self.voice_channel.disconnect()
|
|
|
|
self.voice_channel = None
|
2020-08-09 01:07:21 +02:00
|
|
|
self.ctx = None
|
2020-08-09 08:28:14 +02:00
|
|
|
bot_log.debug("Stopped")
|
2020-08-05 02:17:22 +02:00
|
|
|
await ctx.send(":stop_button: Stopped")
|
2020-07-24 08:41:59 +02:00
|
|
|
|
2022-04-15 12:43:55 +02:00
|
|
|
@command()
|
|
|
|
async def loop(self, ctx):
|
|
|
|
"""
|
|
|
|
User command to activate looping the current track
|
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
bot_log.debug("Looping "+str(self.current_track))
|
|
|
|
self.is_looping = self.current_track
|
|
|
|
|
2022-04-15 15:41:50 +02:00
|
|
|
@command()
|
|
|
|
async def loopq(self, ctx):
|
|
|
|
"""
|
|
|
|
User command to activate looping the current queue
|
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
bot_log.debug("Looping current queue")
|
|
|
|
self.loop_queue = asyncio.Queue()
|
|
|
|
|
2022-04-15 12:43:55 +02:00
|
|
|
@command()
|
|
|
|
async def unloop(self, ctx):
|
|
|
|
"""
|
|
|
|
User command to deactivate looping the current track
|
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
2022-04-15 15:41:50 +02:00
|
|
|
bot_log.debug("Looping current queue")
|
|
|
|
self.is_looping = False
|
|
|
|
|
|
|
|
@command()
|
|
|
|
async def unloopq(self, ctx):
|
|
|
|
"""
|
|
|
|
User command to deactivate looping the current queue
|
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
bot_log.debug("Unlooping")
|
|
|
|
self.loop_queue = None
|
2022-04-15 12:43:55 +02:00
|
|
|
|
|
|
|
|
2020-07-24 08:41:59 +02:00
|
|
|
@command()
|
|
|
|
async def pause(self, ctx):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
User command to pause playback
|
2020-08-09 22:47:00 +02:00
|
|
|
|
|
|
|
Pauses playback, but doesn't reset anything
|
|
|
|
to allow playback resuming.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
if self.voice_channel:
|
|
|
|
self.voice_channel.pause()
|
2020-08-09 08:28:14 +02:00
|
|
|
bot_log.debug("Paused")
|
2020-08-05 02:07:29 +02:00
|
|
|
await ctx.send(":play_pause: Paused")
|
2020-07-24 08:41:59 +02:00
|
|
|
|
|
|
|
@command()
|
|
|
|
async def resume(self, ctx):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
User command to resume playback
|
2020-08-09 22:47:00 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
if self.voice_channel:
|
|
|
|
self.voice_channel.resume()
|
2020-08-09 08:28:14 +02:00
|
|
|
bot_log.debug("Resumed")
|
2020-08-05 02:07:29 +02:00
|
|
|
await ctx.send(":play_pause: Resumed")
|
2020-07-24 08:41:59 +02:00
|
|
|
|
|
|
|
@command()
|
2022-04-15 11:53:43 +02:00
|
|
|
async def skip(self, ctx, *args):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
User command to skip song in queue
|
2020-08-09 22:47:00 +02:00
|
|
|
|
|
|
|
Skips currently playing song. If no other songs in
|
|
|
|
queue, stops playback, otherwise moves to next song.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
2022-04-15 11:53:43 +02:00
|
|
|
n = 1
|
|
|
|
if args:
|
|
|
|
n = int(args[0])
|
|
|
|
bot_log.debug("Skipping "+str(n))
|
2020-08-09 22:47:00 +02:00
|
|
|
if self.voice_channel:
|
|
|
|
self.voice_channel.stop()
|
2020-08-09 08:28:14 +02:00
|
|
|
bot_log.debug("Skipped")
|
2022-04-15 11:53:43 +02:00
|
|
|
if n>1:
|
|
|
|
for i in range(n-1):
|
|
|
|
await self.play_queue.get()
|
2020-08-05 02:07:29 +02:00
|
|
|
self._toggle_next()
|
2020-07-27 06:08:00 +02:00
|
|
|
|
2020-08-09 22:47:00 +02:00
|
|
|
@command(name="np")
|
|
|
|
async def now_playing(self, ctx):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
User command to get currently playing song.
|
2020-08-09 22:47:00 +02:00
|
|
|
|
|
|
|
Deletes old `now playing` status message,
|
|
|
|
Creates a new one with up to date information.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
2020-08-05 02:07:29 +02:00
|
|
|
if self.current_track:
|
2022-04-15 11:53:43 +02:00
|
|
|
embed, img = self._build_embed_track(self.current_track,type_="play")
|
2020-08-09 08:28:14 +02:00
|
|
|
bot_log.debug("Now playing")
|
2020-08-09 08:35:27 +02:00
|
|
|
if self.np_message_id:
|
2022-04-15 11:53:43 +02:00
|
|
|
try:
|
|
|
|
await self.np_message_id.delete()
|
|
|
|
bot_log.debug("Deleted old np status")
|
|
|
|
except discord.errors.NotFound:
|
|
|
|
pass
|
2020-08-09 08:35:27 +02:00
|
|
|
|
2020-08-09 22:47:00 +02:00
|
|
|
bot_log.debug("Created np status")
|
|
|
|
self.np_message_id = await ctx.send(embed=embed, file=img)
|
2020-08-05 01:20:13 +02:00
|
|
|
|
2022-04-15 11:53:43 +02:00
|
|
|
@command(name="q")
|
|
|
|
async def show_queue(self, ctx):
|
|
|
|
"""
|
|
|
|
User command to print the current queue
|
|
|
|
|
|
|
|
Deletes old `now playing` status message,
|
|
|
|
Creates a new one with up to date information.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
bot_log.debug("Deleted old queue messages")
|
|
|
|
for msg in self.show_queue_message_ids:
|
|
|
|
await msg.delete()
|
|
|
|
|
|
|
|
# need to do this in order to avoid errors when queue is modified during inspection
|
|
|
|
elems = []
|
|
|
|
for track in self.play_queue._queue:
|
|
|
|
elems.append(track)
|
|
|
|
|
|
|
|
for track in elems:
|
|
|
|
embed, img = self._build_embed_track(track,type_="queued")
|
|
|
|
bot_log.debug("Show queue")
|
|
|
|
|
|
|
|
bot_log.debug("Created queue message")
|
|
|
|
self.show_queue_message_ids.append(await ctx.send(embed=embed, file=img))
|
|
|
|
|
2020-08-05 01:20:13 +02:00
|
|
|
@command()
|
|
|
|
async def clear(self, ctx):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
User command to clear play queue.
|
2020-08-09 22:47:00 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
2020-08-05 02:07:29 +02:00
|
|
|
self.play_queue = asyncio.Queue()
|
2022-04-15 15:41:50 +02:00
|
|
|
self.loop_queue = None
|
2020-08-09 08:28:14 +02:00
|
|
|
bot_log.debug("Cleared queue")
|
2020-08-05 02:07:29 +02:00
|
|
|
await ctx.send(":boom: Queue cleared.")
|
2020-09-06 23:32:40 +02:00
|
|
|
|
|
|
|
@command()
|
|
|
|
async def lyrics(self, ctx):
|
2021-05-28 00:35:34 +02:00
|
|
|
"""
|
|
|
|
User command to get lyrics of a song.
|
2020-09-06 23:32:40 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
ctx: discord.ext.commands.Context message context from command
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
None
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
None
|
|
|
|
"""
|
|
|
|
if not self.current_track:
|
|
|
|
plex_log.info("No song currently playing")
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.genius:
|
|
|
|
plex_log.info(
|
|
|
|
"Searching for %s, %s",
|
|
|
|
self.current_track.title,
|
|
|
|
self.current_track.artist().title,
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
song = self.genius.search_song(
|
|
|
|
self.current_track.title, self.current_track.artist().title
|
|
|
|
)
|
|
|
|
except TypeError:
|
|
|
|
self.genius = None
|
|
|
|
plex_log.error("Invalid genius token, disabling lyrics")
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
lyrics = song.lyrics
|
|
|
|
# Split into 1950 char chunks
|
|
|
|
# Discord max message length is 2000
|
|
|
|
lines = [(lyrics[i : i + 1950]) for i in range(0, len(lyrics), 1950)]
|
|
|
|
|
|
|
|
for i in lines:
|
|
|
|
if i == "":
|
|
|
|
continue
|
|
|
|
# Apply code block format
|
|
|
|
i = f"```{i}```"
|
|
|
|
await ctx.send(i)
|
|
|
|
|
|
|
|
except (IndexError, TypeError):
|
|
|
|
plex_log.info("Could not find lyrics")
|
|
|
|
await ctx.send("Can't find lyrics for this song.")
|
|
|
|
else:
|
|
|
|
plex_log.warning("Attempted lyrics without valid token")
|