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-07-27 06:08:00 +02:00
|
|
|
logger = logging.getLogger("PlexBot")
|
|
|
|
|
2020-07-23 06:21:41 +02:00
|
|
|
|
|
|
|
class General(commands.Cog):
|
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
|
|
|
|
|
|
|
@command()
|
2020-07-24 08:41:59 +02:00
|
|
|
async def kill(self, ctx):
|
2020-07-23 06:21:41 +02:00
|
|
|
await ctx.send(f"Stopping upon the request of {ctx.author.mention}")
|
|
|
|
await self.bot.close()
|
2020-07-27 06:08:00 +02:00
|
|
|
logger.info(f"Stopping upon the request of {ctx.author.mention}")
|
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:
|
|
|
|
logger.fatal("Invalid Plex token, stopping...")
|
|
|
|
raise Unauthorized("Invalid Plex token")
|
|
|
|
|
2020-07-23 06:21:41 +02:00
|
|
|
self.music = self.pms.library.section(self.library_name)
|
|
|
|
|
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-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
|
|
|
|
|
|
|
logger.info("Started bot successfully")
|
|
|
|
|
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
|
|
|
|
|
|
|
@command()
|
|
|
|
async def hello(self, ctx, *, member: discord.member = None):
|
|
|
|
member = member or ctx.author
|
|
|
|
await ctx.send(f"Hello {member}")
|
|
|
|
|
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-05 02:07:29 +02:00
|
|
|
logger.debug(f"Playing {self.current_track.title}")
|
|
|
|
logger.debug(f"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)
|
|
|
|
await self.ctx.send(embed=embed, file=f)
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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-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-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}")
|
|
|
|
return
|
|
|
|
# Fail if user not in vc
|
|
|
|
elif not ctx.author.voice:
|
|
|
|
await ctx.send("Join a voice channel first!")
|
|
|
|
return
|
|
|
|
|
|
|
|
# Connect to voice if not already
|
|
|
|
if not self.vc:
|
|
|
|
self.vc = await ctx.author.voice.channel.connect()
|
|
|
|
logger.debug("Connected to vc.")
|
|
|
|
|
|
|
|
# Specific add to queue message
|
|
|
|
if self.vc.is_playing():
|
|
|
|
embed, f = self._build_embed(track, t="queue")
|
|
|
|
await ctx.send(embed=embed, file=f)
|
|
|
|
|
|
|
|
# Save the context to use with async callbacks
|
|
|
|
self.ctx = ctx
|
|
|
|
# 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-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-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-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-04 11:59:48 +02:00
|
|
|
logger.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-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)
|
|
|
|
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()
|
|
|
|
await ctx.send(":boom: Queue cleared.")
|