1
0
Fork 0

Add presentation extension

This commit is contained in:
Ethanell 2020-07-23 21:29:02 +02:00
parent 59656cdbf8
commit 5846bb7bac
5 changed files with 103 additions and 1 deletions

View file

@ -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

15
db/Presentation.py Normal file
View file

@ -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

View file

@ -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)

View file

@ -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")

View file

@ -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")