From af1f2d1ae201c6b418655ec4b8456fc462d18ff2 Mon Sep 17 00:00:00 2001 From: ddorn Date: Sun, 10 May 2020 12:43:21 +0200 Subject: [PATCH] :sparkles: jokes with images --- data/jokes | 9 --------- src/cogs/misc.py | 27 ++++++++++++++++++++------- src/constants.py | 1 + 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/data/jokes b/data/jokes index 6798d44..ff34e8a 100644 --- a/data/jokes +++ b/data/jokes @@ -1,12 +1,3 @@ -``` -- Quelle est votre principale qualité ? -- Je suis très rapide en calcul mental. -- 23 x 547 ? -- 56 -- Mais c'est faux ! -- Oui mais c'est rapide ! -``` ---- Why did the chicken cross the mobius strip? || To get to the same side. || --- diff --git a/src/cogs/misc.py b/src/cogs/misc.py index e543fbf..d312122 100644 --- a/src/cogs/misc.py +++ b/src/cogs/misc.py @@ -37,6 +37,7 @@ class Joke(yaml.YAMLObject): joker: int likes: Set[int] = field(default_factory=set) dislikes: Set[int] = field(default_factory=set) + file: str = None class MiscCog(Cog, name="Divers"): @@ -154,14 +155,21 @@ class MiscCog(Cog, name="Divers"): yaml.safe_dump_all(jokes, f) @group(name="joke", invoke_without_command=True) - async def joke(self, ctx): - await ctx.message.delete() + async def joke(self, ctx: Context): + + m: discord.Message = ctx.message + await m.delete() jokes = self.load_jokes() joke_id = random.randrange(len(jokes)) joke = jokes[joke_id] - message: discord.Message = await ctx.send(joke.joke) + if joke.file: + file = discord.File(joke.file) + else: + file = None + + message: discord.Message = await ctx.send(joke.joke, file=file) await message.add_reaction(Emoji.PLUS_1) await message.add_reaction(Emoji.MINUS_1) @@ -171,18 +179,23 @@ class MiscCog(Cog, name="Divers"): @send_and_bin async def new_joke(self, ctx: Context): """Ajoute une blague pour le concours de blague.""" + jokes = self.load_jokes() + joke_id = len(jokes) + author: discord.Member = ctx.author message: discord.Message = ctx.message - start = "!joke new " - msg = message.content[len(start) :] + msg = message.content[len("!joke new ") :] joke = Joke(msg, ctx.author.id, set()) - jokes = self.load_jokes() + if message.attachments: + file: discord.Attachment = message.attachments[0] + joke.file = str(File.MEMES / f"{joke_id}-{file.filename}") + await file.save(joke.file) + jokes.append(joke) self.save_jokes(jokes) - joke_id = len(jokes) - 1 await message.add_reaction(Emoji.PLUS_1) await message.add_reaction(Emoji.MINUS_1) diff --git a/src/constants.py b/src/constants.py index 82a0aee..830d133 100644 --- a/src/constants.py +++ b/src/constants.py @@ -76,6 +76,7 @@ class File: TEAMS = TOP_LEVEL / "data" / "teams" JOKES = TOP_LEVEL / "data" / "jokes" JOKES_V2 = TOP_LEVEL / "data" / "jokesv2" + MEMES = TOP_LEVEL / "data" / "memes" with open(File.TOP_LEVEL / "data" / "problems") as f: