1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
Administrator-py/extensions/greetings.py

136 lines
5.9 KiB
Python
Raw Normal View History

2020-07-23 21:27:34 +02:00
from discord.ext import commands
from discord import Member, Embed, Forbidden
2021-01-10 00:13:37 +01:00
from discord_slash import cog_ext, SlashContext, SlashCommandOptionType
from discord_slash.utils import manage_commands
2020-07-23 21:27:34 +02:00
2021-02-03 14:46:22 +01:00
from administrator.check import is_enabled, guild_only, has_permissions
2020-07-23 21:27:34 +02:00
from administrator.logger import logger
2021-01-10 00:13:37 +01:00
from administrator import db, slash
from administrator.utils import event_is_enabled
2020-07-23 21:27:34 +02:00
extension_name = "greetings"
logger = logger.getChild(extension_name)
class Greetings(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
2021-01-10 00:13:37 +01:00
slash.get_cog_commands(self)
2020-07-23 21:27:34 +02:00
def description(self):
return "Setup join and leave message"
2021-02-03 14:46:22 +01:00
@cog_ext.cog_subcommand(base="greetings", name="set",
2021-01-10 00:13:37 +01:00
description="Set the greetings message\n`{}` will be replace by the username",
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")]),
manage_commands.create_option("message", "The message", SlashCommandOptionType.STRING,
True)
])
2021-02-03 14:46:22 +01:00
@is_enabled()
@guild_only()
@has_permissions(manage_guild=True)
2021-01-10 00:13:37 +01:00
async def greetings_set(self, ctx: SlashContext, message_type: str, message: str):
2020-07-23 21:27:34 +02:00
s = db.Session()
m = s.query(db.Greetings).filter(db.Greetings.guild == ctx.guild.id).first()
if not m:
m = db.Greetings(ctx.guild.id)
s.add(m)
setattr(m, message_type+"_enable", True)
2021-01-10 00:13:37 +01:00
setattr(m, message_type+"_message", message.replace("\\n", '\n'))
2020-07-23 21:27:34 +02:00
s.commit()
2021-01-10 00:13:37 +01:00
await ctx.send(content="\U0001f44d")
2021-02-03 14:46:22 +01:00
@cog_ext.cog_subcommand(base="greetings", name="show",
2021-01-10 00:13:37 +01:00
description="Show the greetings message",
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")])])
2021-02-03 14:46:22 +01:00
@is_enabled()
@guild_only()
@has_permissions(manage_guild=True)
2021-01-10 00:13:37 +01:00
async def greetings_show(self, ctx: SlashContext, message_type: str):
2020-07-23 21:27:34 +02:00
s = db.Session()
m = s.query(db.Greetings).filter(db.Greetings.guild == ctx.guild.id).first()
s.close()
if not m:
2021-01-10 00:13:37 +01:00
await ctx.send(content=f"No {message_type} message set !")
2020-07-23 21:27:34 +02:00
else:
if message_type == "join":
2021-01-10 00:13:37 +01:00
await ctx.send(embeds=[m.join_embed(ctx.guild.name, str(ctx.author))])
2020-07-23 21:27:34 +02:00
else:
2021-01-10 00:13:37 +01:00
await ctx.send(content=m.leave_msg(str(ctx.author)))
2021-02-03 14:46:22 +01:00
@cog_ext.cog_subcommand(base="greetings", name="toggle",
2021-01-10 00:13:37 +01:00
description="Enable or disable the greetings message",
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")])])
2021-02-03 14:46:22 +01:00
@is_enabled()
@guild_only()
@has_permissions(manage_guild=True)
2021-01-10 00:13:37 +01:00
async def greetings_toggle(self, ctx: SlashContext, message_type: str):
2020-07-23 21:27:34 +02:00
s = db.Session()
m = s.query(db.Greetings).filter(db.Greetings.guild == ctx.guild.id).first()
if not m:
2021-01-10 00:13:37 +01:00
await ctx.send(content=f"No {message_type} message set !")
2020-07-23 21:27:34 +02:00
else:
setattr(m, message_type+"_enable", not getattr(m, message_type+"_enable"))
s.commit()
2021-01-10 00:13:37 +01:00
await ctx.send(content=f"{message_type.title()} message is " +
("enable" if getattr(m, message_type+"_enable") else "disable"))
2020-07-23 21:27:34 +02:00
s.close()
@commands.Cog.listener()
async def on_member_join(self, member: Member):
s = db.Session()
if not event_is_enabled(self.qualified_name, member.guild.id, s):
return
2020-07-23 21:27:34 +02:00
m = s.query(db.Greetings).filter(db.Greetings.guild == member.guild.id).first()
s.close()
if m and m.join_enable:
embed = m.join_embed(member.guild.name, str(member))
try:
await member.send(embed=embed)
except Forbidden:
await member.guild.system_channel.send(member.mention, embed=embed)
2020-07-23 21:27:34 +02:00
@commands.Cog.listener()
async def on_member_remove(self, member: Member):
s = db.Session()
if not event_is_enabled(self.qualified_name, member.guild.id, s):
return
2020-07-23 21:27:34 +02:00
m = s.query(db.Greetings).filter(db.Greetings.guild == member.guild.id).first()
s.close()
if m and m.leave_enable:
await member.guild.system_channel.send(m.leave_msg(str(member)))
2021-01-10 00:13:37 +01:00
def cog_unload(self):
slash.remove_cog_commands(self)
2020-07-23 21:27:34 +02:00
def setup(bot):
logger.info(f"Loading...")
try:
bot.add_cog(Greetings(bot))
except Exception as e:
logger.error(f"Error loading: {e}")
else:
logger.info(f"Load successful")
def teardown(bot):
logger.info(f"Unloading...")
try:
bot.remove_cog("Greetings")
except Exception as e:
logger.error(f"Error unloading: {e}")
else:
logger.info(f"Unload successful")