7 Commits

Author SHA1 Message Date
5c45500450 Merge pull request #5 from jarulsamy/docker
Incremental Updates
2020-08-08 18:45:24 -06:00
2d633acc67 🐛 Reduce dependency load of ffmpeg 2020-08-08 18:44:44 -06:00
257ea91c4c 🐛 Add appropiate steps for docker build 2020-08-08 18:30:12 -06:00
21c6bcc746 Add auto message deletion
Now playing messages now auto delete on song completion.
2020-08-08 18:08:51 -06:00
7a8cd631a5 Merge pull request #4 from jarulsamy/docker
Async Improvements, v0.0.2
2020-08-08 17:21:23 -06:00
81e0b50620 🙈 Deployment script 2020-08-08 17:18:11 -06:00
9f9ba0f5e5 Add auto disconnect on idle
Bot now leaves a voice channel if nothing is playing for more than 15
seconds.
2020-08-08 17:07:21 -06:00
4 changed files with 49 additions and 27 deletions

1
.gitignore vendored
View File

@ -1,6 +1,7 @@
# Project specific
config.yaml
config/
deploy.sh
# Byte-compiled / optimized / DLL files
__pycache__/

View File

@ -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
View File

@ -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 &QUOT;<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>&QUOT;</p>""",
recipientProviders: [[$class: 'DevelopersRecipientProvider']]
)
sh 'docker system prune -a -f'
}
}
}

View File

@ -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()