diff options
author | 2024-04-01 13:23:46 +0100 | |
---|---|---|
committer | 2024-04-01 14:10:35 +0100 | |
commit | 7d6c0712327a65c52ed1d99693a9e57ad47ff0be (patch) | |
tree | 959b57506c3e08ee91e9e26d19ef06c5652fd07f | |
parent | Allow extra channels to be ignored by the duck pond cog (#2990) (diff) |
Wait 10 seconds before syncing the guild & users
This will give other cogs a chance to start up
-rw-r--r-- | bot/exts/backend/sync/_cog.py | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/bot/exts/backend/sync/_cog.py b/bot/exts/backend/sync/_cog.py index 83ebb70d2..d14e23ae7 100644 --- a/bot/exts/backend/sync/_cog.py +++ b/bot/exts/backend/sync/_cog.py @@ -1,10 +1,11 @@ import asyncio from typing import Any -from discord import Member, Role, User +from discord import Guild, Member, Role, User from discord.ext import commands from discord.ext.commands import Cog, Context from pydis_core.site_api import ResponseCodeError +from pydis_core.utils.scheduling import create_task from bot import constants from bot.bot import Bot @@ -20,33 +21,39 @@ class Sync(Cog): def __init__(self, bot: Bot) -> None: self.bot = bot + self.guild: Guild = None + async def cog_load(self) -> None: """Syncs the roles/users of the guild with the database.""" await self.bot.wait_until_guild_available() - guild = self.bot.get_guild(constants.Guild.id) - if guild is None: - return + self.guild = self.bot.get_guild(constants.Guild.id) + if self.guild is None: + raise ValueError("Could not fetch guild from cache, not loading sync cog.") attempts = 0 while True: attempts += 1 - if guild.chunked: + if self.guild.chunked: log.info("Guild was found to be chunked after %d attempt(s).", attempts) break if attempts == MAX_ATTEMPTS: log.info("Guild not chunked after %d attempts, calling chunk manually.", MAX_ATTEMPTS) - await guild.chunk() + await self.guild.chunk() break log.info("Attempt %d/%d: Guild not yet chunked, checking again in 10s.", attempts, MAX_ATTEMPTS) await asyncio.sleep(10) + create_task(self.sync()) + + async def sync(self) -> None: + await asyncio.sleep(10) # Give time to other cogs starting up log.info("Starting syncers.") for syncer in (_syncers.RoleSyncer, _syncers.UserSyncer): - await syncer.sync(guild) + await syncer.sync(self.guild) async def patch_user(self, user_id: int, json: dict[str, Any], ignore_404: bool = False) -> None: """Send a PATCH request to partially update a user in the database.""" |