2020-11-05 00:53:12 +01:00
|
|
|
from traceback import format_exc
|
|
|
|
|
2020-04-08 17:36:42 +02:00
|
|
|
from discord.ext import commands
|
2020-11-05 13:56:00 +01:00
|
|
|
from discord import Embed, Guild
|
2021-01-06 17:53:30 +01:00
|
|
|
from discord.ext.commands import BadArgument
|
|
|
|
from discord_slash import cog_ext, SlashContext, SlashCommandOptionType
|
|
|
|
from discord_slash.utils import manage_commands
|
2020-11-05 13:56:00 +01:00
|
|
|
|
|
|
|
import db
|
2021-01-06 17:53:30 +01:00
|
|
|
from administrator import slash
|
2020-07-23 21:22:47 +02:00
|
|
|
from administrator.logger import logger
|
2020-04-08 17:36:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
extension_name = "extension"
|
|
|
|
logger = logger.getChild(extension_name)
|
|
|
|
|
|
|
|
|
|
|
|
class Extension(commands.Cog):
|
|
|
|
def __init__(self, bot: commands.Bot):
|
|
|
|
self.bot = bot
|
2021-01-06 17:53:30 +01:00
|
|
|
slash.get_cog_commands(self)
|
2020-04-08 17:36:42 +02:00
|
|
|
|
2020-07-23 21:34:32 +02:00
|
|
|
def description(self):
|
|
|
|
return "Manage bot's extensions"
|
|
|
|
|
2021-01-06 17:53:30 +01:00
|
|
|
@cog_ext.cog_subcommand(base="extension", name="list", description="List all enabled extensions")
|
2020-11-05 13:56:00 +01:00
|
|
|
@commands.has_guild_permissions(administrator=True)
|
2021-01-06 17:53:30 +01:00
|
|
|
async def extension_list(self, ctx: SlashContext):
|
2020-11-05 13:56:00 +01:00
|
|
|
s = db.Session()
|
2020-11-05 00:58:39 +01:00
|
|
|
embed = Embed(title="Extensions list")
|
2020-11-05 13:56:00 +01:00
|
|
|
for es in s.query(db.ExtensionState).filter(db.ExtensionState.guild_id == ctx.guild.id):
|
|
|
|
embed.add_field(name=es.extension_name, value="Enable" if es.state else "Disable")
|
2021-01-06 17:53:30 +01:00
|
|
|
s.close()
|
|
|
|
await ctx.send(embeds=[embed])
|
2020-11-05 13:56:00 +01:00
|
|
|
|
2021-01-06 17:53:30 +01:00
|
|
|
@cog_ext.cog_subcommand(base="extension",
|
|
|
|
name="enable",
|
|
|
|
description="Enable an extensions",
|
|
|
|
options=[manage_commands.create_option("extension", "The extension to enable",
|
|
|
|
SlashCommandOptionType.STRING, True)])
|
2020-11-05 13:56:00 +01:00
|
|
|
@commands.has_guild_permissions(administrator=True)
|
2021-01-06 17:53:30 +01:00
|
|
|
async def extension_enable(self, ctx: SlashContext, name: str):
|
2020-11-05 13:56:00 +01:00
|
|
|
s = db.Session()
|
|
|
|
es = s.query(db.ExtensionState).get((name, ctx.guild.id))
|
2021-01-06 17:53:30 +01:00
|
|
|
if not es:
|
2020-11-05 13:56:00 +01:00
|
|
|
raise BadArgument()
|
2021-01-06 17:53:30 +01:00
|
|
|
elif es.state:
|
|
|
|
message = "Extension already enabled"
|
|
|
|
else:
|
|
|
|
es.state = True
|
|
|
|
s.add(es)
|
|
|
|
s.commit()
|
|
|
|
s.close()
|
|
|
|
message = "\U0001f44d"
|
|
|
|
await ctx.send(content=message)
|
|
|
|
|
|
|
|
@cog_ext.cog_subcommand(base="extension",
|
|
|
|
name="disable",
|
|
|
|
description="Disable an extensions",
|
|
|
|
options=[manage_commands.create_option("extension", "The extension to disable",
|
|
|
|
SlashCommandOptionType.STRING, True)])
|
2020-11-05 13:56:00 +01:00
|
|
|
@commands.has_guild_permissions(administrator=True)
|
2021-01-06 17:53:30 +01:00
|
|
|
async def extension_disable(self, ctx: SlashContext, name: str):
|
2020-11-05 13:56:00 +01:00
|
|
|
s = db.Session()
|
|
|
|
es = s.query(db.ExtensionState).get((name, ctx.guild.id))
|
2021-01-06 17:53:30 +01:00
|
|
|
if not es:
|
2020-11-05 13:56:00 +01:00
|
|
|
raise BadArgument()
|
2021-01-06 17:53:30 +01:00
|
|
|
elif not es.state:
|
|
|
|
message = "Extension already disabled"
|
|
|
|
else:
|
|
|
|
es.state = False
|
|
|
|
s.add(es)
|
|
|
|
s.commit()
|
|
|
|
s.close()
|
|
|
|
message = "\U0001f44d"
|
|
|
|
await ctx.send(content=message)
|
|
|
|
|
|
|
|
@commands.group("extension", pass_context=True)
|
|
|
|
async def extension(self, ctx: commands.Context):
|
|
|
|
pass
|
2020-11-05 13:56:00 +01:00
|
|
|
|
|
|
|
@extension.group("loaded", pass_context=True)
|
|
|
|
@commands.is_owner()
|
|
|
|
async def extension_loaded(self, ctx: commands.Context):
|
|
|
|
embed = Embed(title="Extensions loaded")
|
2020-11-05 00:58:39 +01:00
|
|
|
for extension in self.bot.extensions:
|
|
|
|
embed.add_field(name=extension, value="Loaded", inline=False)
|
|
|
|
await ctx.send(embed=embed)
|
2020-04-08 17:36:42 +02:00
|
|
|
|
|
|
|
@extension.group("load", pass_context=True)
|
2020-11-05 13:56:00 +01:00
|
|
|
@commands.is_owner()
|
2020-04-08 17:36:42 +02:00
|
|
|
async def extension_load(self, ctx: commands.Context, name: str):
|
|
|
|
try:
|
|
|
|
self.bot.load_extension(name)
|
2020-05-26 17:54:25 +02:00
|
|
|
except Exception as e:
|
2020-04-08 17:36:42 +02:00
|
|
|
await ctx.message.add_reaction("\u26a0")
|
2020-11-05 00:53:12 +01:00
|
|
|
await ctx.send(f"{e.__class__.__name__}: {e}\n```{format_exc()}```")
|
2020-04-08 17:36:42 +02:00
|
|
|
else:
|
|
|
|
await ctx.message.add_reaction("\U0001f44d")
|
|
|
|
|
|
|
|
@extension.group("unload", pass_context=True)
|
2020-11-05 13:56:00 +01:00
|
|
|
@commands.is_owner()
|
2020-04-08 17:36:42 +02:00
|
|
|
async def extension_unload(self, ctx: commands.Context, name: str):
|
|
|
|
try:
|
|
|
|
self.bot.unload_extension(name)
|
2020-05-26 17:54:25 +02:00
|
|
|
except Exception as e:
|
2020-04-08 17:36:42 +02:00
|
|
|
await ctx.message.add_reaction("\u26a0")
|
2020-11-05 00:53:12 +01:00
|
|
|
await ctx.send(f"{e.__class__.__name__}: {e}\n```{format_exc()}```")
|
2020-04-08 17:36:42 +02:00
|
|
|
else:
|
|
|
|
await ctx.message.add_reaction("\U0001f44d")
|
|
|
|
|
|
|
|
@extension.group("reload", pass_context=True)
|
2020-11-05 13:56:00 +01:00
|
|
|
@commands.is_owner()
|
2020-04-08 17:36:42 +02:00
|
|
|
async def extension_reload(self, ctx: commands.Context, name: str):
|
|
|
|
try:
|
|
|
|
self.bot.unload_extension(name)
|
|
|
|
self.bot.load_extension(name)
|
2020-05-26 17:54:25 +02:00
|
|
|
except Exception as e:
|
2020-04-08 17:36:42 +02:00
|
|
|
await ctx.message.add_reaction("\u26a0")
|
2020-11-05 00:53:12 +01:00
|
|
|
await ctx.send(f"{e.__class__.__name__}: {e}\n```{format_exc()}```")
|
2020-04-08 17:36:42 +02:00
|
|
|
else:
|
|
|
|
await ctx.message.add_reaction("\U0001f44d")
|
|
|
|
|
2020-11-05 13:56:00 +01:00
|
|
|
@commands.Cog.listener()
|
|
|
|
async def on_ready(self):
|
|
|
|
s = db.Session()
|
|
|
|
for guild in self.bot.guilds:
|
|
|
|
for extension in filter(lambda x: x not in ["Extension", "Help"], self.bot.cogs):
|
|
|
|
e = s.query(db.Extension).get(extension)
|
|
|
|
if not e:
|
|
|
|
s.add(db.Extension(extension))
|
|
|
|
s.commit()
|
|
|
|
es = s.query(db.ExtensionState).get((extension, guild.id))
|
|
|
|
if not es:
|
|
|
|
s.add(db.ExtensionState(extension, guild.id))
|
|
|
|
s.commit()
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
@commands.Cog.listener()
|
|
|
|
async def on_guild_join(self, guild: Guild):
|
|
|
|
s = db.Session()
|
|
|
|
for extension in s.query(db.Extension).all():
|
|
|
|
s.add(db.ExtensionState(extension.name, guild.id))
|
|
|
|
s.commit()
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
@commands.Cog.listener()
|
|
|
|
async def on_guild_remove(self, guild: Guild):
|
|
|
|
s = db.Session()
|
|
|
|
for es in s.query(db.ExtensionState).filter(db.ExtensionState.guild_id == guild.id):
|
|
|
|
s.delete(es)
|
|
|
|
s.commit()
|
|
|
|
s.close()
|
|
|
|
|
2021-01-06 17:53:30 +01:00
|
|
|
def cog_unload(self):
|
|
|
|
slash.remove_cog_commands(self)
|
|
|
|
|
2020-04-08 17:36:42 +02:00
|
|
|
|
|
|
|
def setup(bot):
|
|
|
|
logger.info(f"Loading...")
|
|
|
|
try:
|
|
|
|
bot.add_cog(Extension(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("Extension")
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Error unloading: {e}")
|
|
|
|
else:
|
|
|
|
logger.info(f"Unload successful")
|