package com.bernard.juliapoll; import static com.bernard.juliapoll.PollManager.pollID; import java.awt.Color; import java.time.temporal.ChronoUnit; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; import com.bernard.juliabot.api.Command; import com.bernard.juliabot.api.JuLIAddon; import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.MessageBuilder; import net.dv8tion.jda.api.entities.Guild; import net.dv8tion.jda.api.entities.IMentionable; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.TextChannel; @JuLIAddon(name = "juliapoll", devs = "Mysaa", version = "20w33a") public class CommandCommander { @Command(name="poll",description="Gère les scrutins",admin=false) public void poll(Guild g, TextChannel chan, Message m){ String[] args = m.getContentRaw().split(" "); if(args.length < 2) { commandReturn(m.getAuthor(), "Cette commande prends au moins un argument : l'action a effectuer (voir !!poll help)", chan); return; } switch(args[0]) { case "create": case "new": case "nouveau": case "créer": case "add": case "ajouter": //!!poll ajouter pollName addable durée votesParPersonne votants(GroupID) couleur(hexa) String pollName; boolean addable = false; int durée = 3600*24; Integer votesParPersonne = 1; Long votants = -1L; Color color = Color.getHSBColor((float)Math.random(), 1.0f, 1.0f); switch(args.length) { default: commandReturn(m.getAuthor(), "Il y a trop d'arguments dans cette commande, je ne prends que les premiers", chan); case 8: try { color = Color.decode(args[7]); }catch(NumberFormatException e) { commandReturn(m.getAuthor(), "J'ai pas compris cette couleur : "+args[7]+" ``` "+e.getLocalizedMessage() + "```", chan); return; } case 7: try { votants = Long.parseLong(args[6]); }catch(NumberFormatException e) { commandReturn(m.getAuthor(), "J'ai pas compris cet ID de groupe ... : "+args[6]+" ``` "+e.getLocalizedMessage() + "```", chan); return; } if(g.getRoleById(votants) == null) commandReturn(m.getAuthor(), "L'ID de groupe "+args[6]+" ne correspond a aucun groupe .... je l'enregistre mais sans conviction", chan); case 6: try { votesParPersonne = Integer.parseInt(args[5]); }catch(NumberFormatException e) { commandReturn(m.getAuthor(), "J'ai pas compris ce nombre de votes ... : "+args[5]+" ``` "+e.getLocalizedMessage() + "```", chan); return; } case 5: if(!Pattern.matches("[0-9]+(s|m|h|j)?",args[4])) { commandReturn(m.getAuthor(), "J'ai pas compris cette durée de scrutin ... : "+args[4]+" \n\t (pour info, la regex à match c'est \"[0-9]+(s|m|h|j)?\"", chan); return; } char suffix = args[4].charAt(args[4].length()-1); String data = args[4]; if(Character.isDigit(suffix)) suffix='s'; else data = data.substring(0, data.length()-2); durée = Integer.parseInt(data); switch(suffix) { case 'j': durée *= 24; case 'h': durée *= 60; case 'm': durée *= 60; } case 4: addable = Pattern.compile("^(true|false|ok|oui|ja|da|okiedokie|d'accord|agree|ouais|vrai|richtig|right|gut)$", Pattern.CASE_INSENSITIVE).matcher(args[3]).find(); case 3: pollName = args[2]; if(!Pattern.matches("^[a-zA-Z0-9-_~.]+$", pollName)) { commandReturn(m.getAuthor(), "ERREUR: nom de scrutin illégal ... : "+args[4]+" \n\t (pour info, la regex à match c'est \"^[a-zA-Z0-9-_~.]+$\"", chan); return; } break; case 2: commandReturn(m.getAuthor(), "Cette commande prends au moins UN argument obligatoire (le nom du poll créé)", chan); return; } Date finalDate = Date.from(new Date().toInstant().plus(durée, ChronoUnit.SECONDS)); JuliaPoll poll = new JuliaPoll(pollName, addable, votesParPersonne, new HashMap<>(), votants, m.getChannel(), finalDate, m.getAuthor().getIdLong(), color, g); PollManager.registerPoll(g, poll); commandReturn(m.getAuthor(), "Poll créé, amusez vous bien !", chan); break; case "option": case "options": case "propositions": case "choix": case "choices": if(args.length < 3) { commandReturn(m.getAuthor(), "Cette commande '"+args[1]+"' prends au moins un argument : le nom du poll a modifier (voir !!poll help)", chan); return; } if(PollManager.checkPoll(pollID(g, args[2]))) { commandReturn(m.getAuthor(), "Je ne connais pas le poll "+args[2], chan); return; } //Parsing Map addedOptions = new HashMap<>(); Set removedOptions = new HashSet<>(); char state = '}'; String emote = "", text = ""; for(int i = 3;i