aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2020-10-16 14:42:09 -0700
committerGravatar MarkKoz <[email protected]>2020-10-16 14:42:09 -0700
commit952f30f7cce351337c36655f4ff81e7e86d02b00 (patch)
tree4a0959d1ad97b9ecd1fed7f4e752db1fbf7e0ca6
parentSnekbox: raise paste character length (diff)
Add global bot instance
Is **very** convenient when writing utility functions that rely on the bot's state, but aren't in cogs and therefore lack the typical way to access the instance. No more passing around of the instance as an arg!
-rw-r--r--bot/__init__.py6
-rw-r--r--bot/__main__.py7
2 files changed, 10 insertions, 3 deletions
diff --git a/bot/__init__.py b/bot/__init__.py
index 3ee70c4e9..0642b2c5d 100644
--- a/bot/__init__.py
+++ b/bot/__init__.py
@@ -5,12 +5,16 @@ import sys
from functools import partial, partialmethod
from logging import Logger, handlers
from pathlib import Path
+from typing import TYPE_CHECKING
import coloredlogs
from discord.ext import commands
from bot.command import Command
+if TYPE_CHECKING:
+ from bot.bot import Bot
+
TRACE_LEVEL = logging.TRACE = 5
logging.addLevelName(TRACE_LEVEL, "TRACE")
@@ -76,3 +80,5 @@ if os.name == "nt":
# Must be patched before any cogs are added.
commands.command = partial(commands.command, cls=Command)
commands.GroupMixin.command = partialmethod(commands.GroupMixin.command, cls=Command)
+
+instance: "Bot" = None # Global Bot instance.
diff --git a/bot/__main__.py b/bot/__main__.py
index 367be1300..9d48c9092 100644
--- a/bot/__main__.py
+++ b/bot/__main__.py
@@ -9,6 +9,7 @@ from sentry_sdk.integrations.aiohttp import AioHttpIntegration
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.integrations.redis import RedisIntegration
+import bot
from bot import constants
from bot.bot import Bot
from bot.utils.extensions import EXTENSIONS
@@ -54,7 +55,7 @@ intents.dm_reactions = False
intents.invites = False
intents.webhooks = False
intents.integrations = False
-bot = Bot(
+bot.instance = Bot(
redis_session=redis_session,
loop=loop,
command_prefix=when_mentioned_or(constants.Bot.prefix),
@@ -71,6 +72,6 @@ if not constants.HelpChannels.enable:
extensions.remove("bot.exts.help_channels")
for extension in extensions:
- bot.load_extension(extension)
+ bot.instance.load_extension(extension)
-bot.run(constants.Bot.token)
+bot.instance.run(constants.Bot.token)