Skip to content

Commit

Permalink
Improve automatic config cleanup features
Browse files Browse the repository at this point in the history
  • Loading branch information
yiays committed May 27, 2024
1 parent 67a05ee commit e1f55d2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
7 changes: 5 additions & 2 deletions extensions/confessions_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ def get_guildchannels(config:SectionProxy, guild_id:int) -> dict[int, ChannelTyp
)}


def set_guildchannels(config:SectionProxy, guild_id:int, guildchannels:dict[int, ChannelType]):
def set_guildchannels(config:SectionProxy, guild_id:int, guildchannels:dict[int, ChannelType] | None):
""" Writes a dictionary of {channel_id: channel_type} to the config """
config[f'{guild_id}_channels'] = ','.join(f'{k}={int(v)}' for k,v in guildchannels.items())
if guildchannels:
config[f'{guild_id}_channels'] = ','.join(f'{k}={int(v)}' for k,v in guildchannels.items())
else:
config.pop(f'{guild_id}_channels')


# Exceptions
Expand Down
15 changes: 8 additions & 7 deletions extensions/confessions_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,16 @@ async def config_verify(self):
""" Ensure guilds stored in config are still accessible to the bot """
await asyncio.sleep(15)

if self.bot.verbose:
print("Starting lost guild search")
for guild_id in set(k.split('_')[0] for k in self.config):
if self.bot.get_guild(guild_id) is None:
guildchannels = get_guildchannels(self.config, guild_id)
if self.bot.verbose:
if guild_id.isdigit() and self.bot.get_guild(int(guild_id)) is None:
for key in list(s for s in self.config if s.startswith(f'{guild_id}_')):
self.config.pop(key)
if not self.bot.quiet:
print("Removed guild", guild_id, "from config.")
for channel_id in guildchannels:
guildchannels.pop(channel_id)
set_guildchannels(self.config, guild_id, guildchannels)
self.bot.config.save()
#TODO: Also check for deleted channels
self.bot.config.save()

@commands.Cog.listener('on_guild_remove')
async def guild_cleanup(self, guild:disnake.Guild):
Expand Down

0 comments on commit e1f55d2

Please sign in to comment.