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/presentation.py

96 lines
3.6 KiB
Python
Raw Normal View History

2021-02-03 22:47:05 +01:00
from discord.abc import GuildChannel
2020-07-23 21:29:02 +02:00
from discord.ext import commands
2021-02-03 22:47:05 +01:00
from discord import Message, Role, TextChannel
2020-07-23 21:29:02 +02:00
from discord.ext.commands import BadArgument
2021-02-03 22:47:05 +01:00
from discord_slash import cog_ext, SlashCommandOptionType, SlashContext
from discord_slash.utils import manage_commands
2020-07-23 21:29:02 +02:00
2021-02-03 22:47:05 +01:00
from administrator.check import is_enabled, guild_only, has_permissions
2020-07-23 21:29:02 +02:00
from administrator.logger import logger
2021-02-03 22:47:05 +01:00
from administrator import db, slash
from administrator.utils import event_is_enabled
2020-07-23 21:29:02 +02:00
extension_name = "presentation"
logger = logger.getChild(extension_name)
class Presentation(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
2021-02-03 22:47:05 +01:00
slash.get_cog_commands(self)
2020-07-23 21:29:02 +02:00
def description(self):
return "Give role to user who make a presentation in a dedicated channel"
2021-02-03 22:47:05 +01:00
@cog_ext.cog_subcommand(base="presentation", name="set",
description="Set the presentation channel and the role to give",
options=[
manage_commands.create_option("channel", "The presentation channel",
SlashCommandOptionType.CHANNEL, True),
manage_commands.create_option("role", "The role to give",
SlashCommandOptionType.ROLE, True)
])
@is_enabled()
2021-02-03 22:47:05 +01:00
@guild_only()
@has_permissions(manage_guild=True)
async def presentation_set(self, ctx: SlashContext, channel: GuildChannel, role: Role):
if not isinstance(channel, TextChannel):
2020-07-23 21:29:02 +02:00
raise BadArgument()
s = db.Session()
p = s.query(db.Presentation).filter(db.Presentation.guild == ctx.guild.id).first()
if not p:
2021-02-03 22:47:05 +01:00
p = db.Presentation(ctx.guild.id, channel.id, role.id)
2020-07-23 21:29:02 +02:00
s.add(p)
else:
2021-02-03 22:47:05 +01:00
p.channel = channel.id
p.role = role.id
2020-07-23 21:29:02 +02:00
s.commit()
2021-02-03 22:47:05 +01:00
await ctx.send(content="\U0001f44d")
2020-07-23 21:29:02 +02:00
2021-02-03 22:47:05 +01:00
@cog_ext.cog_subcommand(base="presentation", name="disable", description="Disable the auto role give",
guild_ids=[693108780434587708])
@is_enabled()
@guild_only()
@has_permissions(manage_guild=True)
async def presentation_disable(self, ctx: SlashContext):
2020-07-23 21:29:02 +02:00
s = db.Session()
p = s.query(db.Presentation).filter(db.Presentation.guild == ctx.guild.id).first()
if not p:
2021-02-03 22:47:05 +01:00
await ctx.send(content="Nothing to disable !")
2020-07-23 21:29:02 +02:00
else:
s.delete(p)
s.commit()
2021-02-03 22:47:05 +01:00
await ctx.send(content="\U0001f44d")
2020-07-23 21:29:02 +02:00
s.close()
@commands.Cog.listener()
async def on_message(self, message: Message):
2020-07-23 21:32:43 +02:00
if message.guild is not None:
s = db.Session()
if not event_is_enabled(self.qualified_name, message.guild.id, s):
return
2020-07-23 21:32:43 +02:00
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")
2020-07-23 21:29:02 +02:00
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")