- Got rid of duplicate Token Import

- Moved the code around a little to read like:
Imports → Load environment → Declare globals → Load bad words → Events/commands → DB setup → Helper functions → Run bot.

This should make the code easier to read/follow while keeping related things closer together in the codebase.
This commit is contained in:
2025-08-08 16:14:54 -05:00
parent 4f442c2f92
commit 7238ccca34

27
Main.py
View File

@@ -9,11 +9,9 @@ import re
# Show the path we expect to load # Show the path we expect to load
# Explicitly find the .env file in the same directory as the script # Explicitly find the .env file in the same directory as the script
env_path = Path(__file__).resolve().parent / ".env" env_path = Path(__file__).resolve().parent / ".env"
print(f"Looking for .env at: {env_path}") print(f"Looking for .env at: {env_path}")
# Load the env file # Load the env file
load_dotenv(dotenv_path=env_path) load_dotenv(dotenv_path=env_path)
@@ -24,7 +22,16 @@ print(f"Loaded token Succesfully")
if token is None: if token is None:
raise RuntimeError(".env file not loaded or DISCORD_TOKEN not set.") raise RuntimeError(".env file not loaded or DISCORD_TOKEN not set.")
# --- Load bad words from file into a set --- ### Declare generic and global variables here ###
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
# --- Load bad words from file into a set when the bot starts,
# --- this makes the file accessed only once each time the bot runs,
# --- as oppsoed to checking the txt file every time a discord message comes though ---
with open("bad_words.txt", "r", encoding="utf-8") as f: with open("bad_words.txt", "r", encoding="utf-8") as f:
BAD_WORDS = {line.strip().lower() for line in f if line.strip()} BAD_WORDS = {line.strip().lower() for line in f if line.strip()}
@@ -32,16 +39,6 @@ with open("bad_words.txt", "r", encoding="utf-8") as f:
# \b = word boundary, (?i) = case-insensitive # \b = word boundary, (?i) = case-insensitive
bad_words_pattern = re.compile(r"\b(" + "|".join(map(re.escape, BAD_WORDS)) + r")\b", re.IGNORECASE) bad_words_pattern = re.compile(r"\b(" + "|".join(map(re.escape, BAD_WORDS)) + r")\b", re.IGNORECASE)
token = os.getenv("DISCORD_TOKEN")
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event @bot.event
async def on_ready(): async def on_ready():
print(f"{bot.user.name} is ready") print(f"{bot.user.name} is ready")
@@ -55,7 +52,9 @@ async def on_ready():
@bot.event @bot.event
async def on_member_join(member): async def on_member_join(member):
await member.send(f"Welcome to the server {member.name}") await member.send(f"Welcome to the server {member.name}!")
# Should we give new members some points just for joining so they can start off with something? - Joshypoo
# we should also add users to the DB on member join, becasue we will get to a point where the bot isn't constantly restarting
#Responding to cuss words #Responding to cuss words
@bot.event @bot.event