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
|
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
|
|
|
|
from fuzzywuzzy import fuzz
|
2020-08-04 11:59:48 +02:00
|
|
|
from plexapi.exceptions import Unauthorized
|
2020-07-23 06:21:41 +02:00
|
|
|
from plexapi.server import PlexServer
|
|
|
|
|
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-07-23 06:21:41 +02:00
|
|
|
|
|
|
|
class General(commands.Cog):
|
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
|
|
|
|
|
|
|
@command()
|
2020-08-09 02:08:51 +02:00
|
|
|
async def kill(self, ctx, *args):
|
|
|
|
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 08:28:14 +02:00
|
|
|
bot_log.info(f"Stopping upon the request of {ctx.author.mention}")
|
2020-07-23 06:21:41 +02:00
|
|
|
|
2020-08-09 02:08:51 +02:00
|
|
|
@command()
|
|
|
|
async def cleanup(self, ctx, limit=250):
|
|
|
|
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
|
|
|
|
|
|
|
|
except discord.Forbidden:
|
|
|
|
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-05 02:07:29 +02:00
|
|
|
def __init__(self, bot, base_url, plex_token, lib_name, bot_prefix) -> None:
|
2020-07-23 06:21:41 +02:00
|
|
|
self.bot = bot
|
|
|
|
self.base_url = base_url
|
|
|
|
self.plex_token = plex_token
|
|
|
|
self.library_name = lib_name
|
2020-08-05 02:07:29 +02:00
|
|
|
self.bot_prefix = bot_prefix
|
2020-07-23 06:21:41 +02:00
|
|
|
|
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 08:28:14 +02:00
|
|
|
plex_log.debug(f"Connected to plex library: {self.library_name}")
|
2020-07-23 06:21:41 +02:00
|
|
|
|
2020-07-24 08:41:59 +02:00
|
|
|
self.vc = None
|
2020-07-27 06:08:00 +02:00
|
|
|
self.current_track = None
|
2020-08-09 02:08:51 +02:00
|
|
|
self.np_message_id = None
|
|
|
|
|
2020-08-05 02:07:29 +02:00
|
|
|
self.play_queue = asyncio.Queue()
|
|
|
|
self.play_next_event = asyncio.Event()
|
|
|
|
|
|
|
|
self.bot.loop.create_task(self._audio_player_task())
|
2020-07-27 06:08:00 +02:00
|
|
|
|
2020-08-09 08:28:14 +02:00
|
|
|
bot_log.info("Started bot successfully")
|
2020-07-27 06:08:00 +02:00
|
|
|
|
2020-07-23 06:21:41 +02:00
|
|
|
def _search_tracks(self, title):
|
|
|
|
tracks = self.music.searchTracks()
|
2020-07-24 08:41:59 +02:00
|
|
|
score = [None, -1]
|
2020-07-23 06:21:41 +02:00
|
|
|
for i in tracks:
|
|
|
|
s = fuzz.ratio(title.lower(), i.title.lower())
|
|
|
|
if s > score[1]:
|
2020-07-24 08:41:59 +02:00
|
|
|
score[0] = i
|
2020-07-23 06:21:41 +02:00
|
|
|
score[1] = s
|
|
|
|
elif s == score[1]:
|
2020-07-24 08:41:59 +02:00
|
|
|
score[0] = i
|
2020-07-23 06:21:41 +02:00
|
|
|
|
2020-07-24 08:41:59 +02:00
|
|
|
return score[0]
|
2020-07-23 06:21:41 +02:00
|
|
|
|
2020-08-05 02:07:29 +02:00
|
|
|
async def _play(self):
|
|
|
|
track_url = self.current_track.getStreamURL()
|
|
|
|
audio_stream = FFmpegPCMAudio(track_url)
|
2020-08-05 02:17:22 +02:00
|
|
|
|
|
|
|
while self.vc.is_playing():
|
2020-08-09 01:07:21 +02:00
|
|
|
asyncio.sleep(2)
|
2020-08-05 02:17:22 +02:00
|
|
|
|
2020-08-05 02:07:29 +02:00
|
|
|
self.vc.play(audio_stream, after=self._toggle_next)
|
2020-08-05 01:20:13 +02:00
|
|
|
|
2020-08-09 08:28:14 +02:00
|
|
|
plex_log.debug(f"{self.current_track.title} - URL: {track_url}")
|
2020-08-05 01:20:13 +02:00
|
|
|
|
2020-08-05 02:07:29 +02:00
|
|
|
embed, f = self._build_embed(self.current_track)
|
2020-08-09 02:08:51 +02:00
|
|
|
self.np_message_id = await self.ctx.send(embed=embed, file=f)
|
2020-08-05 02:07:29 +02:00
|
|
|
|
|
|
|
async def _audio_player_task(self):
|
|
|
|
while True:
|
|
|
|
self.play_next_event.clear()
|
2020-08-09 01:07:21 +02:00
|
|
|
if self.vc:
|
|
|
|
try:
|
|
|
|
# Disconnect after 15 seconds idle
|
|
|
|
async with timeout(15):
|
|
|
|
self.current_track = await self.play_queue.get()
|
|
|
|
except asyncio.TimeoutError:
|
|
|
|
await self.vc.disconnect()
|
|
|
|
self.vc = None
|
|
|
|
|
|
|
|
if not self.current_track:
|
|
|
|
self.current_track = await self.play_queue.get()
|
|
|
|
|
2020-08-05 02:07:29 +02:00
|
|
|
await self._play()
|
|
|
|
await self.play_next_event.wait()
|
2020-08-09 02:08:51 +02:00
|
|
|
await self.np_message_id.delete()
|
2020-08-05 02:07:29 +02:00
|
|
|
|
|
|
|
def _toggle_next(self, error=None):
|
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-05 02:07:29 +02:00
|
|
|
def _build_embed(self, track, t="play"):
|
|
|
|
"""Creates a pretty embed card.
|
|
|
|
"""
|
2020-08-05 01:20:13 +02:00
|
|
|
# Grab the relevant thumbnail
|
|
|
|
img_stream = urlopen(track.thumbUrl)
|
|
|
|
img = io.BytesIO(img_stream.read())
|
|
|
|
|
|
|
|
# Attach to discord embed
|
|
|
|
f = discord.File(img, filename="image0.png")
|
2020-08-09 01:07:21 +02:00
|
|
|
# Get appropiate status message
|
2020-08-05 02:07:29 +02:00
|
|
|
if t == "play":
|
|
|
|
title = f"Now Playing - {track.title}"
|
|
|
|
elif t == "queue":
|
|
|
|
title = f"Added to queue - {track.title}"
|
|
|
|
else:
|
|
|
|
raise ValueError(f"Unsupported type of embed {t}")
|
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-09 08:28:14 +02:00
|
|
|
bot_log.debug(f"Built embed for {track.title}")
|
|
|
|
|
2020-08-05 01:20:13 +02:00
|
|
|
return embed, f
|
2020-07-24 08:41:59 +02:00
|
|
|
|
2020-07-23 06:21:41 +02:00
|
|
|
@command()
|
|
|
|
async def play(self, ctx, *args):
|
2020-08-09 02:08:51 +02:00
|
|
|
# Save the context to use with async callbacks
|
|
|
|
self.ctx = ctx
|
2020-08-05 02:07:29 +02:00
|
|
|
|
2020-08-04 11:59:48 +02:00
|
|
|
if not len(args):
|
2020-08-05 02:07:29 +02:00
|
|
|
await ctx.send(f"Usage: {self.bot_prefix}play TITLE_OF_SONG")
|
2020-08-09 08:28:14 +02:00
|
|
|
bot_log.debug("Failed to play, invalid usage")
|
2020-08-04 11:59:48 +02:00
|
|
|
return
|
|
|
|
|
2020-07-23 06:21:41 +02:00
|
|
|
title = " ".join(args)
|
|
|
|
track = self._search_tracks(title)
|
2020-08-05 02:07:29 +02:00
|
|
|
|
|
|
|
# Fail if song title can't be found
|
|
|
|
if not track:
|
|
|
|
await ctx.send(f"Can't find song: {title}")
|
2020-08-09 08:28:14 +02:00
|
|
|
bot_log.debug(f"Failed to play, can't find song - {title}")
|
2020-08-05 02:07:29 +02:00
|
|
|
return
|
2020-08-09 08:28:14 +02:00
|
|
|
|
2020-08-05 02:07:29 +02:00
|
|
|
# Fail if user not in vc
|
|
|
|
elif not ctx.author.voice:
|
|
|
|
await ctx.send("Join a voice channel first!")
|
2020-08-09 08:28:14 +02:00
|
|
|
bot_log.debug("Failed to play, requester not in voice channel")
|
2020-08-05 02:07:29 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
# Connect to voice if not already
|
|
|
|
if not self.vc:
|
|
|
|
self.vc = await ctx.author.voice.channel.connect()
|
2020-08-09 08:28:14 +02:00
|
|
|
bot_log.debug("Connected to vc.")
|
2020-08-05 02:07:29 +02:00
|
|
|
|
|
|
|
# Specific add to queue message
|
|
|
|
if self.vc.is_playing():
|
2020-08-09 08:28:14 +02:00
|
|
|
bot_log.debug(f"Added to queue - {title}")
|
2020-08-05 02:07:29 +02:00
|
|
|
embed, f = self._build_embed(track, t="queue")
|
|
|
|
await ctx.send(embed=embed, file=f)
|
|
|
|
|
|
|
|
# Add the song to the async queue
|
|
|
|
await self.play_queue.put(track)
|
2020-07-24 08:41:59 +02:00
|
|
|
|
|
|
|
@command()
|
|
|
|
async def stop(self, ctx):
|
|
|
|
if self.vc:
|
|
|
|
self.vc.stop()
|
|
|
|
await self.vc.disconnect()
|
|
|
|
self.vc = 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
|
|
|
|
|
|
|
@command()
|
|
|
|
async def pause(self, ctx):
|
|
|
|
if self.vc:
|
2020-08-05 01:20:13 +02:00
|
|
|
self.vc.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):
|
|
|
|
if self.vc:
|
2020-08-05 01:20:13 +02:00
|
|
|
self.vc.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()
|
|
|
|
async def skip(self, ctx):
|
2020-08-09 08:28:14 +02:00
|
|
|
bot_log.debug("Skip")
|
2020-07-24 08:41:59 +02:00
|
|
|
if self.vc:
|
2020-08-04 11:59:48 +02:00
|
|
|
self.vc.stop()
|
2020-08-09 08:28:14 +02:00
|
|
|
bot_log.debug("Skipped")
|
2020-08-05 02:07:29 +02:00
|
|
|
self._toggle_next()
|
2020-07-27 06:08:00 +02:00
|
|
|
|
|
|
|
@command()
|
|
|
|
async def np(self, ctx):
|
2020-08-05 02:07:29 +02:00
|
|
|
if self.current_track:
|
|
|
|
embed, f = self._build_embed(self.current_track)
|
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:
|
|
|
|
await self.np_message_id.delete()
|
|
|
|
bot_log("Deleted old np status")
|
|
|
|
|
|
|
|
bot_log("Created np status")
|
|
|
|
self.np_message_id = await ctx.send(embed=embed, file=f)
|
2020-08-05 01:20:13 +02:00
|
|
|
|
|
|
|
@command()
|
|
|
|
async def clear(self, ctx):
|
2020-08-05 02:07:29 +02:00
|
|
|
self.play_queue = asyncio.Queue()
|
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.")
|