Browse Source

Bootstrapping loader

rewrite
noirscape 1 year ago
parent
commit
38688b0aa2
6 changed files with 47 additions and 4 deletions
  1. +2
    -0
      .gitignore
  2. +7
    -4
      client.py
  3. +2
    -0
      config.yml.example
  4. +31
    -0
      main.py
  5. +4
    -0
      models/config.py
  6. +1
    -0
      secure.yml.example

+ 2
- 0
.gitignore View File

@ -0,0 +1,2 @@
secure.yml
config.yml

+ 7
- 4
client.py View File

@ -1,5 +1,6 @@
# stdlib
import asyncio
from typing import Callable
# external libraries
from discord.ext import commands
@ -9,16 +10,18 @@ import discord
from models.management_guild import ManagementGuild
class KirigiriBot(commands.Bot):
def __init__(self, management_guild: int, **kwargs):
def __init__(self, management_guild: int, post_ready_callback=None, **kwargs):
"""
Initialize KirigiriBot. Accepts the same arguments as commands.Bot, in addition to a couple of extra ones.
Arguments:
management_guild - Guild used to manage the bot. The bot will send messages about it's functionality to channels in this guild and considers itself the owner of this guild.
post_ready_callback - Optional callback argument. Used to run tasks when starting the bot. Used for running one-time commands from the bot context (eg. setting up guilds)
"""
super().__init__(**kwargs)
self.management_guild_id = management_guild
self._cache_ready = asyncio.Event()
self._post_ready_callback = post_ready_callback
async def get_management_guild(self) -> discord.Guild:
if not self.is_cache_ready():
@ -31,7 +34,7 @@ class KirigiriBot(commands.Bot):
async def wait_for_cache_to_ready(self):
await self._cache_ready.wait()
async def on_ready(self, post_ready_callback=None):
async def on_ready(self):
"""
Initializes the bot. Sets up the required information for the bot to fully function.
"""
@ -41,5 +44,5 @@ class KirigiriBot(commands.Bot):
# mark the cache as ready
self._cache_ready.set()
if post_ready_callback:
await post_ready_callback()
if self._post_ready_callback:
await self._post_ready_callback()

+ 2
- 0
config.yml.example View File

@ -0,0 +1,2 @@
prefix:
management_guild:

+ 31
- 0
main.py View File

@ -1 +1,32 @@
# external libraries
import yaml
# internal files
from client import KirigiriBot
from cliparser import CliParser
from models.config import Config
def parse_cli_args() -> dict:
parser = CliParser()
return parser.parse_options()
def load_config() -> Config:
with open("config.yml", "r") as configfile:
configdict = yaml.safe_load(configfile)
with open("secure.yml", "r") as securefile:
token = yaml.safe_load(securefile)["token"]
config = Config(configdict)
return config, token
def main():
args = parse_cli_args()
config, token = load_config()
if args["run"]:
bot = KirigiriBot(config.management_guild_id, command_prefix=config.prefix)
bot.run(token)
if __name__ == "__main__":
main()

+ 4
- 0
models/config.py View File

@ -0,0 +1,4 @@
class Config:
def __init__(self, configdict):
self.prefix: str = configdict["prefix"]
self.management_guild_id: int = configdict["management_guild"]

+ 1
- 0
secure.yml.example View File

@ -0,0 +1 @@
token:

Loading…
Cancel
Save