✨ better joke system
This commit is contained in:
parent
c13aa6bec3
commit
7dd6ed2273
|
@ -47,6 +47,9 @@ class DevCog(Cog, name="Dev tools"):
|
||||||
# Utility functions
|
# Utility functions
|
||||||
|
|
||||||
def send(msg, channel=None):
|
def send(msg, channel=None):
|
||||||
|
if isinstance(channel, int):
|
||||||
|
channel = get(ctx.guild.channels, id=channel)
|
||||||
|
|
||||||
channel = channel or ctx.channel
|
channel = channel or ctx.channel
|
||||||
asyncio.create_task(channel.send(msg))
|
asyncio.create_task(channel.send(msg))
|
||||||
|
|
||||||
|
|
109
src/cogs/misc.py
109
src/cogs/misc.py
|
@ -2,11 +2,14 @@ import datetime
|
||||||
import io
|
import io
|
||||||
import itertools
|
import itertools
|
||||||
import random
|
import random
|
||||||
|
from dataclasses import dataclass, field
|
||||||
from operator import attrgetter
|
from operator import attrgetter
|
||||||
from time import time
|
from time import time
|
||||||
|
from typing import List, Set
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import discord
|
import discord
|
||||||
|
import yaml
|
||||||
from discord import Guild
|
from discord import Guild
|
||||||
from discord.ext import commands
|
from discord.ext import commands
|
||||||
from discord.ext.commands import (
|
from discord.ext.commands import (
|
||||||
|
@ -16,12 +19,24 @@ from discord.ext.commands import (
|
||||||
Command,
|
Command,
|
||||||
CommandError,
|
CommandError,
|
||||||
Group,
|
Group,
|
||||||
|
group,
|
||||||
)
|
)
|
||||||
|
|
||||||
from src.constants import *
|
from src.constants import *
|
||||||
from src.constants import Emoji
|
from src.constants import Emoji
|
||||||
from src.core import CustomBot
|
from src.core import CustomBot
|
||||||
from src.utils import has_role, start_time
|
from src.utils import has_role, start_time, send_and_bin
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Joke(yaml.YAMLObject):
|
||||||
|
yaml_tag = "Joke"
|
||||||
|
yaml_dumper = yaml.SafeDumper
|
||||||
|
yaml_loader = yaml.SafeLoader
|
||||||
|
joke: str
|
||||||
|
joker: int
|
||||||
|
likes: Set[int] = field(default_factory=set)
|
||||||
|
dislikes: Set[int] = field(default_factory=set)
|
||||||
|
|
||||||
|
|
||||||
class MiscCog(Cog, name="Divers"):
|
class MiscCog(Cog, name="Divers"):
|
||||||
|
@ -47,19 +62,6 @@ class MiscCog(Cog, name="Divers"):
|
||||||
msg = await ctx.send(f"J'ai choisi... **{choice}**")
|
msg = await ctx.send(f"J'ai choisi... **{choice}**")
|
||||||
await self.bot.wait_for_bin(ctx.author, msg),
|
await self.bot.wait_for_bin(ctx.author, msg),
|
||||||
|
|
||||||
@command(name="joke", aliases=["blague"], hidden=True)
|
|
||||||
async def joke_cmd(self, ctx):
|
|
||||||
await ctx.message.delete()
|
|
||||||
with open(File.JOKES) as f:
|
|
||||||
jokes = f.read().split("---")
|
|
||||||
|
|
||||||
msg = random.choice(jokes)
|
|
||||||
message: discord.Message = await ctx.send(msg)
|
|
||||||
|
|
||||||
await message.add_reaction(Emoji.JOY)
|
|
||||||
await message.add_reaction(Emoji.SOB)
|
|
||||||
await self.bot.wait_for_bin(ctx.message.author, message)
|
|
||||||
|
|
||||||
@command(name="status")
|
@command(name="status")
|
||||||
@commands.has_role(Role.CNO)
|
@commands.has_role(Role.CNO)
|
||||||
async def status_cmd(self, ctx: Context):
|
async def status_cmd(self, ctx: Context):
|
||||||
|
@ -70,12 +72,15 @@ class MiscCog(Cog, name="Divers"):
|
||||||
participants = [g for g in guild.members if has_role(g, Role.PARTICIPANT)]
|
participants = [g for g in guild.members if has_role(g, Role.PARTICIPANT)]
|
||||||
no_role = [g for g in guild.members if g.top_role == guild.default_role]
|
no_role = [g for g in guild.members if g.top_role == guild.default_role]
|
||||||
uptime = datetime.timedelta(seconds=round(time() - start_time()))
|
uptime = datetime.timedelta(seconds=round(time() - start_time()))
|
||||||
|
text = len(guild.text_channels)
|
||||||
|
vocal = len(guild.voice_channels)
|
||||||
infos = {
|
infos = {
|
||||||
"Bénévoles": len(benevoles),
|
"Bénévoles": len(benevoles),
|
||||||
"Participants": len(participants),
|
"Participants": len(participants),
|
||||||
"Sans rôle": len(no_role),
|
"Sans rôle": len(no_role),
|
||||||
"Total": len(guild.members),
|
"Total": len(guild.members),
|
||||||
|
"Salons texte": text,
|
||||||
|
"Salons vocaux": vocal,
|
||||||
"Bot uptime": uptime,
|
"Bot uptime": uptime,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,6 +104,80 @@ class MiscCog(Cog, name="Divers"):
|
||||||
data = io.BytesIO(await resp.read())
|
data = io.BytesIO(await resp.read())
|
||||||
await ctx.send(file=discord.File(data, "cool_image.png"))
|
await ctx.send(file=discord.File(data, "cool_image.png"))
|
||||||
|
|
||||||
|
# ---------------- Jokes ---------------- #
|
||||||
|
|
||||||
|
def load_jokes(self) -> List[Joke]:
|
||||||
|
# Ensure it exists
|
||||||
|
File.JOKES_V2.touch()
|
||||||
|
with open(File.JOKES_V2) as f:
|
||||||
|
jokes = list(yaml.safe_load_all(f))
|
||||||
|
|
||||||
|
return jokes
|
||||||
|
|
||||||
|
def save_jokes(self, jokes):
|
||||||
|
File.JOKES_V2.touch()
|
||||||
|
with open(File.JOKES_V2, "w") as f:
|
||||||
|
yaml.safe_dump_all(jokes, f)
|
||||||
|
|
||||||
|
@group(name="joke", hidden=True, invoke_without_command=True)
|
||||||
|
async def joke(self, ctx):
|
||||||
|
await ctx.message.delete()
|
||||||
|
|
||||||
|
jokes = self.load_jokes()
|
||||||
|
joke_id = random.randrange(len(jokes))
|
||||||
|
joke = jokes[joke_id]
|
||||||
|
|
||||||
|
message: discord.Message = await ctx.send(joke.joke)
|
||||||
|
|
||||||
|
await message.add_reaction(Emoji.PLUS_1)
|
||||||
|
await message.add_reaction(Emoji.MINUS_1)
|
||||||
|
await self.wait_for_joke_reactions(joke_id, message)
|
||||||
|
|
||||||
|
@joke.command(name="new", hidden=True)
|
||||||
|
@send_and_bin
|
||||||
|
async def new_joke(self, ctx: Context):
|
||||||
|
author: discord.Member = ctx.author
|
||||||
|
message: discord.Message = ctx.message
|
||||||
|
|
||||||
|
start = "!joke new "
|
||||||
|
msg = message.content[len(start) :]
|
||||||
|
|
||||||
|
joke = Joke(msg, ctx.author.id, set())
|
||||||
|
|
||||||
|
jokes = self.load_jokes()
|
||||||
|
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)
|
||||||
|
|
||||||
|
await self.wait_for_joke_reactions(joke_id, message)
|
||||||
|
|
||||||
|
async def wait_for_joke_reactions(self, joke_id, message):
|
||||||
|
def check(reaction: discord.Reaction, u):
|
||||||
|
return (message.id == reaction.message.id) and str(reaction.emoji) in (
|
||||||
|
Emoji.PLUS_1,
|
||||||
|
Emoji.MINUS_1,
|
||||||
|
)
|
||||||
|
|
||||||
|
start = time()
|
||||||
|
end = start + 24 * 60 * 60
|
||||||
|
while time() < end:
|
||||||
|
reaction, user = await self.bot.wait_for(
|
||||||
|
"reaction_add", check=check, timeout=end - time()
|
||||||
|
)
|
||||||
|
|
||||||
|
if user.id == BOT:
|
||||||
|
continue
|
||||||
|
|
||||||
|
jokes = self.load_jokes()
|
||||||
|
if str(reaction.emoji) == Emoji.PLUS_1:
|
||||||
|
jokes[joke_id].likes.add(user.id)
|
||||||
|
else:
|
||||||
|
jokes[joke_id].dislikes.add(user.id)
|
||||||
|
|
||||||
|
self.save_jokes(jokes)
|
||||||
|
|
||||||
# ----------------- Help ---------------- #
|
# ----------------- Help ---------------- #
|
||||||
|
|
||||||
@command(name="help", aliases=["h"])
|
@command(name="help", aliases=["h"])
|
||||||
|
|
|
@ -189,17 +189,18 @@ class TeamsCog(Cog, name="Teams"):
|
||||||
f"est {ctx.author.mention}"
|
f"est {ctx.author.mention}"
|
||||||
)
|
)
|
||||||
|
|
||||||
diego = get(ctx.guild.members, display_name=DIEGO, top_role__name=Role.CNO)
|
diego = get(ctx.guild.members, id=DIEGO)
|
||||||
await ctx.author.send(
|
await ctx.author.send(
|
||||||
f"Salut Capitaine !\n"
|
"Salut Capitaine !\n"
|
||||||
"On va être amené à faire de nombreuses choses ensemble "
|
"On va être amené à faire de nombreuses choses ensemble "
|
||||||
"ces prochains jours, donc n'hésite pas à abuser de `!help`. "
|
"ces prochains jours, donc n'hésite pas à abuser de `!help`. "
|
||||||
"Tu peux l'utiliser ici mais malheureusement tu ne pourra pas voir "
|
"Tu peux l'utiliser ici mais malheureusement tu ne pourra pas voir "
|
||||||
"les commandes qui sont réservés aux capitaines. \n"
|
"les commandes qui sont réservés aux capitaines. \n"
|
||||||
"Une commande que tu peux avoir envie d'utiliser c'est "
|
"Une commande que tu peux avoir envie d'utiliser c'est "
|
||||||
"`!team channel un-super-nom` pour créer une channel réservée à "
|
"`!team channel un-super-nom` pour créer une channel réservée à "
|
||||||
"ton équipe. \n\n"
|
"ton équipe. `!team voice un-super-nom` permet "
|
||||||
f"Si tu as des suggestions pour que le bot permette à chacun d'avoir "
|
"aussi de créer un salon vocal :wink: \n\n"
|
||||||
|
"Si tu as des suggestions pour que le bot permette à chacun d'avoir "
|
||||||
f"une meilleure expérience ici, envoie un petit message à {diego.mention} ;)"
|
f"une meilleure expérience ici, envoie un petit message à {diego.mention} ;)"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -265,7 +266,7 @@ class TeamsCog(Cog, name="Teams"):
|
||||||
|
|
||||||
if not channel_name:
|
if not channel_name:
|
||||||
return (
|
return (
|
||||||
"Tu dois mettre un nom d'équipe, par exemple "
|
"Tu dois mettre un nom de salon, par exemple "
|
||||||
"`!team channel un-super-nom`"
|
"`!team channel un-super-nom`"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -302,7 +303,7 @@ class TeamsCog(Cog, name="Teams"):
|
||||||
|
|
||||||
if not channel_name:
|
if not channel_name:
|
||||||
return (
|
return (
|
||||||
"Tu dois mettre un nom d'équipe, par exemple "
|
"Tu dois mettre un nom de salon, par exemple "
|
||||||
"`!team voice un-super-nom`"
|
"`!team voice un-super-nom`"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -845,6 +845,7 @@ class TirageCog(Cog, name="Tirages"):
|
||||||
@draw_group.command(name="dump")
|
@draw_group.command(name="dump")
|
||||||
@commands.has_role(Role.DEV)
|
@commands.has_role(Role.DEV)
|
||||||
async def dump_cmd(self, ctx, tirage_id: int, round=0):
|
async def dump_cmd(self, ctx, tirage_id: int, round=0):
|
||||||
|
"""Affiche un résumé succint d'un tirage."""
|
||||||
tirages = self.get_tirages()
|
tirages = self.get_tirages()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -864,11 +865,6 @@ class TirageCog(Cog, name="Tirages"):
|
||||||
|
|
||||||
await ctx.send(msg)
|
await ctx.send(msg)
|
||||||
|
|
||||||
@draw_group.command()
|
|
||||||
@commands.has_role(Role.DEV)
|
|
||||||
async def debug(self, ctx, id: int):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def setup(bot):
|
def setup(bot):
|
||||||
bot.add_cog(TirageCog(bot))
|
bot.add_cog(TirageCog(bot))
|
||||||
|
|
|
@ -10,6 +10,7 @@ __all__ = [
|
||||||
"ROUND_NAMES",
|
"ROUND_NAMES",
|
||||||
"TEAMS_CHANNEL_CATEGORY",
|
"TEAMS_CHANNEL_CATEGORY",
|
||||||
"DIEGO",
|
"DIEGO",
|
||||||
|
"BOT",
|
||||||
"TOURNOIS",
|
"TOURNOIS",
|
||||||
"EMBED_COLOR",
|
"EMBED_COLOR",
|
||||||
"FRACTAL_URL",
|
"FRACTAL_URL",
|
||||||
|
@ -30,7 +31,8 @@ if TOKEN is None:
|
||||||
quit(1)
|
quit(1)
|
||||||
|
|
||||||
GUILD = "690934836696973404"
|
GUILD = "690934836696973404"
|
||||||
DIEGO = "Diego" # Mon display name
|
DIEGO = 430566197868625920 # Mon id
|
||||||
|
BOT = 703305132300959754
|
||||||
TEAMS_CHANNEL_CATEGORY = "Channels d'équipes"
|
TEAMS_CHANNEL_CATEGORY = "Channels d'équipes"
|
||||||
EMBED_COLOR = 0xFFA500
|
EMBED_COLOR = 0xFFA500
|
||||||
FRACTAL_URL = "https://thefractal.space/img/{seed}.png?size=1500"
|
FRACTAL_URL = "https://thefractal.space/img/{seed}.png?size=1500"
|
||||||
|
@ -65,6 +67,8 @@ class Emoji:
|
||||||
BIN = "🗑️"
|
BIN = "🗑️"
|
||||||
DICE = "🎲"
|
DICE = "🎲"
|
||||||
CHECK = "✅"
|
CHECK = "✅"
|
||||||
|
PLUS_1 = "👍"
|
||||||
|
MINUS_1 = "👎"
|
||||||
|
|
||||||
|
|
||||||
class File:
|
class File:
|
||||||
|
@ -72,6 +76,7 @@ class File:
|
||||||
TIRAGES = TOP_LEVEL / "data" / "tirages.yaml"
|
TIRAGES = TOP_LEVEL / "data" / "tirages.yaml"
|
||||||
TEAMS = TOP_LEVEL / "data" / "teams"
|
TEAMS = TOP_LEVEL / "data" / "teams"
|
||||||
JOKES = TOP_LEVEL / "data" / "jokes"
|
JOKES = TOP_LEVEL / "data" / "jokes"
|
||||||
|
JOKES_V2 = TOP_LEVEL / "data" / "jokesv2"
|
||||||
|
|
||||||
|
|
||||||
with open(File.TOP_LEVEL / "data" / "problems") as f:
|
with open(File.TOP_LEVEL / "data" / "problems") as f:
|
||||||
|
|
|
@ -2,7 +2,7 @@ import asyncio
|
||||||
import sys
|
import sys
|
||||||
from importlib import reload
|
from importlib import reload
|
||||||
|
|
||||||
from discord import User, Message, Reaction
|
from discord import User, Message, Reaction, NotFound
|
||||||
from discord.ext.commands import Bot
|
from discord.ext.commands import Bot
|
||||||
|
|
||||||
__all__ = ["CustomBot"]
|
__all__ = ["CustomBot"]
|
||||||
|
@ -67,4 +67,8 @@ class CustomBot(Bot):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
for m in msgs:
|
for m in msgs:
|
||||||
await m.clear_reaction(Emoji.BIN)
|
try:
|
||||||
|
await m.clear_reaction(Emoji.BIN)
|
||||||
|
except NotFound:
|
||||||
|
# Message or reaction deleted
|
||||||
|
pass
|
||||||
|
|
Loading…
Reference in New Issue