Skip to content

Commit

Permalink
More blocklist fixes (raidensakura#15)
Browse files Browse the repository at this point in the history
* update requirements.txt

* fix blocklist migration detection

* fix migrations.py type hints for python 3.8
  • Loading branch information
khakers committed Jul 25, 2023
1 parent b1cd3cd commit 9bf357d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
12 changes: 7 additions & 5 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,6 @@ def startup(self):
logger.line()
logger.info("discord.py: v%s", discord.__version__)
logger.line()
if not self.config["blocked"] or not self.config["blocked_roles"]:
logger.warning(
"Un-migrated blocklists found. Please run the '[p]migrate blocklist' command after backing "
"up your config/database. Blocklist functionality will be disabled until this is done."
)

async def load_extensions(self):
for cog in self.loaded_cogs:
Expand Down Expand Up @@ -581,6 +576,13 @@ async def on_ready(self):
)
logger.line()

if len(self.config["blocked"]) > 0 or len(self.config["blocked_roles"]) > 0:
logger.warning(
"Un-migrated blocklists found. Please run the '[p]migrate blocklist' command after backing "
"up your config/database. Blocklist functionality will be disabled until this is done."
)
logger.line()

await self.threads.populate_cache()

# closures
Expand Down
13 changes: 9 additions & 4 deletions core/migrations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime
import re
from typing import Optional
from typing import Optional, List

from core import blocklist
from core.models import getLogger
Expand Down Expand Up @@ -76,7 +76,7 @@ def _convert_legacy_block_format(

async def _convert_legacy_block_list(
blocklist_dict: dict,
blocklist_batch: list[blocklist.BlocklistEntry],
blocklist_batch: List[blocklist.BlocklistEntry],
block_type: blocklist.BlockType,
bot,
) -> int:
Expand Down Expand Up @@ -115,6 +115,10 @@ async def migrate_blocklist(bot):
logger.info("preparing to migrate blocklist")
skipped = 0

if len(blocked_users) == 0 and len(bot.blocked_roles) == 0:
logger.info("no blocklist entries to migrate")
return

blocklist_batch: list[blocklist.BlocklistEntry] = []
logger.info(f"preparing to process {len(blocked_users)} blocked users")
skipped += await _convert_legacy_block_list(
Expand All @@ -133,8 +137,9 @@ async def migrate_blocklist(bot):
)
logger.info("processed blocked roles")

await bot.api.db.blocklist.insert_many([x.__dict__ for x in blocklist_batch])
blocklist_batch.clear()
if len(blocklist_batch) > 0:
await bot.api.db.blocklist.insert_many([x.__dict__ for x in blocklist_batch])
blocklist_batch.clear()

logger.info("clearing old blocklists")
bot.blocked_users.clear()
Expand Down
Binary file modified requirements.txt
Binary file not shown.

0 comments on commit 9bf357d

Please sign in to comment.