2024-09-08 01:24:03 +01:00
|
|
|
# import sys
|
|
|
|
import asyncio
|
2024-07-28 03:59:19 +01:00
|
|
|
|
|
|
|
import niobot
|
|
|
|
import tomllib
|
|
|
|
import platform
|
|
|
|
import logging
|
2024-09-08 01:24:03 +01:00
|
|
|
# import tortoise
|
2024-07-28 03:59:19 +01:00
|
|
|
from pathlib import Path
|
2024-07-28 23:33:15 +01:00
|
|
|
|
2024-07-28 03:59:19 +01:00
|
|
|
log = logging.getLogger(__name__)
|
2024-07-31 23:17:09 +01:00
|
|
|
logging.getLogger("nio.rooms").setLevel(logging.WARNING)
|
|
|
|
logging.getLogger("nio.crypto.log").setLevel(logging.WARNING)
|
|
|
|
logging.getLogger("peewee").setLevel(logging.WARNING)
|
|
|
|
logging.getLogger("nio.responses").setLevel(logging.WARNING)
|
2024-07-28 03:59:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
with open("config.toml", "rb") as fd:
|
|
|
|
config = tomllib.load(fd)
|
|
|
|
|
|
|
|
|
|
|
|
(store := (Path.cwd() / "store")).mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
|
|
class TortoiseIntegratedBot(niobot.NioBot):
|
2024-07-28 23:33:15 +01:00
|
|
|
cfg: dict
|
|
|
|
|
2024-07-28 03:59:19 +01:00
|
|
|
async def start(self, **kwargs):
|
2024-09-08 01:24:03 +01:00
|
|
|
# url = config["database"].get("uri")
|
|
|
|
# if not url:
|
|
|
|
# log.critical("No database URI specified in config.toml - using ephemeral sqlite.")
|
|
|
|
# url = "sqlite://:memory:"
|
|
|
|
# sys.path.extend(("..", "."))
|
2024-08-04 17:21:00 +01:00
|
|
|
# await tortoise.Tortoise.init(db_url=url, modules={"models": ["app.models"]})
|
|
|
|
# await tortoise.Tortoise.generate_schemas()
|
2024-07-28 03:59:19 +01:00
|
|
|
|
|
|
|
for file in (Path(__file__).parent / "./modules").glob("*.py"):
|
|
|
|
if file.name.startswith("__"):
|
|
|
|
log.warning("Skipping loading %s - dunder file.", file)
|
|
|
|
name = "app.modules." + file.name[:-3]
|
|
|
|
try:
|
|
|
|
log.info("Loading %s...", name)
|
|
|
|
bot.mount_module(name)
|
|
|
|
except Exception:
|
|
|
|
log.error("Failed to load %s!", name, exc_info=True)
|
|
|
|
else:
|
|
|
|
log.info("Loaded %s.", name)
|
|
|
|
|
|
|
|
await super().start(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
bot = TortoiseIntegratedBot(
|
|
|
|
homeserver=config["bot"]["homeserver"],
|
|
|
|
user_id=config["bot"]["user_id"],
|
|
|
|
device_id=config["bot"].get("device_id", platform.node()),
|
|
|
|
store_path=str(store.resolve()),
|
|
|
|
command_prefix="h!",
|
2024-07-28 23:33:15 +01:00
|
|
|
owner_id=config["bot"].get("owner_id") or "@nex:nexy7574.co.uk",
|
2024-07-28 03:59:19 +01:00
|
|
|
)
|
|
|
|
bot.cfg = config
|
|
|
|
|
|
|
|
|
|
|
|
@bot.on_event("ready")
|
|
|
|
async def on_ready(_):
|
|
|
|
log.info("Bot has logged in.")
|
2024-07-28 23:33:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bot.on_event("command")
|
|
|
|
async def on_command(ctx: niobot.Context):
|
|
|
|
log.info("Command %s invoked by %s.", ctx.command, ctx.message.sender)
|
|
|
|
|
|
|
|
|
|
|
|
@bot.on_event("command_error")
|
|
|
|
async def on_command_error(ctx: niobot.Context, exc: Exception):
|
|
|
|
log.error("Command %s failed.", ctx.command, exc_info=exc)
|
2024-09-08 01:24:03 +01:00
|
|
|
text = "\N{warning sign} Command failed: `%s`" % exc
|
|
|
|
text = text.replace(bot.access_token, "REDACTED")
|
|
|
|
await ctx.respond(
|
|
|
|
text
|
|
|
|
)
|
2024-07-28 23:33:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
@bot.on_event("command_complete")
|
|
|
|
async def on_command_complete(ctx: niobot.Context, _):
|
|
|
|
log.info("Command %s completed.", ctx.command)
|
2024-07-31 23:17:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-09-08 01:24:03 +01:00
|
|
|
asyncio.run(bot.start(access_token=bot.cfg["bot"]["access_token"]))
|
|
|
|
# tortoise.run_async(bot.start(access_token=bot.cfg["bot"]["access_token"]))
|