Merge pull request #4 from jarulsamy/docker

Async Improvements, v0.0.2
This commit is contained in:
Joshua Arulsamy 2020-08-08 17:21:23 -06:00 committed by GitHub
commit 7a8cd631a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 4 deletions

1
.gitignore vendored
View File

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

View File

@ -4,6 +4,7 @@ import logging
from urllib.request import urlopen from urllib.request import urlopen
import discord import discord
from async_timeout import timeout
from discord import FFmpegPCMAudio from discord import FFmpegPCMAudio
from discord.ext import commands from discord.ext import commands
from discord.ext.commands import command from discord.ext.commands import command
@ -70,11 +71,10 @@ class Plex(commands.Cog):
async def _play(self): async def _play(self):
track_url = self.current_track.getStreamURL() track_url = self.current_track.getStreamURL()
audio_stream = FFmpegPCMAudio(track_url) audio_stream = FFmpegPCMAudio(track_url)
while self.vc.is_playing(): while self.vc.is_playing():
asyncio.sleep(10) asyncio.sleep(2)
self.vc.play(audio_stream, after=self._toggle_next) self.vc.play(audio_stream, after=self._toggle_next)
@ -87,11 +87,23 @@ class Plex(commands.Cog):
async def _audio_player_task(self): async def _audio_player_task(self):
while True: while True:
self.play_next_event.clear() self.play_next_event.clear()
if self.vc:
try:
# Disconnect after 15 seconds idle
async with timeout(15):
self.current_track = await self.play_queue.get() 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()
await self.play_next_event.wait() await self.play_next_event.wait()
def _toggle_next(self, error=None): def _toggle_next(self, error=None):
self.current_track = None
self.bot.loop.call_soon_threadsafe(self.play_next_event.set) self.bot.loop.call_soon_threadsafe(self.play_next_event.set)
def _build_embed(self, track, t="play"): def _build_embed(self, track, t="play"):
@ -103,6 +115,7 @@ class Plex(commands.Cog):
# Attach to discord embed # Attach to discord embed
f = discord.File(img, filename="image0.png") f = discord.File(img, filename="image0.png")
# Get appropiate status message
if t == "play": if t == "play":
title = f"Now Playing - {track.title}" title = f"Now Playing - {track.title}"
elif t == "queue": elif t == "queue":
@ -110,13 +123,13 @@ class Plex(commands.Cog):
else: else:
raise ValueError(f"Unsupported type of embed {t}") raise ValueError(f"Unsupported type of embed {t}")
# Include song details
descrip = f"{track.album().title} - {track.artist().title}" descrip = f"{track.album().title} - {track.artist().title}"
# Build the actual embed # Build the actual embed
embed = discord.Embed( embed = discord.Embed(
title=title, description=descrip, colour=discord.Color.red() title=title, description=descrip, colour=discord.Color.red()
) )
embed.set_author(name="Plex") embed.set_author(name="Plex")
# Point to file attached with ctx object. # Point to file attached with ctx object.
embed.set_thumbnail(url="attachment://image0.png") embed.set_thumbnail(url="attachment://image0.png")
@ -163,6 +176,7 @@ class Plex(commands.Cog):
self.vc.stop() self.vc.stop()
await self.vc.disconnect() await self.vc.disconnect()
self.vc = None self.vc = None
self.ctx = None
await ctx.send(":stop_button: Stopped") await ctx.send(":stop_button: Stopped")
@command() @command()