From 34fc5e3c60e8ee6513c1f6ef035a9847a217d0da Mon Sep 17 00:00:00 2001 From: ddorn Date: Thu, 30 Apr 2020 01:04:54 +0200 Subject: [PATCH] :recycle: File and Emoji enum --- src/cogs/misc.py | 12 ++++++------ src/cogs/teams.py | 2 +- src/cogs/tirages.py | 12 ++++++------ src/constants.py | 32 +++++++++++++++++--------------- 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/cogs/misc.py b/src/cogs/misc.py index e2065e3..187ab0f 100644 --- a/src/cogs/misc.py +++ b/src/cogs/misc.py @@ -1,12 +1,11 @@ +import datetime import itertools import random -import datetime from operator import attrgetter -from pprint import pprint from time import time import discord -from discord import Guild, Emoji +from discord import Guild from discord.ext import commands from discord.ext.commands import ( Cog, @@ -19,6 +18,7 @@ from discord.ext.commands import ( ) from src.constants import * +from src.constants import Emoji from src.utils import has_role @@ -47,14 +47,14 @@ class MiscCog(Cog, name="Divers"): @command(name="joke", aliases=["blague"], hidden=True) async def joke_cmd(self, ctx): await ctx.message.delete() - with open(JOKES_FILE) as f: + with open(File.JOKES) as f: jokes = f.read().split("\n\n\n") msg = random.choice(jokes) message: discord.Message = await ctx.send(msg) - await message.add_reaction("😂") - await message.add_reaction("😭") + await message.add_reaction(Emoji.JOY) + await message.add_reaction(Emoji.SOB) @command(name="status") @commands.has_role(Role.CNO) diff --git a/src/cogs/teams.py b/src/cogs/teams.py index 9962f8b..c551944 100644 --- a/src/cogs/teams.py +++ b/src/cogs/teams.py @@ -18,7 +18,7 @@ class TeamsCog(Cog, name="Teams"): self.teams = self.load_teams() def load_teams(self): - with open(TEAMS_FILE) as f: + with open(File.TEAMS) as f: # first line is header lines = f.read().splitlines()[1:] teams = [ diff --git a/src/cogs/tirages.py b/src/cogs/tirages.py index f170ea1..0ef7cf2 100644 --- a/src/cogs/tirages.py +++ b/src/cogs/tirages.py @@ -140,15 +140,15 @@ class Tirage(yaml.YAMLObject): await ctx.channel.edit(overwrites={ctx.guild.default_role: send}) tl = {} - if TIRAGES_FILE.exists(): - with open(TIRAGES_FILE) as f: + if File.TIRAGES.exists(): + with open(File.TIRAGES) as f: tl = yaml.load(f) else: - TIRAGES_FILE.touch() + File.TIRAGES.touch() key = max(0, *tl.keys()) + 1 tl[key] = self - with open(TIRAGES_FILE, "w") as f: + with open(File.TIRAGES, "w") as f: yaml.dump(tl, f) await ctx.send( @@ -785,10 +785,10 @@ class TirageCog(Cog, name="Tirages"): await tirage.update_phase(ctx) def get_tirages(self) -> Dict[int, Tirage]: - if not TIRAGES_FILE.exists(): + if not File.TIRAGES.exists(): return {} - with open(TIRAGES_FILE) as f: + with open(File.TIRAGES) as f: tirages = yaml.load(f) return tirages diff --git a/src/constants.py b/src/constants.py index 6436952..58cde80 100644 --- a/src/constants.py +++ b/src/constants.py @@ -1,5 +1,6 @@ import os from pathlib import Path +from time import time __all__ = [ "TOKEN", @@ -7,17 +8,15 @@ __all__ = [ "PROBLEMS", "MAX_REFUSE", "ROUND_NAMES", - "TIRAGES_FILE", - "TEAMS_FILE", - "JOKES_FILE", "TEAMS_CHANNEL_CATEGORY", "DIEGO", "TOURNOIS", "EMBED_COLOR", "START_TIME", + "File", + "Emoji", ] -from time import time TOKEN = os.environ.get("TFJM_DISCORD_TOKEN") @@ -30,9 +29,12 @@ if TOKEN is None: print() quit(1) +START_TIME = time() GUILD = "690934836696973404" DIEGO = "Diego" # Mon display name - +TEAMS_CHANNEL_CATEGORY = "Channels d'équipes" +EMBED_COLOR = 0xFFA500 +ROUND_NAMES = ["premier tour", "deuxième tour"] TOURNOIS = [ "Lille", "Lyon", @@ -56,18 +58,18 @@ class Role: PARTICIPANT = "Participant" -TEAMS_CHANNEL_CATEGORY = "Channels d'équipes" +class Emoji: + JOY = "😂" + SOB = "😭" -ROUND_NAMES = ["premier tour", "deuxième tour"] -TOP_LEVEL_DIR = Path(__file__).parent.parent -TIRAGES_FILE = TOP_LEVEL_DIR / "data" / "tirages.yaml" -TEAMS_FILE = TOP_LEVEL_DIR / "data" / "teams" -JOKES_FILE = TOP_LEVEL_DIR / "data" / "jokes" +class File: + TOP_LEVEL = Path(__file__).parent.parent + TIRAGES = TOP_LEVEL / "data" / "tirages.yaml" + TEAMS = TOP_LEVEL / "data" / "teams" + JOKES = TOP_LEVEL / "data" / "jokes" -with open(TOP_LEVEL_DIR / "data" / "problems") as f: + +with open(File.TOP_LEVEL / "data" / "problems") as f: PROBLEMS = f.read().splitlines() MAX_REFUSE = len(PROBLEMS) - 4 # -5 usually but not in 2020 because of covid-19 - -EMBED_COLOR = 0xFFA500 -START_TIME = time()