|
| 1 | +"""Slash commands for managing per-guild playback profiles.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Optional |
| 6 | + |
| 7 | +import discord |
| 8 | +from discord import app_commands |
| 9 | +from discord.ext import commands |
| 10 | + |
| 11 | +from src.services.profile_service import GuildProfileManager |
| 12 | +from src.utils.embeds import EmbedFactory |
| 13 | + |
| 14 | + |
| 15 | +def _manager(bot: commands.Bot) -> GuildProfileManager: |
| 16 | + manager = getattr(bot, "profile_manager", None) |
| 17 | + if not manager: |
| 18 | + raise RuntimeError("GuildProfileManager not initialised on bot.") |
| 19 | + return manager |
| 20 | + |
| 21 | + |
| 22 | +class ProfileCommands(commands.Cog): |
| 23 | + """Expose guild-level configuration toggles for playback behaviour.""" |
| 24 | + |
| 25 | + def __init__(self, bot: commands.Bot): |
| 26 | + self.bot = bot |
| 27 | + |
| 28 | + profile = app_commands.Group( |
| 29 | + name="profile", |
| 30 | + description="Inspect and configure the guild playback profile.", |
| 31 | + guild_only=True, |
| 32 | + ) |
| 33 | + |
| 34 | + # ------------------------------------------------------------------ helpers |
| 35 | + @staticmethod |
| 36 | + def _ensure_manage_guild(inter: discord.Interaction) -> Optional[str]: |
| 37 | + """Verify the invoker has manage_guild permissions.""" |
| 38 | + if not inter.guild: |
| 39 | + return "This command can only be used inside a guild." |
| 40 | + member = inter.guild.get_member(inter.user.id) if isinstance(inter.user, discord.User) else inter.user |
| 41 | + if not isinstance(member, discord.Member): |
| 42 | + return "Unable to resolve invoking member." |
| 43 | + if not member.guild_permissions.manage_guild: |
| 44 | + return "You require the `Manage Server` permission to modify playback profiles." |
| 45 | + return None |
| 46 | + |
| 47 | + @staticmethod |
| 48 | + def _profile_embed(inter: discord.Interaction, profile) -> discord.Embed: |
| 49 | + """Build a concise embed representing the guild profile.""" |
| 50 | + factory = EmbedFactory(inter.guild.id if inter.guild else None) |
| 51 | + embed = factory.primary("Playback Profile") |
| 52 | + embed.add_field(name="Default Volume", value=f"`{profile.default_volume}%`", inline=True) |
| 53 | + embed.add_field(name="Autoplay", value="✅ Enabled" if profile.autoplay else "❌ Disabled", inline=True) |
| 54 | + embed.add_field(name="Announcement Style", value=f"`{profile.announcement_style}`", inline=True) |
| 55 | + embed.set_footer(text="Use /profile commands to adjust these defaults.") |
| 56 | + return embed |
| 57 | + |
| 58 | + # ------------------------------------------------------------------ slash commands |
| 59 | + @profile.command(name="show", description="Display the current playback profile for this guild.") |
| 60 | + async def show(self, inter: discord.Interaction): |
| 61 | + profile = _manager(self.bot).get(inter.guild.id) # type: ignore[union-attr] |
| 62 | + await inter.response.send_message(embed=self._profile_embed(inter, profile), ephemeral=True) |
| 63 | + |
| 64 | + @profile.command(name="set-volume", description="Set the default playback volume for this guild.") |
| 65 | + @app_commands.describe(level="Volume percent to apply automatically (0-200).") |
| 66 | + async def set_volume(self, inter: discord.Interaction, level: app_commands.Range[int, 0, 200]): |
| 67 | + if (error := self._ensure_manage_guild(inter)) is not None: |
| 68 | + return await inter.response.send_message(error, ephemeral=True) |
| 69 | + manager = _manager(self.bot) |
| 70 | + profile = manager.update(inter.guild.id, volume=level) # type: ignore[union-attr] |
| 71 | + |
| 72 | + player = self.bot.lavalink.player_manager.get(inter.guild.id) # type: ignore[union-attr] |
| 73 | + if player: |
| 74 | + await player.set_volume(profile.default_volume) |
| 75 | + |
| 76 | + await inter.response.send_message( |
| 77 | + embed=self._profile_embed(inter, profile), |
| 78 | + ephemeral=True, |
| 79 | + ) |
| 80 | + |
| 81 | + @profile.command(name="set-autoplay", description="Enable or disable autoplay when the queue finishes.") |
| 82 | + async def set_autoplay(self, inter: discord.Interaction, enabled: bool): |
| 83 | + if (error := self._ensure_manage_guild(inter)) is not None: |
| 84 | + return await inter.response.send_message(error, ephemeral=True) |
| 85 | + manager = _manager(self.bot) |
| 86 | + profile = manager.update(inter.guild.id, autoplay=enabled) # type: ignore[union-attr] |
| 87 | + |
| 88 | + player = self.bot.lavalink.player_manager.get(inter.guild.id) # type: ignore[union-attr] |
| 89 | + if player: |
| 90 | + player.store("autoplay_enabled", profile.autoplay) |
| 91 | + |
| 92 | + await inter.response.send_message( |
| 93 | + embed=self._profile_embed(inter, profile), |
| 94 | + ephemeral=True, |
| 95 | + ) |
| 96 | + |
| 97 | + @profile.command(name="set-announcement", description="Choose how now-playing messages are displayed.") |
| 98 | + @app_commands.describe(style="Select between rich embeds or minimal text notifications.") |
| 99 | + @app_commands.choices( |
| 100 | + style=[ |
| 101 | + app_commands.Choice(name="Rich Embed", value="rich"), |
| 102 | + app_commands.Choice(name="Minimal Text", value="minimal"), |
| 103 | + ] |
| 104 | + ) |
| 105 | + async def set_announcement(self, inter: discord.Interaction, style: app_commands.Choice[str]): |
| 106 | + if (error := self._ensure_manage_guild(inter)) is not None: |
| 107 | + return await inter.response.send_message(error, ephemeral=True) |
| 108 | + manager = _manager(self.bot) |
| 109 | + profile = manager.update(inter.guild.id, announcement_style=style.value) # type: ignore[union-attr] |
| 110 | + |
| 111 | + player = self.bot.lavalink.player_manager.get(inter.guild.id) # type: ignore[union-attr] |
| 112 | + if player: |
| 113 | + player.store("announcement_style", profile.announcement_style) |
| 114 | + |
| 115 | + await inter.response.send_message( |
| 116 | + embed=self._profile_embed(inter, profile), |
| 117 | + ephemeral=True, |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | +async def setup(bot: commands.Bot): |
| 122 | + await bot.add_cog(ProfileCommands(bot)) |
0 commit comments