From 7757847ecb894ca50a06881d2fc01f2e8ff2632e Mon Sep 17 00:00:00 2001 From: flifloo Date: Wed, 3 Feb 2021 22:47:05 +0100 Subject: [PATCH] Switch presentation to slash commands --- extensions/presentation.py | 60 ++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/extensions/presentation.py b/extensions/presentation.py index ffdaee5..bd9cfb7 100644 --- a/extensions/presentation.py +++ b/extensions/presentation.py @@ -1,10 +1,13 @@ +from discord.abc import GuildChannel from discord.ext import commands -from discord import Embed, Message +from discord import Message, Role, TextChannel from discord.ext.commands import BadArgument +from discord_slash import cog_ext, SlashCommandOptionType, SlashContext +from discord_slash.utils import manage_commands -from administrator.check import is_enabled +from administrator.check import is_enabled, guild_only, has_permissions from administrator.logger import logger -from administrator import db +from administrator import db, slash from administrator.utils import event_is_enabled extension_name = "presentation" @@ -14,51 +17,50 @@ logger = logger.getChild(extension_name) class Presentation(commands.Cog): def __init__(self, bot: commands.Bot): self.bot = bot + slash.get_cog_commands(self) def description(self): return "Give role to user who make a presentation in a dedicated channel" - @commands.group("presentation", pass_context=True) + @cog_ext.cog_subcommand(base="presentation", name="set", + description="Set the presentation channel and the role to give", + options=[ + manage_commands.create_option("channel", "The presentation channel", + SlashCommandOptionType.CHANNEL, True), + manage_commands.create_option("role", "The role to give", + SlashCommandOptionType.ROLE, True) + ]) @is_enabled() - @commands.guild_only() - @commands.has_permissions(manage_guild=True) - async def presentation(self, ctx: commands.Context): - if ctx.invoked_subcommand is None: - await ctx.invoke(self.presentation_help) - - @presentation.group("help", pass_context=True) - async def presentation_help(self, ctx: commands.Context): - embed = Embed(title="Presentation help", description="Give a role to a new member after a presentation") - embed.add_field(name="set <#channel> <@role>", value="Set the presentation channel and the role to give", - inline=False) - embed.add_field(name="disable", value="Disable the auto role give", inline=False) - await ctx.send(embed=embed) - - @presentation.group("set", pass_context=True) - async def presentation_set(self, ctx: commands.Context): - if len(ctx.message.channel_mentions) != 1 and not len(ctx.message.role_mentions) != 1: + @guild_only() + @has_permissions(manage_guild=True) + async def presentation_set(self, ctx: SlashContext, channel: GuildChannel, role: Role): + if not isinstance(channel, TextChannel): raise BadArgument() s = db.Session() p = s.query(db.Presentation).filter(db.Presentation.guild == ctx.guild.id).first() if not p: - p = db.Presentation(ctx.guild.id, ctx.message.channel_mentions[0].id, ctx.message.role_mentions[0].id) + p = db.Presentation(ctx.guild.id, channel.id, role.id) s.add(p) else: - p.channel = ctx.message.channel_mentions[0].id - p.role = ctx.message.role_mentions[0].id + p.channel = channel.id + p.role = role.id s.commit() - await ctx.message.add_reaction("\U0001f44d") + await ctx.send(content="\U0001f44d") - @presentation.group("disable", pass_context=True) - async def presentation_disable(self, ctx: commands.Context): + @cog_ext.cog_subcommand(base="presentation", name="disable", description="Disable the auto role give", + guild_ids=[693108780434587708]) + @is_enabled() + @guild_only() + @has_permissions(manage_guild=True) + async def presentation_disable(self, ctx: SlashContext): s = db.Session() p = s.query(db.Presentation).filter(db.Presentation.guild == ctx.guild.id).first() if not p: - await ctx.send(f"Nothing to disable !") + await ctx.send(content="Nothing to disable !") else: s.delete(p) s.commit() - await ctx.message.add_reaction("\U0001f44d") + await ctx.send(content="\U0001f44d") s.close() @commands.Cog.listener()