mirror of
https://github.com/jarulsamy/Plex-Bot.git
synced 2024-08-19 15:01:55 +02:00
Compare commits
No commits in common. "360416bb02e27a821c63254d94b72e7931ac9369" and "19360e3101e1bbfc29dfe59e2c960777238c80f0" have entirely different histories.
360416bb02
...
19360e3101
279
PlexBot/bot.py
279
PlexBot/bot.py
@ -34,15 +34,10 @@ Plex:
|
|||||||
show_playlists <ARG> <ARG> - Query for playlists with a name matching any of the arguments.
|
show_playlists <ARG> <ARG> - Query for playlists with a name matching any of the arguments.
|
||||||
lyrics - Print the lyrics of the song (Requires Genius API)
|
lyrics - Print the lyrics of the song (Requires Genius API)
|
||||||
np - Print the current playing song.
|
np - Print the current playing song.
|
||||||
q - Print the current queue (This can take very long!)
|
|
||||||
stop - Halt playback and leave vc.
|
stop - Halt playback and leave vc.
|
||||||
loop - Loop the current song.
|
|
||||||
loopq - Loop the current queue.
|
|
||||||
unloop - Disable looping the current track.
|
|
||||||
unloopq - Disable looping the current queue.
|
|
||||||
pause - Pause playback.
|
pause - Pause playback.
|
||||||
resume - Resume playback.
|
resume - Resume playback.
|
||||||
skip - Skip the current song. Give a number as argument to skip more than 1.
|
skip - Skip the current song.
|
||||||
clear - Clear play queue.
|
clear - Clear play queue.
|
||||||
|
|
||||||
[] - Optional args.
|
[] - Optional args.
|
||||||
@ -207,10 +202,7 @@ class Plex(commands.Cog):
|
|||||||
# Initialize necessary vars
|
# Initialize necessary vars
|
||||||
self.voice_channel = None
|
self.voice_channel = None
|
||||||
self.current_track = None
|
self.current_track = None
|
||||||
self.is_looping = False
|
|
||||||
self.loop_queue = None
|
|
||||||
self.np_message_id = None
|
self.np_message_id = None
|
||||||
self.show_queue_message_ids = []
|
|
||||||
self.ctx = None
|
self.ctx = None
|
||||||
|
|
||||||
# Initialize events
|
# Initialize events
|
||||||
@ -304,41 +296,15 @@ class Plex(commands.Cog):
|
|||||||
track_url = self.current_track.getStreamURL()
|
track_url = self.current_track.getStreamURL()
|
||||||
audio_stream = FFmpegPCMAudio(track_url)
|
audio_stream = FFmpegPCMAudio(track_url)
|
||||||
|
|
||||||
while self.voice_channel and self.voice_channel.is_playing():
|
while self.voice_channel.is_playing():
|
||||||
bot_log.debug("waiting for track to finish")
|
asyncio.sleep(2)
|
||||||
await asyncio.sleep(2)
|
|
||||||
bot_log.debug("track finished")
|
|
||||||
|
|
||||||
if self.voice_channel:
|
self.voice_channel.play(audio_stream, after=self._toggle_next)
|
||||||
self.voice_channel.play(audio_stream, after=self._toggle_next)
|
|
||||||
|
|
||||||
plex_log.debug("%s - URL: %s", self.current_track, track_url)
|
plex_log.debug("%s - URL: %s", self.current_track, track_url)
|
||||||
|
|
||||||
embed, img = self._build_embed_track(self.current_track)
|
embed, img = self._build_embed_track(self.current_track)
|
||||||
self.np_message_id = await self.ctx.send(embed=embed, file=img)
|
self.np_message_id = await self.ctx.send(embed=embed, file=img)
|
||||||
|
|
||||||
async def _play_next(self):
|
|
||||||
try:
|
|
||||||
from asyncio.exceptions import CancelledError
|
|
||||||
except ImportError:
|
|
||||||
from concurrent.futures._base import CancelledError
|
|
||||||
if self.is_looping:
|
|
||||||
self.current_track = self.is_looping
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
self.current_track = await self.play_queue.get()
|
|
||||||
except CancelledError:
|
|
||||||
bot_log.debug("failed to pop queue")
|
|
||||||
|
|
||||||
if not self.current_track and self.loop_queue:
|
|
||||||
bot_log.debug("swapping loop queue and play queue")
|
|
||||||
for item in self.loop_queue:
|
|
||||||
await self.play_queue.put(item)
|
|
||||||
|
|
||||||
try:
|
|
||||||
self.current_track = await self.play_queue.get()
|
|
||||||
except CancelledError:
|
|
||||||
bot_log.debug("failed to pop queue")
|
|
||||||
|
|
||||||
async def _audio_player_task(self):
|
async def _audio_player_task(self):
|
||||||
"""
|
"""
|
||||||
@ -363,19 +329,17 @@ class Plex(commands.Cog):
|
|||||||
try:
|
try:
|
||||||
# Disconnect after 15 seconds idle
|
# Disconnect after 15 seconds idle
|
||||||
async with timeout(15):
|
async with timeout(15):
|
||||||
await self._play_next()
|
self.current_track = await self.play_queue.get()
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
bot_log("timeout - disconnecting")
|
|
||||||
await self.voice_channel.disconnect()
|
await self.voice_channel.disconnect()
|
||||||
self.voice_channel = None
|
self.voice_channel = None
|
||||||
|
|
||||||
if not self.current_track:
|
if not self.current_track:
|
||||||
await self._play_next()
|
self.current_track = await self.play_queue.get()
|
||||||
|
|
||||||
await self._play()
|
await self._play()
|
||||||
await self.play_next_event.wait()
|
await self.play_next_event.wait()
|
||||||
if self.np_message_id:
|
await self.np_message_id.delete()
|
||||||
await self.np_message_id.delete()
|
|
||||||
|
|
||||||
def _toggle_next(self, error=None):
|
def _toggle_next(self, error=None):
|
||||||
"""
|
"""
|
||||||
@ -417,22 +381,16 @@ class Plex(commands.Cog):
|
|||||||
ValueError: Unsupported type of embed {type_}
|
ValueError: Unsupported type of embed {type_}
|
||||||
"""
|
"""
|
||||||
# Grab the relevant thumbnail
|
# Grab the relevant thumbnail
|
||||||
if track.thumbUrl:
|
img_stream = requests.get(track.thumbUrl, stream=True).raw
|
||||||
img_stream = requests.get(track.thumbUrl, stream=True).raw
|
img = io.BytesIO(img_stream.read())
|
||||||
img = io.BytesIO(img_stream.read())
|
|
||||||
|
|
||||||
# Attach to discord embed
|
|
||||||
art_file = discord.File(img, filename="image0.png")
|
|
||||||
else:
|
|
||||||
art_file = None
|
|
||||||
|
|
||||||
|
# Attach to discord embed
|
||||||
|
art_file = discord.File(img, filename="image0.png")
|
||||||
# Get appropiate status message
|
# Get appropiate status message
|
||||||
if type_ == "play":
|
if type_ == "play":
|
||||||
title = f"Now Playing - {track.title}"
|
title = f"Now Playing - {track.title}"
|
||||||
elif type_ == "queue":
|
elif type_ == "queue":
|
||||||
title = f"Added to queue - {track.title}"
|
title = f"Added to queue - {track.title}"
|
||||||
elif type_ == "queued":
|
|
||||||
title = f"Next in line - {track.title}"
|
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Unsupported type of embed {type_}")
|
raise ValueError(f"Unsupported type of embed {type_}")
|
||||||
|
|
||||||
@ -547,11 +505,8 @@ class Plex(commands.Cog):
|
|||||||
|
|
||||||
# Connect to voice if not already
|
# Connect to voice if not already
|
||||||
if not self.voice_channel:
|
if not self.voice_channel:
|
||||||
try:
|
self.voice_channel = await ctx.author.voice.channel.connect()
|
||||||
self.voice_channel = await ctx.author.voice.channel.connect()
|
bot_log.debug("Connected to vc.")
|
||||||
bot_log.debug("Connected to vc.")
|
|
||||||
except asyncio.exceptions.TimeoutError:
|
|
||||||
bot_log.debug("Cannot connect to vc - timeout")
|
|
||||||
|
|
||||||
@command()
|
@command()
|
||||||
async def play(self, ctx, *args):
|
async def play(self, ctx, *args):
|
||||||
@ -588,7 +543,7 @@ class Plex(commands.Cog):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
# Specific add to queue message
|
# Specific add to queue message
|
||||||
if self.voice_channel and self.voice_channel.is_playing():
|
if self.voice_channel.is_playing():
|
||||||
bot_log.debug("Added to queue - %s", title)
|
bot_log.debug("Added to queue - %s", title)
|
||||||
embed, img = self._build_embed_track(track, type_="queue")
|
embed, img = self._build_embed_track(track, type_="queue")
|
||||||
await ctx.send(embed=embed, file=img)
|
await ctx.send(embed=embed, file=img)
|
||||||
@ -637,37 +592,6 @@ class Plex(commands.Cog):
|
|||||||
for track in album.tracks():
|
for track in album.tracks():
|
||||||
await self.play_queue.put(track)
|
await self.play_queue.put(track)
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
@command()
|
@command()
|
||||||
async def playlist(self, ctx, *args):
|
async def playlist(self, ctx, *args):
|
||||||
"""
|
"""
|
||||||
@ -689,32 +613,30 @@ class Plex(commands.Cog):
|
|||||||
# Save the context to use with async callbacks
|
# Save the context to use with async callbacks
|
||||||
self.ctx = ctx
|
self.ctx = ctx
|
||||||
title = " ".join(args)
|
title = " ".join(args)
|
||||||
await self.play_playlist(title)
|
|
||||||
|
|
||||||
@command()
|
try:
|
||||||
async def playlist_shuffle(self, ctx, *args):
|
playlist = self._search_playlists(title)
|
||||||
"""
|
except MediaNotFoundError:
|
||||||
User command to play playlist in shuffle mode
|
await ctx.send(f"Can't find playlist: {title}")
|
||||||
|
bot_log.debug("Failed to queue playlist, can't find - %s", title)
|
||||||
|
return
|
||||||
|
|
||||||
Searchs plex db and either, initiates playback, or
|
try:
|
||||||
adds to queue. Handles invalid usage from the user.
|
await self._validate(ctx)
|
||||||
|
except VoiceChannelError:
|
||||||
Args:
|
pass
|
||||||
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)
|
|
||||||
await self.play_playlist(title,shuffle=True)
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
embed, img = self._build_embed_playlist(self, playlist, "Added playlist to queue", playlist.title)
|
||||||
|
await ctx.send(embed=embed, file=img)
|
||||||
|
|
||||||
|
for item in playlist.items():
|
||||||
|
if (item.TYPE == "track"):
|
||||||
|
await self.play_queue.put(item)
|
||||||
|
bot_log.debug("Added to queue - %s", title)
|
||||||
|
except MediaNotFoundError:
|
||||||
|
await ctx.send(message="Playlist "+title+" seems to be empty!")
|
||||||
|
bot_log.debug("Playlist empty - %s", title)
|
||||||
@command()
|
@command()
|
||||||
async def show_playlists(self, ctx, *args):
|
async def show_playlists(self, ctx, *args):
|
||||||
"""
|
"""
|
||||||
@ -775,80 +697,6 @@ class Plex(commands.Cog):
|
|||||||
bot_log.debug("Stopped")
|
bot_log.debug("Stopped")
|
||||||
await ctx.send(":stop_button: Stopped")
|
await ctx.send(":stop_button: Stopped")
|
||||||
|
|
||||||
@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
|
|
||||||
|
|
||||||
@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")
|
|
||||||
if self.current_track:
|
|
||||||
self.loop_queue = [ self.current_track ]
|
|
||||||
else:
|
|
||||||
self.loop_queue = []
|
|
||||||
for item in self.play_queue._queue:
|
|
||||||
self.loop_queue.append(item)
|
|
||||||
|
|
||||||
@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
|
|
||||||
"""
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
@command()
|
@command()
|
||||||
async def pause(self, ctx):
|
async def pause(self, ctx):
|
||||||
"""
|
"""
|
||||||
@ -891,7 +739,7 @@ class Plex(commands.Cog):
|
|||||||
await ctx.send(":play_pause: Resumed")
|
await ctx.send(":play_pause: Resumed")
|
||||||
|
|
||||||
@command()
|
@command()
|
||||||
async def skip(self, ctx, *args):
|
async def skip(self, ctx):
|
||||||
"""
|
"""
|
||||||
User command to skip song in queue
|
User command to skip song in queue
|
||||||
|
|
||||||
@ -907,16 +755,10 @@ class Plex(commands.Cog):
|
|||||||
Raises:
|
Raises:
|
||||||
None
|
None
|
||||||
"""
|
"""
|
||||||
n = 1
|
bot_log.debug("Skip")
|
||||||
if args:
|
|
||||||
n = int(args[0])
|
|
||||||
bot_log.debug("Skipping "+str(n))
|
|
||||||
if self.voice_channel:
|
if self.voice_channel:
|
||||||
self.voice_channel.stop()
|
self.voice_channel.stop()
|
||||||
bot_log.debug("Skipped")
|
bot_log.debug("Skipped")
|
||||||
if n>1:
|
|
||||||
for i in range(n-1):
|
|
||||||
await self.play_queue.get()
|
|
||||||
self._toggle_next()
|
self._toggle_next()
|
||||||
|
|
||||||
@command(name="np")
|
@command(name="np")
|
||||||
@ -937,51 +779,15 @@ class Plex(commands.Cog):
|
|||||||
None
|
None
|
||||||
"""
|
"""
|
||||||
if self.current_track:
|
if self.current_track:
|
||||||
embed, img = self._build_embed_track(self.current_track,type_="play")
|
embed, img = self._build_embed_track(self.current_track)
|
||||||
bot_log.debug("Now playing")
|
bot_log.debug("Now playing")
|
||||||
if self.np_message_id:
|
if self.np_message_id:
|
||||||
try:
|
await self.np_message_id.delete()
|
||||||
await self.np_message_id.delete()
|
bot_log.debug("Deleted old np status")
|
||||||
bot_log.debug("Deleted old np status")
|
|
||||||
except discord.errors.NotFound:
|
|
||||||
pass
|
|
||||||
|
|
||||||
bot_log.debug("Created np status")
|
bot_log.debug("Created np status")
|
||||||
self.np_message_id = await ctx.send(embed=embed, file=img)
|
self.np_message_id = await ctx.send(embed=embed, file=img)
|
||||||
|
|
||||||
@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))
|
|
||||||
|
|
||||||
@command()
|
@command()
|
||||||
async def clear(self, ctx):
|
async def clear(self, ctx):
|
||||||
"""
|
"""
|
||||||
@ -997,7 +803,6 @@ class Plex(commands.Cog):
|
|||||||
None
|
None
|
||||||
"""
|
"""
|
||||||
self.play_queue = asyncio.Queue()
|
self.play_queue = asyncio.Queue()
|
||||||
self.loop_queue = None
|
|
||||||
bot_log.debug("Cleared queue")
|
bot_log.debug("Cleared queue")
|
||||||
await ctx.send(":boom: Queue cleared.")
|
await ctx.send(":boom: Queue cleared.")
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user