Move things into a repo named module.

This commit is contained in:
2025-03-18 10:48:48 -04:00
parent 5edd4e9a0c
commit 56406de693
9 changed files with 12 additions and 33 deletions
+1
View File
@@ -0,0 +1 @@
"""Empty init file for ${REPO_NAME_SNAKE} module."""
@@ -0,0 +1,12 @@
"""Cog loader. Adds all command cogs to the bot."""
from nextcord.ext import commands
from cogs.commands.hi import HiCog
# This function will be called when this extension is loaded.
# It is necessary to add these functions to the bot.
def setup(bot: commands.Bot) -> None:
"""Add all command classes to bot."""
bot.add_cog(HiCog(bot))
+22
View File
@@ -0,0 +1,22 @@
"""Example command file."""
import nextcord
from nextcord.ext import commands
class HiCog(commands.Cog, name="Test commands."):
"""Class holds commands for testing."""
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot
@nextcord.slash_command()
async def hi(self, interaction: nextcord.Interaction) -> None:
"""
Says hi.
Args:
interaction (nextcord.Interaction): Interaction to reply to.
"""
await interaction.response.send_message("Hi!", ephemeral=True)
+52
View File
@@ -0,0 +1,52 @@
"""Main file for bot."""
import logging
import os
import nextcord
from nextcord.ext import commands
from settings import settings
nextcord_logger = logging.getLogger("nextcord")
nextcord_logger.setLevel(logging.DEBUG)
nextcord_handler = logging.FileHandler(filename="nextcord.log", encoding="utf-8", mode="w")
nextcord_handler.setFormatter(logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s"))
nextcord_logger.addHandler(nextcord_handler)
def main() -> None:
"""Set config, intents, then connects the bot to discord."""
# Allows privileged 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
# To enable the guilds privileged intent:
# intents.guilds = True
# To enable the members privileged intent:
# intents.members = True
bot = commands.Bot(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() -> None:
"""When Discord is connected."""
if bot.user is None:
print("Discord connection failed.")
return
print(f"{bot.user.name} has connected to Discord!")
# Run Discord bot
bot.run(settings.params.discord_token)
if __name__ == "__main__":
main()
+28
View File
@@ -0,0 +1,28 @@
"""File contains classes defining ${REPO_NAME} settings structure."""
from dataclasses import dataclass
from dataclass_wizard import YAMLWizard
CONFIG_LOCATION = "conf/config.yaml"
@dataclass
class Params:
"""Class defines start parameters that shouldn't be changed."""
discord_token: str
@dataclass
class Settings(YAMLWizard):
"""Class defines all configs required for ${REPO_NAME}."""
params: Params
def save(self) -> None:
"""Save settings to the yaml file."""
self.to_yaml_file(CONFIG_LOCATION)
settings = Settings.from_yaml_file(CONFIG_LOCATION)