1
0
Fork 0

Switch greetings to slash commands

This commit is contained in:
Ethanell 2021-01-10 00:13:37 +01:00
parent eb34ebd947
commit 8c1b3af1eb

View file

@ -1,18 +1,13 @@
from discord.ext import commands from discord.ext import commands
from discord import Member, Embed, Forbidden from discord import Member, Embed, Forbidden
from discord.ext.commands import BadArgument from discord_slash import cog_ext, SlashContext, SlashCommandOptionType
from discord_slash.utils import manage_commands
from administrator.check import is_enabled
from administrator.logger import logger from administrator.logger import logger
from administrator import db, config from administrator import db, slash
from administrator.utils import event_is_enabled from administrator.utils import event_is_enabled
def check_greetings_message_type(message_type):
if message_type not in ["join", "leave"]:
raise BadArgument()
extension_name = "greetings" extension_name = "greetings"
logger = logger.getChild(extension_name) logger = logger.getChild(extension_name)
@ -20,67 +15,76 @@ logger = logger.getChild(extension_name)
class Greetings(commands.Cog): class Greetings(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 "Setup join and leave message" return "Setup join and leave message"
@commands.group("greetings", pass_context=True) @cog_ext.cog_subcommand(base="greetings", guild_ids=[693108780434587708], name="help",
@is_enabled() description="Help about greetings")
@commands.guild_only() async def greetings_help(self, ctx: SlashContext):
@commands.has_permissions(manage_guild=True)
async def greetings(self, ctx: commands.Context):
if ctx.invoked_subcommand is None:
await ctx.invoke(self.greetings_help)
@greetings.group("help", pass_context=True)
async def greetings_help(self, ctx: commands.Context):
embed = Embed(title="Greetings help") embed = Embed(title="Greetings help")
embed.add_field(name="set <join/leave> <message>", value="Set the greetings message\n" embed.add_field(name="set <join/leave> <message>", value="Set the greetings message\n"
"`{}` will be replace by the username", "`{}` will be replace by the username",
inline=False) inline=False)
embed.add_field(name="show <join/leave>", value="Show the greetings message", inline=False) embed.add_field(name="show <join/leave>", value="Show the greetings message", inline=False)
embed.add_field(name="toggle <join/leave>", value="Enable or disable the greetings message", inline=False) embed.add_field(name="toggle <join/leave>", value="Enable or disable the greetings message", inline=False)
await ctx.send(embed=embed) await ctx.send(embeds=[embed])
@greetings.group("set", pass_context=True) @cog_ext.cog_subcommand(base="greetings", guild_ids=[693108780434587708], name="set",
async def greetings_set(self, ctx: commands.Context, message_type: str): description="Set the greetings message\n`{}` will be replace by the username",
check_greetings_message_type(message_type) options=[
message = ctx.message.content.replace(config.get("prefix")+"greetings set " + message_type, "").strip() manage_commands.create_option("type", "The join or leave message",
SlashCommandOptionType.STRING, True,
[manage_commands.create_choice("join", "join"),
manage_commands.create_choice("leave", "leave")]),
manage_commands.create_option("message", "The message", SlashCommandOptionType.STRING,
True)
])
async def greetings_set(self, ctx: SlashContext, message_type: str, message: str):
s = db.Session() s = db.Session()
m = s.query(db.Greetings).filter(db.Greetings.guild == ctx.guild.id).first() m = s.query(db.Greetings).filter(db.Greetings.guild == ctx.guild.id).first()
if not m: if not m:
m = db.Greetings(ctx.guild.id) m = db.Greetings(ctx.guild.id)
s.add(m) s.add(m)
setattr(m, message_type+"_enable", True) setattr(m, message_type+"_enable", True)
setattr(m, message_type+"_message", message) setattr(m, message_type+"_message", message.replace("\\n", '\n'))
s.commit() s.commit()
await ctx.message.add_reaction("\U0001f44d") await ctx.send(content="\U0001f44d")
@greetings.group("show", pass_context=True) @cog_ext.cog_subcommand(base="greetings", guild_ids=[693108780434587708], name="show",
async def greetings_show(self, ctx: commands.Context, message_type: str): description="Show the greetings message",
check_greetings_message_type(message_type) options=[manage_commands.create_option("type", "The join or leave message",
SlashCommandOptionType.STRING, True,
[manage_commands.create_choice("join", "join"),
manage_commands.create_choice("leave", "leave")])])
async def greetings_show(self, ctx: SlashContext, message_type: str):
s = db.Session() s = db.Session()
m = s.query(db.Greetings).filter(db.Greetings.guild == ctx.guild.id).first() m = s.query(db.Greetings).filter(db.Greetings.guild == ctx.guild.id).first()
s.close() s.close()
if not m: if not m:
await ctx.send(f"No {message_type} message set !") await ctx.send(content=f"No {message_type} message set !")
else: else:
if message_type == "join": if message_type == "join":
await ctx.send(embed=m.join_embed(ctx.guild.name, str(ctx.message.author))) await ctx.send(embeds=[m.join_embed(ctx.guild.name, str(ctx.author))])
else: else:
await ctx.send(m.leave_msg(str(ctx.message.author))) await ctx.send(content=m.leave_msg(str(ctx.author)))
@greetings.group("toggle", pass_context=True) @cog_ext.cog_subcommand(base="greetings", guild_ids=[693108780434587708], name="toggle",
async def greetings_toggle(self, ctx: commands.Context, message_type: str): description="Enable or disable the greetings message",
check_greetings_message_type(message_type) options=[manage_commands.create_option("type", "The join or leave message",
SlashCommandOptionType.STRING, True,
[manage_commands.create_choice("join", "join"),
manage_commands.create_choice("leave", "leave")])])
async def greetings_toggle(self, ctx: SlashContext, message_type: str):
s = db.Session() s = db.Session()
m = s.query(db.Greetings).filter(db.Greetings.guild == ctx.guild.id).first() m = s.query(db.Greetings).filter(db.Greetings.guild == ctx.guild.id).first()
if not m: if not m:
await ctx.send(f"No {message_type} message set !") await ctx.send(content=f"No {message_type} message set !")
else: else:
setattr(m, message_type+"_enable", not getattr(m, message_type+"_enable")) setattr(m, message_type+"_enable", not getattr(m, message_type+"_enable"))
s.commit() s.commit()
await ctx.send(f"{message_type.title()} message is " + await ctx.send(content=f"{message_type.title()} message is " +
("enable" if getattr(m, message_type+"_enable") else "disable")) ("enable" if getattr(m, message_type+"_enable") else "disable"))
s.close() s.close()
@ -108,6 +112,9 @@ class Greetings(commands.Cog):
if m and m.leave_enable: if m and m.leave_enable:
await member.guild.system_channel.send(m.leave_msg(str(member))) await member.guild.system_channel.send(m.leave_msg(str(member)))
def cog_unload(self):
slash.remove_cog_commands(self)
def setup(bot): def setup(bot):
logger.info(f"Loading...") logger.info(f"Loading...")