Ajout fonctionnalité sondage

This commit is contained in:
Mylloon 2020-11-24 09:20:09 +01:00
parent 518c43e15e
commit d963f743e6

View file

@ -1,9 +1,10 @@
import discord import discord
from discord.ext import commands from discord.ext import commands
from random import randint, choice from random import randint, choice, shuffle
import time import time
from datetime import datetime from datetime import datetime
from pytz import timezone from pytz import timezone
import re
def setup(bot): def setup(bot):
bot.add_cog(Commands(bot)) bot.add_cog(Commands(bot))
@ -246,4 +247,54 @@ class Commands(commands.Cog):
if isinstance(error, commands.CheckFailure): if isinstance(error, commands.CheckFailure):
await ctx.send("Tu n'as pas la permission de faire cette commande, demande à un professeur.") await ctx.send("Tu n'as pas la permission de faire cette commande, demande à un professeur.")
else: else:
await ctx.send("Une erreur est survenue, syntaxe: `.appel [ID salon vocal]`.") await ctx.send("Une erreur est survenue, syntaxe: `.appel [ID salon vocal]`.")
@commands.command(name='sondage')
async def _sondage(self, ctx, *args):
"""Fais un sondage.\n ➡ Syntaxe: .sondage "<Question>" "<Proposition1>" "<Proposition...>" "<Proposition20>" """
args = list(args)
if len(args) > 2:
question = args[0].replace("<@!", "").replace(">", "").replace("<@", "")
for i in re.findall(r'\d+', question):
ii = self.user_or_nick(ctx.author.guild.get_member(int(i)))
try:
question = question.replace(i, ii)
except:
pass
propositions = args[1:]
if len(propositions) <= 20:
message = ""
emojis = {}
emojis[0] = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '🔟']
emojis[1] = [
'🟤', '🔴', '🟠', '🟡', '🟢', '🔵', '🟣', '🔘', '', '💜',
'🟫', '🟥', '🟧', '🟨', '🟩', '🟦', '🟪', '🔳', '🧡', '💙'
]
mixable = True
if len(propositions) <= 10:
emojis_chosen = emojis[randint(0, len(emojis) - 1)]
emojis_chosen = emojis_chosen[:10]
if len(propositions) <= 8:
emojis_chosen = emojis_chosen[:8]
else:
emojis_chosen = emojis[randint(1, len(emojis) - 1)]
if emojis[0][0] in emojis_chosen: # rajouter ici les listes qui ne doivent pas être mélanger
mixable = False
if mixable:
shuffle(emojis_chosen)
for i in range(len(args[1:])):
message += f"{emojis_chosen[i]} -> {propositions[i]}\n"
embed = discord.Embed(title = question, description = message,color = randint(0, 0xFFFFFF)).set_footer(text = self.user_or_nick(ctx.author), icon_url = ctx.author.avatar_url)
sondage = await ctx.send(embed = embed)
for i in range(len(args[1:])):
await sondage.add_reaction(emoji = emojis_chosen[i])
return await ctx.message.add_reaction(emoji = '')
else:
return await ctx.send(f"Désolé, mais tu as mis trop de possibilités (maximum : 20)")
else:
return await ctx.send(f'Désolé, mais il manque des arguments : `.sondage "<Question>" "<Proposition1>" "<Proposition...>" "<Proposition20>"`')
def user_or_nick(self, user):
if user.nick:
return f"{user.nick} ({user.name}#{user.discriminator})"
else:
return f"{user.name}#{user.discriminator}"