Skip to content
This repository was archived by the owner on Feb 19, 2021. It is now read-only.
This repository was archived by the owner on Feb 19, 2021. It is now read-only.

Consider using a single loop to load all extensions #2

@kyleerhabor

Description

@kyleerhabor

Hey, I saw your thread on the Hypixel forums and thought I'd drop some feedback.

HypixelBot/main.py

Lines 24 to 46 in f70cb70

def load_extension(extension):
try:
bot.load_extension(extension)
con.log(f"{extension} loaded.")
except Exception as e:
con.log(f"Couldn't load {extension}: {e}")
for file in os.listdir("commands"):
if file.endswith(".py"):
load_extension(f"commands.{file}".replace('.py', ''))
for file in os.listdir("commands/owner"):
if file.endswith(".py"):
load_extension(f"commands.owner.{file}".replace('.py', ''))
for file in os.listdir("events"):
if file.endswith(".py"):
load_extension(f"events.{file}".replace('.py', ''))
for file in os.listdir("tasks"):
if file.endswith(".py"):
load_extension(f"tasks.{file}".replace('.py', ''))

Instead of going loop -> if ends with py -> load extensions, you could move this logic inside the load_extension function, or have a predefined list of directories to load from. For example (not tested):

for dir_name in ["...", "...", "..."]:
  for file in os.listdir(dir_name):
    if file.endswith(".py"):
      load_extension(f"{dir_name}.{file}".replace('.py', ''))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions