2021-06-22 00:36:22 +02:00
|
|
|
from data.snipe import Snipe
|
2021-06-19 20:11:55 +02:00
|
|
|
from discord.ext import commands, ipc
|
2021-08-30 23:21:34 +02:00
|
|
|
from dislash import InteractionClient
|
2021-06-22 00:36:22 +02:00
|
|
|
import os
|
2021-09-03 20:40:03 +02:00
|
|
|
from settings import HOST_IPC, SLASH_TEST_GUILDS
|
2021-06-19 20:31:31 +02:00
|
|
|
from startup.init_files import check_all
|
2021-06-22 00:36:22 +02:00
|
|
|
from typing import Dict
|
2021-06-19 20:11:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Didier(commands.Bot):
|
|
|
|
"""
|
|
|
|
Main Bot class for Didier
|
|
|
|
"""
|
2021-08-30 23:21:34 +02:00
|
|
|
# Reference to interactions client
|
|
|
|
interactions: InteractionClient
|
2021-06-22 00:36:22 +02:00
|
|
|
|
|
|
|
# Dict to store the most recent Snipe info per channel
|
|
|
|
snipe: Dict[int, Snipe] = {}
|
|
|
|
|
2021-06-19 20:11:55 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
self._host_ipc = HOST_IPC
|
|
|
|
|
|
|
|
# IPC Server
|
|
|
|
# TODO secret key
|
|
|
|
self.ipc = ipc.Server(self, secret_key="SOME_SECRET_KEY") if self._host_ipc else None
|
|
|
|
|
|
|
|
# Cogs that should be loaded before the others
|
|
|
|
self._preload = ("ipc", "utils", "failedchecks", "events",)
|
|
|
|
|
|
|
|
# Remove default help command
|
|
|
|
self.remove_command("help")
|
|
|
|
|
2021-08-30 23:21:34 +02:00
|
|
|
# Create interactions client
|
2021-09-03 20:40:03 +02:00
|
|
|
self.interactions = InteractionClient(self, test_guilds=SLASH_TEST_GUILDS)
|
2021-08-30 23:21:34 +02:00
|
|
|
|
2021-06-19 20:31:31 +02:00
|
|
|
# Load all extensions
|
2021-06-19 20:11:55 +02:00
|
|
|
self.init_extensions()
|
|
|
|
|
2021-06-19 20:31:31 +02:00
|
|
|
# Check missing files
|
|
|
|
check_all()
|
|
|
|
|
2021-06-19 20:11:55 +02:00
|
|
|
def init_extensions(self):
|
|
|
|
# Load initial extensions
|
|
|
|
for ext in self._preload:
|
|
|
|
self.load_extension(f"cogs.{ext}")
|
|
|
|
|
|
|
|
# Load all remaining cogs
|
2021-09-03 17:57:45 +02:00
|
|
|
self._init_directory("./cogs")
|
|
|
|
|
|
|
|
def _init_directory(self, path: str):
|
|
|
|
"""
|
|
|
|
Load all cogs from a directory
|
|
|
|
"""
|
|
|
|
# Path to pass into load_extension
|
|
|
|
load_path = path[2:].replace("/", ".")
|
|
|
|
|
|
|
|
for file in os.listdir(path):
|
|
|
|
# Python file
|
|
|
|
if file.endswith(".py"):
|
|
|
|
if not file.startswith(self._preload):
|
|
|
|
self.load_extension(f"{load_path}.{file[:-3]}")
|
|
|
|
elif os.path.isdir(new_path := f"{path}/{file}"):
|
|
|
|
# Subdirectory
|
|
|
|
# Also walrus operator hype
|
|
|
|
self._init_directory(new_path)
|
2021-06-19 20:11:55 +02:00
|
|
|
|
|
|
|
async def on_ipc_ready(self):
|
|
|
|
print("IPC server is ready.")
|
|
|
|
|
|
|
|
async def on_ipc_error(self, endpoint, error):
|
|
|
|
print(endpoint, "raised", error)
|