From 5846bb7bac3b9f5da7c58d82080ab20945cb87db Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 23 Jul 2020 21:29:02 +0200 Subject: [PATCH] Add presentation extension --- db/Greetings.py | 2 +- db/Presentation.py | 15 +++++++ db/__init__.py | 1 + extensions/__init__.py | 1 + extensions/presentation.py | 85 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 db/Presentation.py create mode 100644 extensions/presentation.py diff --git a/db/Greetings.py b/db/Greetings.py index 87b528a..902194c 100644 --- a/db/Greetings.py +++ b/db/Greetings.py @@ -11,7 +11,7 @@ class Greetings(Base): join_enable = Column(Boolean, nullable=False, default=False) leave_message = Column(Text, nullable=False, default="") leave_enable = Column(Boolean, nullable=False, default=False) - guild = Column(BigInteger, nullable=False) + guild = Column(BigInteger, nullable=False, unique=True) def __init__(self, guild: int): self.guild = guild diff --git a/db/Presentation.py b/db/Presentation.py new file mode 100644 index 0000000..1fa0799 --- /dev/null +++ b/db/Presentation.py @@ -0,0 +1,15 @@ +from db import Base +from sqlalchemy import Column, Integer, BigInteger + + +class Presentation(Base): + __tablename__ = "presentations" + id = Column(Integer, primary_key=True) + channel = Column(BigInteger, nullable=False) + role = Column(BigInteger, nullable=False) + guild = Column(BigInteger, nullable=False, unique=True) + + def __init__(self, guild: int, channel: int, role: int): + self.guild = guild + self.channel = channel + self.role = role diff --git a/db/__init__.py b/db/__init__.py index a3a0181..7c56617 100644 --- a/db/__init__.py +++ b/db/__init__.py @@ -7,4 +7,5 @@ Session = sessionmaker(bind=engine) Base = declarative_base() from db.Task import Task from db.Greetings import Greetings +from db.Presentation import Presentation Base.metadata.create_all(engine) diff --git a/extensions/__init__.py b/extensions/__init__.py index e3d5bb2..8492917 100644 --- a/extensions/__init__.py +++ b/extensions/__init__.py @@ -6,3 +6,4 @@ bot.load_extension("extensions.purge") bot.load_extension("extensions.poll") bot.load_extension("extensions.reminders") bot.load_extension("extensions.greetings") +bot.load_extension("extensions.presentation") diff --git a/extensions/presentation.py b/extensions/presentation.py new file mode 100644 index 0000000..ab6148e --- /dev/null +++ b/extensions/presentation.py @@ -0,0 +1,85 @@ +from discord.ext import commands +from discord import Embed, Message +from discord.ext.commands import BadArgument + +from administrator.logger import logger +from administrator import db + + +extension_name = "presentation" +logger = logger.getChild(extension_name) + + +class Presentation(commands.Cog): + def __init__(self, bot: commands.Bot): + self.bot = bot + + @commands.group("presentation", pass_context=True) + @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="greetings 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() + 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) + s.add(p) + else: + p.channel = ctx.message.channel_mentions[0].id + p.role = ctx.message.role_mentions[0].id + s.commit() + await ctx.message.add_reaction("\U0001f44d") + + @presentation.group("disable", pass_context=True) + async def presentation_disable(self, ctx: commands.Context): + 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 !") + else: + s.delete(p) + s.commit() + await ctx.message.add_reaction("\U0001f44d") + s.close() + + @commands.Cog.listener() + async def on_message(self, message: Message): + s = db.Session() + p = s.query(db.Presentation).filter(db.Presentation.guild == message.guild.id).first() + s.close() + if p and p.channel == message.channel.id and p.role not in map(lambda x: x.id, message.author.roles): + await message.author.add_roles(message.guild.get_role(p.role), reason="Presentation done") + + +def setup(bot): + logger.info(f"Loading...") + try: + bot.add_cog(Presentation(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("Presentation") + except Exception as e: + logger.error(f"Error unloading: {e}") + else: + logger.info(f"Unload successful")