This repository was archived by the owner on Oct 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
95 lines (72 loc) · 2.59 KB
/
main.py
File metadata and controls
95 lines (72 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Guilded imports
import guilded
from guilded.ext import commands
# Built-in imports
from utils.jsprint import JSP
import yaml
import os
# Database imports
from motor.motor_asyncio import AsyncIOMotorClient
from beanie import init_beanie
from database import __all__
from pymongo.errors import OperationFailure
# Open the config
with open("./config.yaml", "r") as f:
config = yaml.safe_load(f)
# Create the bot
bot = commands.Bot(
command_prefix=config["guilded"]["prefixes"],
help_command=commands.MinimalHelpCommand(),
features=guilded.ClientFeatures(
experimental_event_style=True,
official_markdown=True,
),
)
bot.config = config
# Create the console
console = JSP()
bot.console = console
# Create the Motor client
client = AsyncIOMotorClient(config["mongodb"]["uri"])
bot.dbClient = client
# Create the module loader
def loadModules(bot: commands.Bot):
"""Loads all modues in the `modules` directory (including subdirectories)"""
for root, dirs, files in os.walk("./modules"):
for file in files:
if file.endswith(".py"):
# If the file begins with "_" then skip it
if file.startswith("_"):
continue
# Load the extension using dot-qualified name
cog_path = os.path.relpath(os.path.join(root, file), start="./modules")
bot.load_extension(
f'modules.{cog_path.replace(os.sep, ".").replace(".py", "")}'
)
console.success(f"Loaded {cog_path}")
loadModules(bot)
@bot.event
async def on_ready():
console.success("Connected to Guilded")
# Check if the cluster is online
try:
await client.admin.command("ping")
console.success("Pinged Mongo successfully")
except Exception as e:
if isinstance(e, OperationFailure):
console.warn("Failed to ping Mongo")
else:
console.error("Unexpected error occurred while pinging Mongo")
console.trace(str(e))
# Initialize Beanie using the previously created Motor client
await init_beanie(client[config["mongodb"]["database"]], document_models=__all__)
console.info("Beanie initialization complete")
# Print success/info stack
console.info(f'Logged into Guilded as "{bot.user.name}"')
console.info(f'Using database "{config["mongodb"]["database"]}" on Mongo cluster')
@bot.event
async def on_command_completion(ctx: commands.Context):
console.info(
f'{ctx.author.id} ran "{ctx.command.qualified_name}" in {ctx.channel.id}'
)
bot.run(config["guilded"]["token"])