mirror of
https://github.com/jarulsamy/Plex-Bot.git
synced 2024-08-19 15:01:55 +02:00
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
5c45500450 | |||
2d633acc67 | |||
257ea91c4c | |||
21c6bcc746 | |||
7a8cd631a5 | |||
81e0b50620 | |||
9f9ba0f5e5 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,6 +1,7 @@
|
||||
# Project specific
|
||||
config.yaml
|
||||
config/
|
||||
deploy.sh
|
||||
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
|
@ -5,7 +5,7 @@ FROM python:3.7
|
||||
RUN apt-get -y update
|
||||
RUN apt-get -y upgrade
|
||||
# Install ffmpeg
|
||||
RUN apt-get install -y ffmpeg
|
||||
RUN apt-get install -y --no-install-recommends ffmpeg
|
||||
|
||||
# All source code
|
||||
WORKDIR /src
|
||||
|
15
Jenkinsfile
vendored
15
Jenkinsfile
vendored
@ -35,7 +35,7 @@ pipeline {
|
||||
echo "Style check"
|
||||
sh ''' source /var/lib/jenkins/miniconda3/etc/profile.d/conda.sh
|
||||
conda activate ${BUILD_TAG}
|
||||
pylint CHANGE_ME || true
|
||||
pylint PlexBot || true
|
||||
'''
|
||||
}
|
||||
}
|
||||
@ -49,9 +49,7 @@ pipeline {
|
||||
steps {
|
||||
sh ''' source /var/lib/jenkins/miniconda3/etc/profile.d/conda.sh
|
||||
conda activate ${BUILD_TAG}
|
||||
pwd
|
||||
ls
|
||||
python setup.py bdist_wheel
|
||||
docker build .
|
||||
'''
|
||||
}
|
||||
post {
|
||||
@ -68,14 +66,7 @@ pipeline {
|
||||
post {
|
||||
always {
|
||||
sh 'conda remove --yes -n ${BUILD_TAG} --all'
|
||||
}
|
||||
failure {
|
||||
emailext (
|
||||
subject: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
|
||||
body: """<p>FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
|
||||
<p>Check console output at "<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</p>""",
|
||||
recipientProviders: [[$class: 'DevelopersRecipientProvider']]
|
||||
)
|
||||
sh 'docker system prune -a -f'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import logging
|
||||
from urllib.request import urlopen
|
||||
|
||||
import discord
|
||||
from async_timeout import timeout
|
||||
from discord import FFmpegPCMAudio
|
||||
from discord.ext import commands
|
||||
from discord.ext.commands import command
|
||||
@ -19,11 +20,29 @@ class General(commands.Cog):
|
||||
self.bot = bot
|
||||
|
||||
@command()
|
||||
async def kill(self, ctx):
|
||||
await ctx.send(f"Stopping upon the request of {ctx.author.mention}")
|
||||
async def kill(self, ctx, *args):
|
||||
if "silent" not in args:
|
||||
await ctx.send(f"Stopping upon the request of {ctx.author.mention}")
|
||||
|
||||
await self.bot.close()
|
||||
logger.info(f"Stopping upon the request of {ctx.author.mention}")
|
||||
|
||||
@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.")
|
||||
|
||||
|
||||
class Plex(commands.Cog):
|
||||
def __init__(self, bot, base_url, plex_token, lib_name, bot_prefix) -> None:
|
||||
@ -43,6 +62,8 @@ class Plex(commands.Cog):
|
||||
|
||||
self.vc = None
|
||||
self.current_track = None
|
||||
self.np_message_id = None
|
||||
|
||||
self.play_queue = asyncio.Queue()
|
||||
self.play_next_event = asyncio.Event()
|
||||
|
||||
@ -63,18 +84,12 @@ class Plex(commands.Cog):
|
||||
|
||||
return score[0]
|
||||
|
||||
@command()
|
||||
async def hello(self, ctx, *, member: discord.member = None):
|
||||
member = member or ctx.author
|
||||
await ctx.send(f"Hello {member}")
|
||||
|
||||
async def _play(self):
|
||||
track_url = self.current_track.getStreamURL()
|
||||
|
||||
audio_stream = FFmpegPCMAudio(track_url)
|
||||
|
||||
while self.vc.is_playing():
|
||||
asyncio.sleep(10)
|
||||
asyncio.sleep(2)
|
||||
|
||||
self.vc.play(audio_stream, after=self._toggle_next)
|
||||
|
||||
@ -82,16 +97,29 @@ class Plex(commands.Cog):
|
||||
logger.debug(f"URL: {track_url}")
|
||||
|
||||
embed, f = self._build_embed(self.current_track)
|
||||
await self.ctx.send(embed=embed, file=f)
|
||||
self.np_message_id = await self.ctx.send(embed=embed, file=f)
|
||||
|
||||
async def _audio_player_task(self):
|
||||
while True:
|
||||
self.play_next_event.clear()
|
||||
self.current_track = await self.play_queue.get()
|
||||
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()
|
||||
|
||||
await self._play()
|
||||
await self.play_next_event.wait()
|
||||
await self.np_message_id.delete()
|
||||
|
||||
def _toggle_next(self, error=None):
|
||||
self.current_track = None
|
||||
self.bot.loop.call_soon_threadsafe(self.play_next_event.set)
|
||||
|
||||
def _build_embed(self, track, t="play"):
|
||||
@ -103,6 +131,7 @@ class Plex(commands.Cog):
|
||||
|
||||
# Attach to discord embed
|
||||
f = discord.File(img, filename="image0.png")
|
||||
# Get appropiate status message
|
||||
if t == "play":
|
||||
title = f"Now Playing - {track.title}"
|
||||
elif t == "queue":
|
||||
@ -110,13 +139,13 @@ class Plex(commands.Cog):
|
||||
else:
|
||||
raise ValueError(f"Unsupported type of embed {t}")
|
||||
|
||||
# Include song details
|
||||
descrip = f"{track.album().title} - {track.artist().title}"
|
||||
|
||||
# Build the actual embed
|
||||
embed = discord.Embed(
|
||||
title=title, description=descrip, colour=discord.Color.red()
|
||||
)
|
||||
|
||||
embed.set_author(name="Plex")
|
||||
# Point to file attached with ctx object.
|
||||
embed.set_thumbnail(url="attachment://image0.png")
|
||||
@ -125,6 +154,8 @@ class Plex(commands.Cog):
|
||||
|
||||
@command()
|
||||
async def play(self, ctx, *args):
|
||||
# Save the context to use with async callbacks
|
||||
self.ctx = ctx
|
||||
|
||||
if not len(args):
|
||||
await ctx.send(f"Usage: {self.bot_prefix}play TITLE_OF_SONG")
|
||||
@ -152,8 +183,6 @@ class Plex(commands.Cog):
|
||||
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)
|
||||
|
||||
@ -163,6 +192,7 @@ class Plex(commands.Cog):
|
||||
self.vc.stop()
|
||||
await self.vc.disconnect()
|
||||
self.vc = None
|
||||
self.ctx = None
|
||||
await ctx.send(":stop_button: Stopped")
|
||||
|
||||
@command()
|
||||
|
Reference in New Issue
Block a user