Skip to content

Commit

Permalink
- Add a flag that cogs can use to check if the bot is starting up, or…
Browse files Browse the repository at this point in the history
… if the cog is being loaded after the fact

- Change the type of the cog constructors to take a HarmonyBot instead of the superclass
  • Loading branch information
emberdex committed Oct 30, 2023
1 parent f96e128 commit 959c466
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion harmony_cogs/cex.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import harmony_ui.cex

from loguru import logger
from main import HarmonyBot
from discord import app_commands
from discord.ext import commands
from harmony_config import config
Expand All @@ -17,7 +18,7 @@ class CexSearch(commands.Cog):

base_url = "https://wss2.cex.uk.webuy.io/v3/boxes?q=$_SEARCH_QUERY&firstRecord=1&count=50&sortOrder=desc"

def __init__(self, bot: commands.Bot):
def __init__(self, bot: HarmonyBot):
self.bot = bot

try:
Expand Down
3 changes: 2 additions & 1 deletion harmony_cogs/ebay.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import harmony_models.ebay

from loguru import logger
from main import HarmonyBot
from discord import app_commands
from discord.ext import commands
from harmony_config import config
Expand All @@ -21,7 +22,7 @@ class Ebay(commands.Cog):
"&_stpos=M300AA&_sargn=-1%26saslc%3D1&_fsradio2=%26LH_LocatedIn%3D1&_salic=3&LH_SubLocation=1" \
"&_sop=12&_dmd=1&_ipg=60&LH_Complete=1&rt=nc&LH_PrefLoc=1"

def __init__(self, bot: commands.Bot):
def __init__(self, bot: HarmonyBot):
self.bot = bot

try:
Expand Down
3 changes: 2 additions & 1 deletion harmony_cogs/feedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import harmony_ui.feedback

from loguru import logger
from main import HarmonyBot
from discord import app_commands
from discord.ext import commands
from harmony_config import config
Expand All @@ -16,7 +17,7 @@
class Feedback(commands.Cog):
_cog_name = "feedback"

def __init__(self, bot: commands.Bot):
def __init__(self, bot: HarmonyBot):
self.bot = bot

self.feedback_channel = bot.get_guild(discord_guild_id).get_channel(feedback_channel_id)
Expand Down
3 changes: 2 additions & 1 deletion harmony_cogs/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import harmony_ui.verify

from loguru import logger
from main import HarmonyBot
from discord import app_commands
from discord.ext import commands
from harmony_config import config
Expand All @@ -24,7 +25,7 @@
class Verify(commands.Cog):
_cog_name = "verify"

def __init__(self, bot: commands.Bot) -> typing.NoReturn:
def __init__(self, bot: HarmonyBot) -> typing.NoReturn:
self.bot = bot

whois_context_menu = app_commands.ContextMenu(
Expand Down
8 changes: 6 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

class HarmonyBot(commands.Bot):
loaded_cogs: typing.List[typing.Type[commands.Cog]] = []
is_starting_up: bool = True

def __init__(self) -> typing.NoReturn:
intents = discord.Intents.default()
Expand Down Expand Up @@ -53,6 +54,7 @@ async def on_ready(self):

self.loaded_cogs.append(cog_class)

self.is_starting_up = False

bot = HarmonyBot()

Expand Down Expand Up @@ -142,7 +144,8 @@ async def manage_cogs(
try:
await ctx.bot.add_cog(cog_class(ctx.bot))
except Exception as e:
output_message += f"- :no_entry_sign: Failed to load `{cog_name}`: `{e.__name__}`: {str(e)}\n"
output_message += (f"- :no_entry_sign: Failed to load `{cog_name}`: "
f"`{type(e).__name__}`: {str(e)}\n")
continue

ctx.bot.loaded_cogs.append(cog_class)
Expand All @@ -168,7 +171,8 @@ async def manage_cogs(
try:
await ctx.bot.remove_cog(cog_class.__name__)
except Exception as e:
output_message += f"- :no_entry_sign: Failed to unload `{cog_name}`: `{e.__name__}`: {str(e)}\n"
output_message += (f"- :no_entry_sign: Failed to unload `{cog_name}`: "
f"`{type(e).__name__}`: {str(e)}\n")
continue

ctx.bot.loaded_cogs.remove(cog_class)
Expand Down

0 comments on commit 959c466

Please sign in to comment.