jokes with images

This commit is contained in:
ddorn 2020-05-10 12:43:21 +02:00
parent 553d124b1a
commit af1f2d1ae2
3 changed files with 21 additions and 16 deletions

View File

@ -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. ||
---

View File

@ -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)

View File

@ -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: