Merge branch '10-embeds' into 'develop'
Resolve "Use embeds" Closes #10 See merge request ekzyis/musicube!9
This commit is contained in:
commit
a0ac98f796
33
src/bot.py
33
src/bot.py
|
@ -1,6 +1,7 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
import discord
|
import discord
|
||||||
from discord.ext import commands, tasks
|
from discord.ext import commands, tasks
|
||||||
|
@ -8,6 +9,7 @@ from dotenv import load_dotenv
|
||||||
import youtube_dl
|
import youtube_dl
|
||||||
|
|
||||||
from error import ErrorHandler
|
from error import ErrorHandler
|
||||||
|
from message import NowPlayingMessage, QueuedMessage
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
|
@ -15,6 +17,13 @@ load_dotenv()
|
||||||
youtube_dl.utils.bug_reports_message = lambda: ''
|
youtube_dl.utils.bug_reports_message = lambda: ''
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Song:
|
||||||
|
title: str
|
||||||
|
webpage_url: str
|
||||||
|
audio_url: str
|
||||||
|
|
||||||
|
|
||||||
class Music(commands.Cog):
|
class Music(commands.Cog):
|
||||||
def __init__(self, bot):
|
def __init__(self, bot):
|
||||||
self._bot = bot
|
self._bot = bot
|
||||||
|
@ -49,8 +58,8 @@ class Music(commands.Cog):
|
||||||
async def _handle_playback(self):
|
async def _handle_playback(self):
|
||||||
while True:
|
while True:
|
||||||
await self._queue_lock.acquire()
|
await self._queue_lock.acquire()
|
||||||
ctx, url, title = await self._queue.get()
|
ctx, song = await self._queue.get()
|
||||||
audio = discord.FFmpegPCMAudio(url, **self._ffmpeg_options)
|
audio = discord.FFmpegPCMAudio(song.audio_url, **self._ffmpeg_options)
|
||||||
|
|
||||||
def after(err):
|
def after(err):
|
||||||
if err:
|
if err:
|
||||||
|
@ -58,7 +67,9 @@ class Music(commands.Cog):
|
||||||
self._queue.task_done()
|
self._queue.task_done()
|
||||||
self._queue_lock.release()
|
self._queue_lock.release()
|
||||||
ctx.voice_client.play(audio, after=after)
|
ctx.voice_client.play(audio, after=after)
|
||||||
await ctx.send(f"Now playing: {title}")
|
|
||||||
|
embed = NowPlayingMessage(title=song.title, url=song.webpage_url)
|
||||||
|
await ctx.send(embed=embed)
|
||||||
|
|
||||||
@_handle_playback.before_loop
|
@_handle_playback.before_loop
|
||||||
async def before_handle_playback(self):
|
async def before_handle_playback(self):
|
||||||
|
@ -70,17 +81,21 @@ class Music(commands.Cog):
|
||||||
data = self._ytdl.extract_info(query, download=False)
|
data = self._ytdl.extract_info(query, download=False)
|
||||||
if 'entries' in data:
|
if 'entries' in data:
|
||||||
data = data['entries'][0]
|
data = data['entries'][0]
|
||||||
title = data.get('title')
|
song = Song(
|
||||||
url = data.get('url')
|
title=data.get('title'),
|
||||||
await self._queue.put((ctx, url, title))
|
audio_url=data.get('url'),
|
||||||
|
webpage_url=data.get('webpage_url')
|
||||||
|
)
|
||||||
|
await self._queue.put((ctx, song))
|
||||||
if ctx.voice_client.is_playing():
|
if ctx.voice_client.is_playing():
|
||||||
await ctx.send(f"Queued: {title}")
|
embed = QueuedMessage(title=song.title, url=song.webpage_url)
|
||||||
|
await ctx.send(embed=embed)
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
async def skip(self, ctx):
|
async def skip(self, ctx):
|
||||||
async with ctx.typing():
|
async with ctx.typing():
|
||||||
if ctx.voice_client is None or not ctx.voice_client.is_playing():
|
if ctx.voice_client is None or not ctx.voice_client.is_playing():
|
||||||
raise commands.CommandError("Cannot skip: No song playing")
|
raise commands.CommandError("No song playing")
|
||||||
ctx.voice_client.stop()
|
ctx.voice_client.stop()
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
|
@ -93,7 +108,7 @@ class Music(commands.Cog):
|
||||||
if ctx.author.voice:
|
if ctx.author.voice:
|
||||||
await ctx.author.voice.channel.connect()
|
await ctx.author.voice.channel.connect()
|
||||||
else:
|
else:
|
||||||
raise commands.CommandError("Author not connected to a voice channel.")
|
raise commands.CommandError("Author not connected to a voice channel")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
|
|
||||||
|
from message import ErrorMessage
|
||||||
|
|
||||||
|
|
||||||
class ErrorHandler(commands.Cog):
|
class ErrorHandler(commands.Cog):
|
||||||
"""A cog for global error handling."""
|
"""A cog for global error handling."""
|
||||||
|
@ -20,7 +22,8 @@ class ErrorHandler(commands.Cog):
|
||||||
else:
|
else:
|
||||||
message = "Oh no! Something went wrong while running the command!"
|
message = "Oh no! Something went wrong while running the command!"
|
||||||
|
|
||||||
await ctx.send(message)
|
embed = ErrorMessage(message)
|
||||||
|
await ctx.send(embed=embed)
|
||||||
|
|
||||||
|
|
||||||
def setup(bot: commands.Bot):
|
def setup(bot: commands.Bot):
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
import discord
|
||||||
|
|
||||||
|
|
||||||
|
class BotMessage(discord.Embed):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ErrorMessage(BotMessage):
|
||||||
|
def __init__(self, message):
|
||||||
|
super().__init__(
|
||||||
|
title=f"Error: {message}",
|
||||||
|
color=discord.Color.red()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class NowPlayingMessage(BotMessage):
|
||||||
|
def __init__(self, title, url):
|
||||||
|
super().__init__(
|
||||||
|
title=f"Now playing: {title}",
|
||||||
|
description=url,
|
||||||
|
color=discord.Color.green()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class QueuedMessage(BotMessage):
|
||||||
|
def __init__(self, title, url):
|
||||||
|
super().__init__(
|
||||||
|
title=f"Queued: {title}",
|
||||||
|
description=url,
|
||||||
|
color=discord.Color.blue()
|
||||||
|
)
|
|
@ -64,9 +64,8 @@ async def test_bot_playback(mbot, ctx):
|
||||||
assert \
|
assert \
|
||||||
ctx.voice_client.play.call_args.args == (deliver_us_audio,), \
|
ctx.voice_client.play.call_args.args == (deliver_us_audio,), \
|
||||||
"Did not playback correct audio"
|
"Did not playback correct audio"
|
||||||
assert \
|
embed = ctx.send.call_args.kwargs["embed"]
|
||||||
ctx.send.call_args.args == (f"Now playing: {title}",), \
|
assert embed.title == f"Now playing: {title}", "Did not send 'Now playing:' message"
|
||||||
"Did not send 'Now playing:' message"
|
|
||||||
|
|
||||||
# TEST: Following songs are put inside a queue
|
# TEST: Following songs are put inside a queue
|
||||||
ctx.voice_client.is_playing.return_value = True
|
ctx.voice_client.is_playing.return_value = True
|
||||||
|
@ -86,9 +85,8 @@ async def test_bot_playback(mbot, ctx):
|
||||||
assert \
|
assert \
|
||||||
not ctx.voice_client.play.call_args.args == (time_of_dying_audio,), \
|
not ctx.voice_client.play.call_args.args == (time_of_dying_audio,), \
|
||||||
"Did immediately playback audio instead of being queued"
|
"Did immediately playback audio instead of being queued"
|
||||||
assert \
|
embed = ctx.send.call_args.kwargs["embed"]
|
||||||
ctx.send.call_args.args == (f"Queued: {title}",), \
|
assert embed.title == f"Queued: {title}", "Did not send 'Queued:' message"
|
||||||
"Did not send 'Queued:' message"
|
|
||||||
await asyncio.sleep(0)
|
await asyncio.sleep(0)
|
||||||
# Assert that there is still no playback because previous song is not finished yet
|
# Assert that there is still no playback because previous song is not finished yet
|
||||||
assert \
|
assert \
|
||||||
|
|
Loading…
Reference in New Issue