Skip to content
This repository was archived by the owner on Oct 18, 2020. It is now read-only.

Commit 7ff7c17

Browse files
committed
refactor purge
1 parent b7395f9 commit 7ff7c17

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

bot.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import discord, random, logging, os, json, re, achallonge, dateutil.parser, dateutil.relativedelta, datetime, time, asyncio, yaml
22
import aiofiles, aiofiles.os
33
from apscheduler.schedulers.asyncio import AsyncIOScheduler
4+
from apscheduler.jobstores.base import JobLookupError
45
from babel.dates import format_date, format_time
56
from discord.ext import commands
67
from pathlib import Path
@@ -278,7 +279,6 @@ async def start_tournament(ctx):
278279
@commands.check(tournament_is_underway)
279280
async def end_tournament(ctx):
280281
with open(tournoi_path, 'r+') as f: tournoi = json.load(f, object_hook=dateparser)
281-
with open(participants_path, 'r+') as f: participants = json.load(f, object_pairs_hook=int_keys)
282282

283283
if datetime.datetime.now() > tournoi["début_tournoi"]:
284284
await async_http_retry(achallonge.tournaments.finalize, tournoi["id"])
@@ -290,7 +290,7 @@ async def end_tournament(ctx):
290290
# Remove underway task
291291
try:
292292
scheduler.remove_job('underway_tournament')
293-
except:
293+
except JobLookupError:
294294
pass
295295

296296
# Annoucements (including results)
@@ -311,19 +311,12 @@ async def end_tournament(ctx):
311311
for file in list(Path(Path(participants_path).parent).rglob('*.bak')):
312312
await aiofiles.os.remove(file)
313313

314-
# Remove tournament categories
315-
await purge_categories()
316-
317314
# Change presence back to default
318315
await bot.change_presence(activity=discord.Game(f'{name}{version}'))
319316

320-
# Remove all the Challenger roles (it can take time)
321-
challenger = ctx.guild.get_role(challenger_id)
322-
for inscrit in participants:
323-
try:
324-
await ctx.guild.get_member(inscrit).remove_roles(challenger)
325-
except discord.HTTPException:
326-
pass
317+
# Remove tournament roles & categories
318+
await purge_categories()
319+
await purge_roles()
327320

328321

329322
### S'execute à chaque lancement, permet de relancer les tâches en cas de crash
@@ -613,7 +606,7 @@ async def rappel_check_in():
613606
del rappel_msg[:50] # and send by groups of 50 people
614607

615608
await bot.get_channel(check_in_channel_id).send(f"*Vous avez jusqu'à {format_time(tournoi['fin_check-in'], format='short', locale=language)}, sinon vous serez désinscrit(s) automatiquement.*")
616-
609+
617610

618611
### Fin du check-in
619612
async def end_check_in():
@@ -625,7 +618,7 @@ async def end_check_in():
625618

626619
try:
627620
scheduler.remove_job('rappel_check_in')
628-
except:
621+
except JobLookupError:
629622
pass
630623

631624
for inscrit in list(participants):
@@ -675,10 +668,7 @@ async def purge_channels():
675668

676669
for channel_id in [check_in_channel_id, queue_channel_id, scores_channel_id]:
677670
channel = guild.get_channel(channel_id)
678-
async for message in channel.history():
679-
await message.delete()
680-
681-
await purge_categories()
671+
await channel.purge(limit=None)
682672

683673

684674
### Nettoyer les catégories liées aux tournois
@@ -687,10 +677,23 @@ async def purge_categories():
687677

688678
for category, channels in guild.by_category():
689679
if category != None and category.name.lower() in ["winner bracket", "looser bracket"]:
690-
for channel in channels: await channel.delete() # first, delete the channels
680+
for channel in channels:
681+
await channel.delete() # first, delete the channels
691682
await category.delete() # then delete the category
692683

693684

685+
### Nettoyer les rôles liés aux tournois
686+
async def purge_roles():
687+
guild = bot.get_guild(id=guild_id)
688+
challenger = guild.get_role(id=challenger_id)
689+
690+
for member in challenger.members:
691+
try:
692+
await member.remove_roles(challenger)
693+
except (discord.HTTPException, discord.Forbidden):
694+
pass
695+
696+
694697
### Affiche le bracket en cours
695698
@bot.command(name='bracket')
696699
@commands.check(tournament_is_underway_or_pending)

0 commit comments

Comments
 (0)