Skip to content

Commit

Permalink
feat: update services system
Browse files Browse the repository at this point in the history
  • Loading branch information
Dysta committed Jan 15, 2025
1 parent 7a417ca commit ee040e3
Show file tree
Hide file tree
Showing 18 changed files with 167 additions and 120 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.10-bookworm
FROM python:3.11-bookworm

WORKDIR /app

Expand Down
2 changes: 1 addition & 1 deletion .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ services:

volumes:
# Update this to wherever you want VS Code to mount the folder of your project
- ..:/app:cached
- ..:/app

# Uncomment the next four lines if you will use a ptrace-based debugger like C++, Go, and Rust.
# cap_add:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python-unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:

strategy:
matrix:
python-version: ['3.9', '3.10', '3.11', '3.12']
python-version: ['3.9', '3.10', '3.11']

steps:
- name: Checkout
Expand Down
7 changes: 7 additions & 0 deletions jukebot/abstract_components/abstract_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
class AbstractService:
"""This class allow you to implement your own service and using it from the bot.
To implement a service, create a new file in the `services` package then implement this class.
Don't forget, services are named `XXXService` where `XXX` is your action.
To register a service, go to the cog where it's used then in the setup function, add your service by using `bot.add_service(XXXService())`.
To use a service: `bot.services.XXX` where `XXX` is your previous named action.
"""

def __init__(self, bot):
self.bot = bot

Expand Down
48 changes: 25 additions & 23 deletions jukebot/cogs/music.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

from disnake import APISlashCommand, CommandInteraction, Embed, Forbidden
from disnake.ext import commands
from disnake.ext.commands import Bot, BucketType
from disnake.ext.commands import BucketType

from jukebot import JukeBot
from jukebot.components.player import Player
from jukebot.components.requests import ShazamRequest
from jukebot.exceptions import QueryFailed
Expand All @@ -27,7 +28,7 @@

class Music(commands.Cog):
def __init__(self, bot):
self.bot: Bot = bot
self.bot: JukeBot = bot

@commands.slash_command()
@commands.cooldown(1, 5.0, BucketType.user)
Expand All @@ -52,8 +53,7 @@ async def play(self, inter: CommandInteraction, query: str, top: Optional[bool]
if not inter.response.is_done():
await inter.response.defer()

with PlayService(self.bot) as play:
song, loop = await play(interaction=inter, query=query, top=top)
song, loop = await self.bot.services.play(interaction=inter, query=query, top=top)

e: Embed = embed.music_message(song, loop)
await inter.edit_original_message(embed=e)
Expand All @@ -71,8 +71,7 @@ async def leave(self, inter: CommandInteraction):
inter : CommandInteraction
The interaction
"""
with LeaveService(self.bot) as leave:
await leave(guild_id=inter.guild.id)
await self.bot.services.leave(guild_id=inter.guild.id)

e = embed.basic_message(title="Player disconnected")
await inter.send(embed=e)
Expand All @@ -91,8 +90,7 @@ async def stop(self, inter: CommandInteraction):
inter : CommandInteraction
The interaction
"""
with StopService(self.bot) as stop:
await stop(guild_id=inter.guild.id)
await self.bot.services.stop(guild_id=inter.guild.id)

e = embed.basic_message(title="Player stopped")
await inter.send(embed=e)
Expand All @@ -111,8 +109,7 @@ async def pause(self, inter: CommandInteraction):
inter : CommandInteraction
The interaction
"""
with PauseService(self.bot) as pause:
await pause(guild_id=inter.guild.id)
await self.bot.services.pause(guild_id=inter.guild.id)

e = embed.basic_message(title="Player paused")
await inter.send(embed=e)
Expand All @@ -137,8 +134,7 @@ async def resume(self, inter: CommandInteraction):
await self.bot.get_slash_command("play").callback(self, inter, query="")
return

with ResumeService(self.bot) as resume:
ok = await resume(guild_id=inter.guild.id)
ok = await self.bot.services.resume(guild_id=inter.guild.id)

if ok:
e = embed.basic_message(title="Player resumed")
Expand All @@ -163,8 +159,7 @@ async def current(self, inter: CommandInteraction):
inter : CommandInteraction
The interaction
"""
with CurrentSongService(self.bot) as current_song:
song, stream, loop = await current_song(inter.guild.id)
song, stream, loop = await self.bot.services.current_song(guild_id=inter.guild.id)

if stream and song:
e = embed.music_message(song, loop, stream.progress)
Expand All @@ -188,8 +183,7 @@ async def join(self, inter: CommandInteraction):
inter : CommandInteraction
The interaction
"""
with JoinService(self.bot) as join:
await join(interaction=inter)
await self.bot.services.join(interaction=inter)

e = embed.basic_message(
content=f"Connected to <#{inter.author.voice.channel.id}>\n" f"Bound to <#{inter.channel.id}>\n",
Expand All @@ -210,8 +204,7 @@ async def skip(self, inter: CommandInteraction):
inter : CommandInteraction
The interaction
"""
with SkipService(self.bot) as skip:
await skip(guild_id=inter.guild.id)
await self.bot.services.skip(guild_id=inter.guild.id)

e: embed = embed.basic_message(title="Skipped !")
await inter.send(embed=e)
Expand All @@ -230,8 +223,7 @@ async def grab(self, inter: CommandInteraction):
inter : CommandInteraction
The interaction
"""
with GrabService(self.bot) as grab:
song, stream = await grab(interaction=inter)
song, stream = await self.bot.services.grab(guild_id=inter.guild.id)

e = embed.grab_message(song, stream.progress)
e.add_field(
Expand Down Expand Up @@ -265,8 +257,7 @@ async def loop(
- queue (loop the current queue)
- none (disable looping)
"""
with LoopService(self.bot) as loop:
new_status = await loop(interaction=inter, mode=mode)
new_status = await self.bot.services.loop(guild_id=inter.guild.id, mode=mode)

e: embed = embed.basic_message(title=new_status)
await inter.send(embed=e)
Expand Down Expand Up @@ -336,5 +327,16 @@ async def share(self, inter: CommandInteraction, url: str):
await inter.edit_original_message(embed=e)


def setup(bot):
def setup(bot: JukeBot):
bot.add_cog(Music(bot))

bot.add_service(GrabService(bot))
bot.add_service(JoinService(bot))
bot.add_service(LeaveService(bot))
bot.add_service(LoopService(bot))
bot.add_service(PauseService(bot))
bot.add_service(PlayService(bot))
bot.add_service(ResumeService(bot))
bot.add_service(SkipService(bot))
bot.add_service(StopService(bot))
bot.add_service(CurrentSongService(bot))
58 changes: 30 additions & 28 deletions jukebot/cogs/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

from disnake import CommandInteraction, Embed
from disnake.ext import commands
from disnake.ext.commands import Bot, BucketType
from disnake.ext.commands import BucketType

from jukebot import JukeBot
from jukebot.components.requests.music_request import MusicRequest
from jukebot.services.queue import (
AddService,
Expand All @@ -22,7 +23,7 @@

class Queue(commands.Cog):
def __init__(self, bot):
self.bot: Bot = bot
self.bot: JukeBot = bot

@commands.slash_command()
async def queue(self, inter: CommandInteraction):
Expand All @@ -46,8 +47,7 @@ async def show(self, inter: CommandInteraction):
inter : CommandInteraction
The interaction
"""
with ShowService(self.bot) as show:
queue: ResultSet = await show(guild_id=inter.guild.id)
queue: ResultSet = await self.bot.services.show(guild_id=inter.guild.id)

e: Embed = embed.queue_message(queue, self.bot, title=f"Queue for {inter.guild.name}")
await inter.send(embed=e)
Expand All @@ -74,8 +74,7 @@ async def add(
if not inter.response.is_done():
await inter.response.defer()

with AddService(self.bot) as add:
type, res = await add(guild_id=inter.guild.id, author=inter.author, query=query, top=top)
type, res = await self.bot.services.add(guild_id=inter.guild.id, author=inter.author, query=query, top=top)

if type == MusicRequest.ResultType.PLAYLIST:
e: Embed = embed.basic_queue_message(content=f"Enqueued : {len(res)} songs")
Expand All @@ -89,58 +88,55 @@ async def add(
@commands.check(checks.bot_and_user_in_same_channel)
@commands.check(checks.bot_is_connected)
@commands.check(checks.user_is_connected)
async def remove(self, inter: CommandInteraction, song: str):
"""Remove a song from the queue
async def clear(self, inter: CommandInteraction):
"""Remove all music from the current queue
Parameters
----------
inter: The interaction
song: The name of the music to remove. The bot auto completes the answer
inter : CommandInteraction
The interaction
"""
with RemoveService(self.bot) as remove:
elem: Result = await remove(guild_id=inter.guild.id, song=song)
await self.bot.services.clear(guild_id=inter.guild.id)

e: Embed = embed.basic_message(content=f"`{elem.title}` have been removed from the queue")
e: Embed = embed.basic_message(title="The queue have been cleared.")
await inter.send(embed=e)

@queue.sub_command()
@commands.cooldown(1, 5.0, BucketType.user)
@commands.check(checks.bot_queue_is_not_empty)
@commands.check(checks.bot_and_user_in_same_channel)
@commands.check(checks.bot_is_connected)
@commands.check(checks.user_is_connected)
async def clear(self, inter: CommandInteraction):
"""Remove all music from the current queue
@commands.cooldown(1, 10.0, BucketType.guild)
async def shuffle(self, inter: CommandInteraction):
"""Shuffles the current queue
Parameters
----------
inter : CommandInteraction
The interaction
"""
with ClearService(self.bot) as clear:
await clear(guild_id=inter.guild.id)
await self.bot.services.shuffle(guild_id=inter.guild.id)

e: Embed = embed.basic_message(title="The queue have been cleared.")
e: Embed = embed.basic_message(title="Queue shuffled.")
await inter.send(embed=e)

@queue.sub_command()
@commands.cooldown(1, 5.0, BucketType.user)
@commands.check(checks.bot_queue_is_not_empty)
@commands.check(checks.bot_and_user_in_same_channel)
@commands.check(checks.bot_is_connected)
@commands.check(checks.user_is_connected)
@commands.cooldown(1, 10.0, BucketType.guild)
async def shuffle(self, inter: CommandInteraction):
"""Shuffles the current queue
async def remove(self, inter: CommandInteraction, song: str):
"""Remove a song from the queue
Parameters
----------
inter : CommandInteraction
The interaction
inter: The interaction
song: The name of the music to remove. The bot auto completes the answer
"""
with ShuffleService(self.bot) as shuffle:
await shuffle(guild_id=inter.guild.id)
elem: Result = await self.bot.services.remove(guild_id=inter.guild.id, song=song)

e: Embed = embed.basic_message(title="Queue shuffled.")
e: Embed = embed.basic_message(content=f"`{elem.title}` have been removed from the queue")
await inter.send(embed=e)

@remove.autocomplete("song")
Expand All @@ -150,5 +146,11 @@ async def remove_autocomplete(self, inter: CommandInteraction, data: str):
return [e.title for e in queue if data in e.title.lower()][:25]


def setup(bot):
def setup(bot: JukeBot):
bot.add_cog(Queue(bot))

bot.add_service(AddService(bot))
bot.add_service(ClearService(bot))
bot.add_service(RemoveService(bot))
bot.add_service(ShuffleService(bot))
bot.add_service(ShowService(bot))
10 changes: 4 additions & 6 deletions jukebot/cogs/radio.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@
from disnake.ext.commands import BucketType
from loguru import logger

from jukebot.services.music import PlayService
from jukebot import JukeBot
from jukebot.utils import checks, converter, embed

if TYPE_CHECKING:
from disnake import Embed
from disnake.ext.commands import Bot


class Radio(commands.Cog):
def __init__(self, bot):
self.bot: Bot = bot
self.bot: JukeBot = bot
self._radios: dict = {}

async def cog_load(self) -> None:
Expand All @@ -27,8 +26,7 @@ async def cog_load(self) -> None:
async def _radio_process(self, inter: CommandInteraction, choices: list):
query: str = random.choice(choices)
logger.opt(lazy=True).debug(f"Choice is {query}")
with PlayService(self.bot) as ps:
await ps(interaction=inter, query=query, top=True)
await self.bot.services.play(interaction=inter, query=query, top=True)

@commands.slash_command(description="Launch a random radio")
@commands.cooldown(1, 5.0, BucketType.user)
Expand All @@ -47,5 +45,5 @@ async def radio_autocomplete(self, inter: CommandInteraction, query: str):
return [e for e in self._radios.keys() if query in e.lower()][:25]


def setup(bot):
def setup(bot: JukeBot):
bot.add_cog(Radio(bot))
Loading

0 comments on commit ee040e3

Please sign in to comment.