Files
agon/highscores.py
T

82 lines
2.3 KiB
Python

"""
main file for highscores bot
"""
import logging
import os
import sys
import nextcord
from nextcord.ext import commands
from sqlalchemy import create_engine
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
import config
from classes.submission import SubmissionMap
logger = logging.getLogger("nextcord")
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename="nextcord.log", encoding="utf-8", mode="w")
handler.setFormatter(logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s"))
logger.addHandler(handler)
# connect with sql alchemy and autoload schema
url = f"mysql://{config.SQL_USER}:{config.SQL_PASS}@{config.SQL_HOST}/{config.SQL_DB}"
engine = create_engine(url)
connection = engine.connect()
Base = automap_base()
Base.prepare(autoload_with=engine)
session = Session(engine)
# define the classes
Guild = Base.classes.Guilds
CategoryGroup = Base.classes.CategoryGroups
Category = Base.classes.Categories
Score = Base.classes.Scores
submission_objects: SubmissionMap = SubmissionMap()
def main():
"""Main function for bot."""
min_python_version = (3, 8)
if sys.version_info < min_python_version:
print("ERROR: Python version must be >= 3.8")
sys.exit()
# Allows privledged intents for monitoring members joining, roles editing, and role assignments
# These need to be enabled in the developer portal as well
intents = nextcord.Intents.default()
# Required in order to read messages (eg. prefix commands)
intents.message_content = True # pylint: disable=assigning-non-slot
# To enable the guilds priveleged intent:
# intents.guilds = True
# To enable the members priveleged intent:
# intents.members = True
bot = commands.Bot(
commands.when_mentioned_or(config.BOT_PREFIX),
intents=intents,
)
# Get the modules of all cogs whose directory structure is ./cogs/<module_name>
for folder in os.listdir("cogs"):
bot.load_extension(f"cogs.{folder}")
@bot.listen()
async def on_ready():
"""When Discord is connected"""
assert bot.user is not None
print(f"{bot.user.name} has connected to Discord!")
# Run Discord bot
bot.run(config.DISCORD_TOKEN)
if __name__ == "__main__":
main()