Skip to content

Commit

Permalink
Fix disgusting logic in fetch_aprox_ban_count
Browse files Browse the repository at this point in the history
  • Loading branch information
Iapetus-11 committed May 29, 2024
1 parent c3d4ceb commit b35ab17
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions bot/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,27 +382,23 @@ def item_case(text: str) -> str:
async def fetch_aprox_ban_count(guild: discord.Guild, seconds: float, chunk_size: int = 500) -> str:
assert chunk_size > 0

start_time = time.time()
last_entry = discord.guild.MISSING
start_time: float = time.time()
last_entry: discord.User | None = None
ban_count = 0

while True:
# ran out of time
if (time.time() - start_time) > seconds:
return f"{ban_count}+"

async for entry in guild.bans(limit=500, after=last_entry):
last_entry = entry.user
ban_count += 1

ban_entries_chunk = [e async for e in guild.bans(limit=chunk_size, after=last_entry)]

if ban_entries_chunk:
last_entry = ban_entries_chunk[-1]
last_entry = ban_entries_chunk[-1].user
ban_count += len(ban_entries_chunk)

# there are no more ban entries to fetch
if len(ban_entries_chunk) < chunk_size:
if len(ban_entries_chunk) < chunk_size or last_entry is None:
return str(ban_count)


Expand Down

0 comments on commit b35ab17

Please sign in to comment.