diff --git a/commands/utils.py b/commands/utils.py index a7d8265..fd1df75 100644 --- a/commands/utils.py +++ b/commands/utils.py @@ -22,6 +22,7 @@ class Command(Enum): RELOAD = "reload" RESUME = "resume" SKIP = "skip" + SPONSORBLOCK = "sponsorblock" STATUS = "status" UPTIME = "uptime" VOLUME = "volume" diff --git a/commands/voice/__init__.py b/commands/voice/__init__.py index 530a6ec..a7e1f7b 100644 --- a/commands/voice/__init__.py +++ b/commands/voice/__init__.py @@ -1,18 +1,20 @@ from .channel import join, leave from .playback import fast_forward, pause, playing, resume, volume from .queue import queue_or_play, skip +from .sponsorblock import sponsorblock_command from .utils import remove_queued __all__ = [ + "fast_forward", "join", "leave", - "fast_forward", + "pause", "playing", "queue_or_play", - "skip", - "resume", - "pause", - "skip", "remove_queued", + "resume", + "skip", + "skip", + "sponsorblock_command", "volume", ] diff --git a/commands/voice/sponsorblock.py b/commands/voice/sponsorblock.py new file mode 100644 index 0000000..1330ffb --- /dev/null +++ b/commands/voice/sponsorblock.py @@ -0,0 +1,44 @@ +import disnake + +import sponsorblock +import utils +from constants import EMBED_COLOR +from state import players + +from .utils import command_allowed + + +async def sponsorblock_command(message): + if not command_allowed(message, immutable=True): + return + + if not message.guild.voice_client.source: + await utils.reply(message, "nothing is playing!") + return + + progress = message.guild.voice_client.source.original.progress + video = await sponsorblock.get_segments(players[message.guild.id].current.player.id) + if not video: + await utils.reply( + message, "no sponsorblock segments were found for this video!" + ) + return + + text = [] + for segment in video["segments"]: + begin, end = map(int, segment["segment"]) + category_name = sponsorblock.CATEGORY_NAMES.get(segment["category"]) + + current = "**" if progress >= begin and progress < end else "" + text.append( + f"{current}`{utils.format_duration(begin, short=True)}` - `{utils.format_duration(end, short=True)}`: {category_name if category_name else 'Unknown'}{current}" + ) + + await utils.reply( + message, + embed=disnake.Embed( + title="Sponsorblock segments", + description="\n".join(text), + color=EMBED_COLOR, + ), + ) diff --git a/constants.py b/constants.py index 45df0de..0005335 100644 --- a/constants.py +++ b/constants.py @@ -30,6 +30,7 @@ RELOADABLE_MODULES = [ "commands.voice.playback", "commands.voice.playing", "commands.voice.queue", + "commands.voice.sponsorblock", "commands.voice.utils", "constants", "core", diff --git a/core.py b/core.py index f9b2a9d..0a8bbe3 100644 --- a/core.py +++ b/core.py @@ -146,6 +146,8 @@ async def on_message(message, edited=False): await commands.bot.ping(message) case C.LOOKUP: await commands.tools.lookup(message) + case C.SPONSORBLOCK: + await commands.voice.sponsorblock_command(message) except Exception as e: await utils.reply( message, diff --git a/sponsorblock.py b/sponsorblock.py index cb7d474..b095538 100644 --- a/sponsorblock.py +++ b/sponsorblock.py @@ -4,6 +4,11 @@ import aiohttp from state import sponsorblock_cache +CATEGORY_NAMES = { + "music_offtopic": "non-music", + "sponsor": "sponsored", +} + async def get_segments(videoId: str): if videoId in sponsorblock_cache: