[V3 Config] Record custom group information using cog_add event (#2550)

* Do things differently

* Uncomment critical lines

* Reduce, reuse, recycle

* Check groups on all new config objects after a cog loads

* I don't know why this is failing now or why we need the global keyword

* gotta fix this too
This commit is contained in:
Will 2019-04-09 22:02:50 -04:00 committed by GitHub
parent e347ffa336
commit ba19179e4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 0 deletions

View File

@ -18,6 +18,8 @@ from .help_formatter import Help, help as help_
from .rpc import RPCMixin from .rpc import RPCMixin
from .utils import common_filters from .utils import common_filters
CUSTOM_GROUPS = "CUSTOM_GROUPS"
def _is_submodule(parent, child): def _is_submodule(parent, child):
return parent == child or child.startswith(parent + ".") return parent == child or child.startswith(parent + ".")
@ -74,6 +76,9 @@ class RedBase(commands.GroupMixin, commands.bot.BotBase, RPCMixin):
self.db.register_user(embeds=None) self.db.register_user(embeds=None)
self.db.init_custom(CUSTOM_GROUPS, 2)
self.db.register_custom(CUSTOM_GROUPS)
async def prefix_manager(bot, message): async def prefix_manager(bot, message):
if not cli_flags.prefix: if not cli_flags.prefix:
global_prefix = await bot.db.prefix() global_prefix = await bot.db.prefix()

View File

@ -12,11 +12,22 @@ from .drivers import get_driver, IdentifierData
if TYPE_CHECKING: if TYPE_CHECKING:
from .drivers.red_base import BaseDriver from .drivers.red_base import BaseDriver
__all__ = ["Config", "get_latest_confs"]
log = logging.getLogger("red.config") log = logging.getLogger("red.config")
_T = TypeVar("_T") _T = TypeVar("_T")
_config_cache = weakref.WeakValueDictionary() _config_cache = weakref.WeakValueDictionary()
_retrieved = weakref.WeakSet()
def get_latest_confs() -> Tuple["Config"]:
global _retrieved
ret = set(_config_cache.values()) - set(_retrieved)
_retrieved |= ret
# noinspection PyTypeChecker
return tuple(ret)
class _ValueCtxManager(Awaitable[_T], AsyncContextManager[_T]): class _ValueCtxManager(Awaitable[_T], AsyncContextManager[_T]):

View File

@ -15,6 +15,7 @@ from pkg_resources import DistributionNotFound
from .. import __version__ as red_version, version_info as red_version_info, VersionInfo from .. import __version__ as red_version, version_info as red_version_info, VersionInfo
from . import commands from . import commands
from .config import get_latest_confs
from .data_manager import storage_type from .data_manager import storage_type
from .utils.chat_formatting import inline, bordered, format_perms_list, humanize_timedelta from .utils.chat_formatting import inline, bordered, format_perms_list, humanize_timedelta
from .utils import fuzzy_command_search, format_fuzzy_results from .utils import fuzzy_command_search, format_fuzzy_results
@ -305,6 +306,14 @@ def init_events(bot, cli_flags):
if command_obj is not None: if command_obj is not None:
command_obj.enable_in(guild) command_obj.enable_in(guild)
@bot.event
async def on_cog_add(cog: commands.Cog):
confs = get_latest_confs()
for c in confs:
uuid = c.unique_identifier
group_data = c.custom_groups
await bot.db.custom("CUSTOM_GROUPS", c.cog_name, uuid).set(group_data)
def _get_startup_screen_specs(): def _get_startup_screen_specs():
"""Get specs for displaying the startup screen on stdout. """Get specs for displaying the startup screen on stdout.