Created command groups to regroup commands

This commit is contained in:
Elnath 2021-01-12 00:49:20 +01:00
parent bdaecd96cd
commit dbe330cf64
1 changed files with 38 additions and 9 deletions

View File

@ -93,11 +93,33 @@ class VocalMaisBot(commands.Cog):
raise discord_utils.CheckFailDoNotNotify
return True
async def subcommand_invoked_without_command(self, ctx: commands.Context):
"""
This functions is a possible implementation for a command group created with invoke_without_command = True.
It assumes that the group was called without a subcommand or with a non-existing subcommand.
This function notifies the user, then raises a discord_utils.CheckFailDoNotNotify exception.
"""
command_elements = [str(cmd) for cmd in ctx.command.parents] + [ctx.command.name] # In case of nested groups
help_message = f"Type `@{self.bot.user.display_name} help {' '.join(command_elements)}` for help"
if ctx.subcommand_passed is None: # The user did not pass a subcommand
await ctx.send(f":x: Subcommand expected. {help_message}")
else:
await ctx.send(f":x: The requested subcommand does not exist. {help_message}")
raise discord_utils.CheckFailDoNotNotify()
@commands.command(help = "See if I am alive")
async def ping(self, ctx: commands.Context):
await ctx.send(":ping_pong:")
@commands.command("register", help = "Make me watch a voice channel")
@commands.group(
"guild",
help = "Commands for server admins",
invoke_without_command = True,
)
async def guild_subcommands(self, ctx: commands.Context):
await self.subcommand_invoked_without_command(ctx)
@guild_subcommands.command("register", help = "Make me watch a voice channel")
async def register_channel(self, ctx: commands.Context, channel_id: int):
await self.check_is_administrator_or_owner(ctx)
@ -113,7 +135,7 @@ class VocalMaisBot(commands.Cog):
else:
await ctx.send(":thumbsup: I was already watching this channel")
@commands.command("forget", help = "Make me stop watching a voice channel")
@guild_subcommands.command("forget", help = "Make me stop watching a voice channel")
async def forget_channel(self, ctx: commands.Context, channel_id: int):
await self.check_is_administrator_or_owner(ctx)
@ -130,7 +152,7 @@ class VocalMaisBot(commands.Cog):
else:
await ctx.send(f":thumbsup: I already was not watching {channel_name}")
@commands.command("list", help = "List watched channels")
@guild_subcommands.command("list", help = "List watched channels")
async def list_watched_channels(self, ctx: commands.Context):
watched_channels_ids = self.channels_config.get_watched_channels_ids(ctx.guild)
if len(watched_channels_ids) > 0:
@ -145,16 +167,23 @@ class VocalMaisBot(commands.Cog):
else:
await ctx.send(f":see_no_evil: I am not watching any voice channel! Do not hesitate to add some by running `@{self.bot.user.display_name} {self.register_channel.name} {self.register_channel.signature}`")
@commands.command("clear", help = "Make me stop watching all channels")
@guild_subcommands.command("clear", help = "Make me stop watching all channels")
async def clear_watched_channels(self, ctx: commands.Context):
await self.check_is_administrator_or_owner(ctx)
self.channels_config.clear_watched_channels(ctx.guild)
await ctx.send(":white_check_mark: I am no longer watching any channel")
@commands.command(
"set_name",
brief = "Set the default name for your voice channels",
@commands.group(
"name",
help = "Commands to set a default name for your channels!",
invoke_without_command = True,
)
async def default_channel_name_subcommands(self, ctx: commands.Context):
await self.subcommand_invoked_without_command(ctx)
@default_channel_name_subcommands.command(
"set",
help = "Set the default name for your voice channels. If you put {user} in the name, it will be replaced by your nickname",
usage = "<name>"
)
@ -175,7 +204,7 @@ class VocalMaisBot(commands.Cog):
# so we do not want the users to get their hopes up by seeing markdown working in the message
await ctx.send(f":white_check_mark: The default name for your channels has been set! If you create one now, it will appear as {discord_utils.voice_channel_safe_name(name, user_name_replacement = ctx.author.display_name, escape_markdown = True)}")
@commands.command("get_name", help = "Get the default name for your voice channels")
@default_channel_name_subcommands.command("get", help = "Get the default name for your voice channels")
async def get_user_default_channel_name(self, ctx: commands.Context):
channel_name = self.channels_config.get_user_channel_name(ctx.guild, ctx.author)
if channel_name is None:
@ -184,7 +213,7 @@ class VocalMaisBot(commands.Cog):
channel_name = discord_utils.voice_channel_safe_name(channel_name, user_name_replacement = ctx.author.display_name, escape_markdown = True)
await ctx.send(f":memo: If you create a voice channel right now, it will be named like this: {channel_name}")
@commands.command("clear_name", help = "Do not use a special name for your voice channels")
@default_channel_name_subcommands.command("clear", help = "Do not use a special name for your voice channels")
async def clear_user_default_channel_name(self, ctx: commands.Context):
self.channels_config.clear_user_channel_name(ctx.guild, ctx.author)
self.channels_config.save_to_file()