Skip to content

Commit

Permalink
Code Refactoring, Improved Logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dkoz committed Aug 14, 2024
1 parent 58dfcbf commit d0896ea
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
22 changes: 2 additions & 20 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async def on_ready():
print(f"Invite link: {nextcord.utils.oauth_url(bot.user.id)}")
print(f"{bot.user} is ready! Created by koz")

await settings.check_whitelist(bot)
bot.loop.create_task(settings.run_whitelist_check(bot))

activity = nextcord.Activity(
type=nextcord.ActivityType.playing, name=settings.bot_activity
Expand All @@ -39,25 +39,7 @@ async def on_application_command_error(interaction, error):
async def ping(ctx):
await ctx.send(f"Pong! {round(bot.latency * 1000)}ms")

def has_setup_function(module_name):
module_spec = importlib.util.find_spec(module_name)
if module_spec is None:
return False
module = importlib.util.module_from_spec(module_spec)
module_spec.loader.exec_module(module)
return hasattr(module, "setup")

for entry in os.listdir("cogs"):
if entry.endswith(".py"):
module_name = f"cogs.{entry[:-3]}"
if has_setup_function(module_name):
bot.load_extension(module_name)
elif os.path.isdir(f"cogs/{entry}"):
for filename in os.listdir(f"cogs/{entry}"):
if filename.endswith(".py"):
module_name = f"cogs.{entry}.{filename[:-3]}"
if has_setup_function(module_name):
bot.load_extension(module_name)
settings.load_cogs(bot)

if __name__ == "__main__":
bot.run(settings.bot_token)
33 changes: 32 additions & 1 deletion utils/settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
from dotenv import load_dotenv
import importlib.util
import asyncio
import logging

load_dotenv()
Expand All @@ -10,10 +12,39 @@
bot_language = os.getenv("BOT_LANGUAGE", "en")
whitelist_check = os.getenv('GUILD_WHITELIST')

# Logic for the guild whitelist.
async def check_whitelist(bot):
if whitelist_check:
wl_ids = [int(gid.strip()) for gid in whitelist_check.split(',')]
for guild in bot.guilds:
if guild.id not in wl_ids:
await guild.leave()
logging.info(f"Left {guild.name} (ID: {guild.id})")
logging.info(f"Left {guild.name} (ID: {guild.id})")

async def run_whitelist_check(bot, interval=600):
while True:
await check_whitelist(bot)
await asyncio.sleep(interval)
logging.info("Whitelist check complete.")

# Logic for loading cogs.
def load_cogs(bot):
for entry in os.listdir("cogs"):
if entry.endswith(".py"):
module_name = f"cogs.{entry[:-3]}"
if _has_setup(module_name):
bot.load_extension(module_name)
elif os.path.isdir(f"cogs/{entry}"):
for filename in os.listdir(f"cogs/{entry}"):
if filename.endswith(".py"):
module_name = f"cogs.{entry}.{filename[:-3]}"
if _has_setup(module_name):
bot.load_extension(module_name)

def _has_setup(module_name):
module_spec = importlib.util.find_spec(module_name)
if module_spec is None:
return False
module = importlib.util.module_from_spec(module_spec)
module_spec.loader.exec_module(module)
return hasattr(module, "setup")

0 comments on commit d0896ea

Please sign in to comment.