Template
Address smells in main.py, remove hydra usage.
This commit is contained in:
@@ -1,57 +1,51 @@
|
|||||||
|
"""Main file for bot."""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
|
|
||||||
import hydra
|
|
||||||
import nextcord
|
import nextcord
|
||||||
from hydra.core.config_store import ConfigStore
|
|
||||||
from nextcord.ext import commands
|
from nextcord.ext import commands
|
||||||
|
|
||||||
from config import BotConfig
|
nextcord_logger = logging.getLogger("nextcord")
|
||||||
|
nextcord_logger.setLevel(logging.DEBUG)
|
||||||
cs = ConfigStore.instance()
|
nextcord_handler = logging.FileHandler(filename="nextcord.log", encoding="utf-8", mode="w")
|
||||||
cs.store(name="${REPO_NAME}_config", node=BotConfig)
|
nextcord_handler.setFormatter(logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s"))
|
||||||
|
nextcord_logger.addHandler(nextcord_handler)
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
@hydra.main(config_path="conf", config_name="config")
|
def main() -> None:
|
||||||
def main(cfg: BotConfig):
|
|
||||||
"""
|
"""
|
||||||
Main function for the bot. Sets config, intents, then connects the bot to discord.
|
Set config, intents, then connects the bot to discord.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
cfg (BotConfig): The local configs to use.
|
cfg (BotConfig): The local configs to use.
|
||||||
"""
|
|
||||||
|
|
||||||
# Allows privledged intents for monitoring members joining, roles editing, and role assignments
|
"""
|
||||||
|
# Allows privileged intents for monitoring members joining, roles editing, and role assignments
|
||||||
# These need to be enabled in the developer portal as well
|
# These need to be enabled in the developer portal as well
|
||||||
intents = nextcord.Intents.default()
|
intents = nextcord.Intents.default()
|
||||||
|
|
||||||
# Required in order to read messages (eg. prefix commands)
|
# Required in order to read messages (eg. prefix commands)
|
||||||
intents.message_content = True
|
intents.message_content = True
|
||||||
|
|
||||||
# To enable the guilds priveleged intent:
|
# To enable the guilds privileged intent:
|
||||||
# intents.guilds = True
|
# intents.guilds = True
|
||||||
|
|
||||||
# To enable the members priveleged intent:
|
# To enable the members privileged intent:
|
||||||
# intents.members = True
|
# intents.members = True
|
||||||
|
|
||||||
bot = commands.Bot(intents=intents)
|
bot = commands.Bot(intents=intents)
|
||||||
bot.config = cfg
|
|
||||||
|
|
||||||
# Get the modules of all cogs whose directory structure is ./cogs/<module_name>
|
# Get the modules of all cogs whose directory structure is ./cogs/<module_name>
|
||||||
for folder in os.listdir("cogs"):
|
for folder in os.listdir("cogs"):
|
||||||
bot.load_extension(f"cogs.{folder}")
|
bot.load_extension(f"cogs.{folder}")
|
||||||
|
|
||||||
@bot.listen()
|
@bot.listen()
|
||||||
async def on_ready():
|
async def on_ready() -> None:
|
||||||
"""When Discord is connected"""
|
"""When Discord is connected."""
|
||||||
assert bot.user is not None
|
if bot.user is None:
|
||||||
|
print("Discord connection failed.")
|
||||||
|
return
|
||||||
print(f"{bot.user.name} has connected to Discord!")
|
print(f"{bot.user.name} has connected to Discord!")
|
||||||
|
|
||||||
# Run Discord bot
|
# Run Discord bot
|
||||||
@@ -59,7 +53,4 @@ def main(cfg: BotConfig):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
sys.argv.append("hydra.job.chdir=False")
|
main()
|
||||||
sys.argv.append("hydra/job_logging=none")
|
|
||||||
sys.argv.append("hydra/hydra_logging=none")
|
|
||||||
main() # pylint: disable=no-value-for-parameter
|
|
||||||
|
|||||||
Reference in New Issue
Block a user