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

Commit

Permalink
refactor purge/end
Browse files Browse the repository at this point in the history
  • Loading branch information
Wonderfall committed Apr 24, 2020
1 parent 2aee3d5 commit 20b3280
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,32 +287,44 @@ async def end_tournament(ctx):
await ctx.message.add_reaction("🕐")
return

scheduler.remove_job('underway_tournament')

# Remove underway task
try:
scheduler.remove_job('underway_tournament')
except:
pass

# Annoucements (including results)
await annonce_resultats()
await bot.get_channel(annonce_channel_id).send(
f"{server_logo} Le tournoi **{tournoi['name']}** est terminé, merci à toutes et à tous d'avoir participé ! "
f"J'espère vous revoir bientôt.")

await bot.get_channel(annonce_channel_id).send(f"{server_logo} Le tournoi **{tournoi['name']}** est terminé, merci à toutes et à tous d'avoir participé ! J'espère vous revoir bientôt.")

challenger = ctx.guild.get_role(challenger_id)

for inscrit in participants:
try:
await ctx.guild.get_member(inscrit).remove_roles(challenger)
except discord.HTTPException:
pass

# Reset JSON storage
with open(participants_path, 'w') as f: json.dump({}, f, indent=4)
with open(waiting_list_path, 'w') as f: json.dump({}, f, indent=4)
with open(tournoi_path, 'w') as f: json.dump({}, f, indent=4)
with open(stream_path, 'w') as f: json.dump({}, f, indent=4)

# Remove now obsolete files
for file in list(Path(Path(ranking_path).parent).rglob('*.csv_*')):
await aiofiles.os.remove(file)
for file in list(Path(Path(participants_path).parent).rglob('*.bak')):
await aiofiles.os.remove(file)

# Remove tournament categories
await purge_categories()

# Change presence back to default
await bot.change_presence(activity=discord.Game(f'{name}{version}'))

# Remove all the Challenger roles (it can take time)
challenger = ctx.guild.get_role(challenger_id)
for inscrit in participants:
try:
await ctx.guild.get_member(inscrit).remove_roles(challenger)
except discord.HTTPException:
pass


### S'execute à chaque lancement, permet de relancer les tâches en cas de crash
async def reload_tournament():
Expand Down Expand Up @@ -661,18 +673,22 @@ async def check_in(ctx):
async def purge_channels():
guild = bot.get_guild(id=guild_id)

for category, channels in guild.by_category():
for channel_id in [check_in_channel_id, queue_channel_id, scores_channel_id]:
channel = guild.get_channel(channel_id)
async for message in channel.history():
await message.delete()

if category != None:
await purge_categories()

if category.id == tournoi_cat_id:
for channel in channels:
async for message in channel.history():
await message.delete()

if category.name.lower() in ["winner bracket", "looser bracket"]:
for channel in channels: await channel.delete()
await category.delete()
### Nettoyer les catégories liées aux tournois
async def purge_categories():
guild = bot.get_guild(id=guild_id)

for category, channels in guild.by_category():
if category != None and category.name.lower() in ["winner bracket", "looser bracket"]:
for channel in channels: await channel.delete() # first, delete the channels
await category.delete() # then delete the category


### Affiche le bracket en cours
Expand Down

0 comments on commit 20b3280

Please sign in to comment.