Switch presentation to slash commands
This commit is contained in:
parent
f82ae0bb3b
commit
7757847ecb
1 changed files with 31 additions and 29 deletions
|
@ -1,10 +1,13 @@
|
||||||
|
from discord.abc import GuildChannel
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from discord import Embed, Message
|
from discord import Message, Role, TextChannel
|
||||||
from discord.ext.commands import BadArgument
|
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.logger import logger
|
||||||
from administrator import db
|
from administrator import db, slash
|
||||||
from administrator.utils import event_is_enabled
|
from administrator.utils import event_is_enabled
|
||||||
|
|
||||||
extension_name = "presentation"
|
extension_name = "presentation"
|
||||||
|
@ -14,51 +17,50 @@ logger = logger.getChild(extension_name)
|
||||||
class Presentation(commands.Cog):
|
class Presentation(commands.Cog):
|
||||||
def __init__(self, bot: commands.Bot):
|
def __init__(self, bot: commands.Bot):
|
||||||
self.bot = bot
|
self.bot = bot
|
||||||
|
slash.get_cog_commands(self)
|
||||||
|
|
||||||
def description(self):
|
def description(self):
|
||||||
return "Give role to user who make a presentation in a dedicated channel"
|
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()
|
@is_enabled()
|
||||||
@commands.guild_only()
|
@guild_only()
|
||||||
@commands.has_permissions(manage_guild=True)
|
@has_permissions(manage_guild=True)
|
||||||
async def presentation(self, ctx: commands.Context):
|
async def presentation_set(self, ctx: SlashContext, channel: GuildChannel, role: Role):
|
||||||
if ctx.invoked_subcommand is None:
|
if not isinstance(channel, TextChannel):
|
||||||
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:
|
|
||||||
raise BadArgument()
|
raise BadArgument()
|
||||||
s = db.Session()
|
s = db.Session()
|
||||||
p = s.query(db.Presentation).filter(db.Presentation.guild == ctx.guild.id).first()
|
p = s.query(db.Presentation).filter(db.Presentation.guild == ctx.guild.id).first()
|
||||||
if not p:
|
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)
|
s.add(p)
|
||||||
else:
|
else:
|
||||||
p.channel = ctx.message.channel_mentions[0].id
|
p.channel = channel.id
|
||||||
p.role = ctx.message.role_mentions[0].id
|
p.role = role.id
|
||||||
s.commit()
|
s.commit()
|
||||||
await ctx.message.add_reaction("\U0001f44d")
|
await ctx.send(content="\U0001f44d")
|
||||||
|
|
||||||
@presentation.group("disable", pass_context=True)
|
@cog_ext.cog_subcommand(base="presentation", name="disable", description="Disable the auto role give",
|
||||||
async def presentation_disable(self, ctx: commands.Context):
|
guild_ids=[693108780434587708])
|
||||||
|
@is_enabled()
|
||||||
|
@guild_only()
|
||||||
|
@has_permissions(manage_guild=True)
|
||||||
|
async def presentation_disable(self, ctx: SlashContext):
|
||||||
s = db.Session()
|
s = db.Session()
|
||||||
p = s.query(db.Presentation).filter(db.Presentation.guild == ctx.guild.id).first()
|
p = s.query(db.Presentation).filter(db.Presentation.guild == ctx.guild.id).first()
|
||||||
if not p:
|
if not p:
|
||||||
await ctx.send(f"Nothing to disable !")
|
await ctx.send(content="Nothing to disable !")
|
||||||
else:
|
else:
|
||||||
s.delete(p)
|
s.delete(p)
|
||||||
s.commit()
|
s.commit()
|
||||||
await ctx.message.add_reaction("\U0001f44d")
|
await ctx.send(content="\U0001f44d")
|
||||||
s.close()
|
s.close()
|
||||||
|
|
||||||
@commands.Cog.listener()
|
@commands.Cog.listener()
|
||||||
|
|
Reference in a new issue