aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2025-03-24 16:42:56 +0100
committerLinnea Gräf <nea@nea.moe>2025-03-24 16:42:56 +0100
commit29911e1bfe040f0d3b187a22aae8ec5ed551040a (patch)
treee61a0a0ecffdea91756bb71017fe30682190021e
parent82002dc85f6b3282c72ae2fe9e991e628d8799a9 (diff)
downloadadultnea-29911e1bfe040f0d3b187a22aae8ec5ed551040a.tar.gz
adultnea-29911e1bfe040f0d3b187a22aae8ec5ed551040a.tar.bz2
adultnea-29911e1bfe040f0d3b187a22aae8ec5ed551040a.zip
feat: Add module system
-rw-r--r--adultnea/client.py61
-rw-r--r--adultnea/modules/meta.py31
2 files changed, 87 insertions, 5 deletions
diff --git a/adultnea/client.py b/adultnea/client.py
index 6050a28..abd9e84 100644
--- a/adultnea/client.py
+++ b/adultnea/client.py
@@ -1,11 +1,62 @@
+import logging
+from pathlib import Path
+from typing import Any, Optional
+
import discord.ext.commands as commands
+from discord.ext.commands import errors
+import discord
+
+class Context(commands.Context['AdultClient']):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.followup_message: Optional[discord.message.Message] = None
+
+ async def followup(self, content: str):
+ if self.followup_message:
+ await self.followup_message.edit(content)
+ else:
+ self.followup_message = await self.reply(content)
class AdultClient(commands.Bot):
async def on_ready(self):
- print('Logged in as', self.user)
+ logging.info(f'Logged in as {self.user}')
+
+ async def setup_hook(self) -> None:
+ await self.reload_all()
+
+ async def reload_all(self):
+ base_path = Path(__file__).parent / 'modules'
+ base_package = __package__ + '.modules'
+ for file in base_path.rglob('*.py'):
+ file: Path
+ relative_file = file.with_name(file.name[:-len('.py')]) \
+ .relative_to(base_path)
+ name = base_package + '.' + str(relative_file).replace('/', '').replace('\\', '.')
+ if name in self.extensions:
+ logging.info(f'Reloading {name}.')
+ await self.reload_extension(name)
+ else:
+ logging.info(f'Loading extension from {file} ({relative_file}) as {name}')
+ await self.load_extension(name)
+
+ def __init__(self):
+ super().__init__(
+ command_prefix='~',
+ self_bot=True,
+ chunk_guilds_at_startup=False,
+ )
+
+ async def on_command_error(
+ self, context: Context,
+ exception: errors.CommandError, /) -> None:
+ logging.error(
+ f"Encountered error in {context.command.qualified_name if context.command else 'unknown'}:",
+ exc_info=exception)
+
+ async def get_context(
+ self, *args, **kwargs) -> Any:
+ return await super().get_context(*args, **kwargs, cls=Context)
+
-client = AdultClient(
- command_prefix='~',
- self_bot=True,
-)
+client = AdultClient()
diff --git a/adultnea/modules/meta.py b/adultnea/modules/meta.py
new file mode 100644
index 0000000..9e81b91
--- /dev/null
+++ b/adultnea/modules/meta.py
@@ -0,0 +1,31 @@
+import logging
+from tarfile import TruncatedHeaderError
+
+import discord.ext.commands as commands
+
+from adultnea.client import AdultClient, client, Context
+
+
+@commands.group(
+ invoke_without_command=True
+)
+async def mod(ctx: Context):
+ pass
+
+
+@mod.command()
+async def ls(ctx: Context):
+ module_names = [it.replace(__package__, '') for it in ctx.bot.extensions.keys()]
+ await ctx.followup('Modules:\n' + '\n'.join(f'- `{name}`' for name in module_names))
+
+
+@mod.command()
+async def reload(ctx: Context):
+ await ctx.followup(content='Reload started.')
+ await ctx.bot.reload_all()
+ await ctx.followup(content='Reloaded.')
+
+
+async def setup(bot: AdultClient):
+ logging.info('Loading meta module.')
+ bot.add_command(mod)