2020-07-23 21:27:34 +02:00
|
|
|
from discord.ext import commands
|
2020-07-30 11:25:01 +02:00
|
|
|
from discord import Member, Embed, Forbidden
|
2020-07-23 21:27:34 +02:00
|
|
|
from discord.ext.commands import BadArgument
|
|
|
|
|
2020-11-05 14:42:16 +01:00
|
|
|
from administrator.check import is_enabled
|
2020-07-23 21:27:34 +02:00
|
|
|
from administrator.logger import logger
|
|
|
|
from administrator import db, config
|
2020-11-05 14:42:16 +01:00
|
|
|
from administrator.utils import event_is_enabled
|
2020-07-23 21:27:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
def check_greetings_message_type(message_type):
|
|
|
|
if message_type not in ["join", "leave"]:
|
|
|
|
raise BadArgument()
|
|
|
|
|
|
|
|
|
|
|
|
extension_name = "greetings"
|
|
|
|
logger = logger.getChild(extension_name)
|
|
|
|
|
|
|
|
|
|
|
|
class Greetings(commands.Cog):
|
|
|
|
def __init__(self, bot: commands.Bot):
|
|
|
|
self.bot = bot
|
|
|
|
|
2020-07-23 21:34:32 +02:00
|
|
|
def description(self):
|
|
|
|
return "Setup join and leave message"
|
|
|
|
|
2020-07-23 21:27:34 +02:00
|
|
|
@commands.group("greetings", pass_context=True)
|
2020-11-05 14:42:16 +01:00
|
|
|
@is_enabled()
|
2020-07-23 21:27:34 +02:00
|
|
|
@commands.guild_only()
|
|
|
|
@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):
|
2020-07-23 21:30:42 +02:00
|
|
|
embed = Embed(title="Greetings help")
|
2020-07-23 21:27:34 +02:00
|
|
|
embed.add_field(name="set <join/leave> <message>", value="Set the greetings message\n"
|
|
|
|
"`{}` will be replace by the username",
|
|
|
|
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)
|
|
|
|
await ctx.send(embed=embed)
|
|
|
|
|
|
|
|
@greetings.group("set", pass_context=True)
|
|
|
|
async def greetings_set(self, ctx: commands.Context, message_type: str):
|
|
|
|
check_greetings_message_type(message_type)
|
|
|
|
message = ctx.message.content.replace(config.get("prefix")+"greetings set " + message_type, "").strip()
|
|
|
|
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)
|
|
|
|
setattr(m, message_type+"_message", message)
|
|
|
|
s.commit()
|
|
|
|
await ctx.message.add_reaction("\U0001f44d")
|
|
|
|
|
|
|
|
@greetings.group("show", pass_context=True)
|
|
|
|
async def greetings_show(self, ctx: commands.Context, message_type: str):
|
|
|
|
check_greetings_message_type(message_type)
|
|
|
|
s = db.Session()
|
|
|
|
m = s.query(db.Greetings).filter(db.Greetings.guild == ctx.guild.id).first()
|
|
|
|
s.close()
|
|
|
|
if not m:
|
|
|
|
await ctx.send(f"No {message_type} message set !")
|
|
|
|
else:
|
|
|
|
if message_type == "join":
|
|
|
|
await ctx.send(embed=m.join_embed(ctx.guild.name, str(ctx.message.author)))
|
|
|
|
else:
|
|
|
|
await ctx.send(m.leave_msg(str(ctx.message.author)))
|
|
|
|
|
|
|
|
@greetings.group("toggle", pass_context=True)
|
|
|
|
async def greetings_toggle(self, ctx: commands.Context, message_type: str):
|
|
|
|
check_greetings_message_type(message_type)
|
|
|
|
s = db.Session()
|
|
|
|
m = s.query(db.Greetings).filter(db.Greetings.guild == ctx.guild.id).first()
|
|
|
|
if not m:
|
|
|
|
await ctx.send(f"No {message_type} message set !")
|
|
|
|
else:
|
|
|
|
setattr(m, message_type+"_enable", not getattr(m, message_type+"_enable"))
|
|
|
|
s.commit()
|
|
|
|
await ctx.send(f"{message_type.title()} message is " +
|
|
|
|
("enable" if getattr(m, message_type+"_enable") else "disable"))
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
@commands.Cog.listener()
|
|
|
|
async def on_member_join(self, member: Member):
|
|
|
|
s = db.Session()
|
2020-11-05 14:42:16 +01:00
|
|
|
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:
|
2020-07-30 11:25:01 +02:00
|
|
|
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()
|
2020-11-05 14:42:16 +01:00
|
|
|
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)))
|
|
|
|
|
|
|
|
|
|
|
|
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")
|