266 lines
8.8 KiB
Java
266 lines
8.8 KiB
Java
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<String, String> addedOptions = new HashMap<>();
|
|
Set<String> removedOptions = new HashSet<>();
|
|
char state = '}';
|
|
String emote = "", text = "";
|
|
for(int i = 3;i<args.length;i++) {
|
|
switch(state) {
|
|
case '{':
|
|
|
|
//TODO check if args[i] is emote
|
|
|
|
emote = args[i];
|
|
text = "";
|
|
state = 'e';
|
|
break;
|
|
case 'e':
|
|
|
|
if(args[i].equals("}"))
|
|
if(text.isEmpty())
|
|
removedOptions.add(emote);
|
|
else
|
|
addedOptions.put(emote, text.substring(1));
|
|
|
|
else
|
|
text += " " + args[i];
|
|
|
|
break;
|
|
|
|
case '}':
|
|
if(args[i] != "{") {
|
|
commandReturn(m.getAuthor(), "Ce caractere n'a rien a faire ici, j'attends une nouvelle ouverture '{' (argument " + i + ")", chan);
|
|
return;
|
|
}
|
|
state = '{';
|
|
break;
|
|
}
|
|
}
|
|
|
|
PollManager.manageOptions(pollID(g, args[2]), addedOptions, removedOptions);
|
|
|
|
commandReturn(m.getAuthor(), "All done !", chan);
|
|
|
|
break;
|
|
|
|
case "message":
|
|
case "repost":
|
|
case "infos":
|
|
|
|
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;
|
|
}else if(PollManager.checkPoll(pollID(g, args[2]))) {
|
|
commandReturn(m.getAuthor(), "Je ne connais pas le poll "+args[2], chan);
|
|
return;
|
|
}
|
|
|
|
PollManager.printResults(g, pollID(g, args[2]), chan);
|
|
|
|
break;
|
|
case "end":
|
|
case "terminer":
|
|
case "finir":
|
|
|
|
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;
|
|
}else if(PollManager.checkPoll(pollID(g, args[2]))) {
|
|
commandReturn(m.getAuthor(), "Je ne connais pas le poll "+args[2], chan);
|
|
return;
|
|
}
|
|
|
|
PollManager.endPoll(g, pollID(g,args[2]));
|
|
|
|
commandReturn(m.getAuthor(), "Et hop ! Fin des votes anticipée", chan);
|
|
|
|
break;
|
|
|
|
case "warn":
|
|
case "rappeler":
|
|
case "prevenir":
|
|
|
|
commandReturn(m.getAuthor(), "Cette commande n'a pas encore été mise en place .......... quels devs de mer##", chan);
|
|
|
|
break;
|
|
|
|
case "help":
|
|
case "aide":
|
|
case "ausecours":
|
|
case "?":
|
|
case "42":
|
|
case "quoi?":
|
|
|
|
EmbedBuilder builder = new EmbedBuilder();
|
|
builder.setTitle("Voila les commandes de poll disponible");
|
|
builder.addField("create|créer|add|new|ajouter|nouveau","Crée un noueau poll\n"
|
|
+"\t\t\"pollName\" [\"addable\"] [\"durée\"] [\"votesParPersonne\"] [\"votants (GroupID)\"] [\"couleur(hexa)\"]",false);
|
|
builder.addField("option|options|propositions|choix|choices","Édite les choix proposée par le poll\n"
|
|
+"\t\t\"pollName\" { :emoji: Le conteu de l'option } { :emoji2: Le contenu de l'autre option }",false);
|
|
builder.addField("message|repost|infos","Réenvoie le message du poll\n"
|
|
+"\t\t\"pollName\"",false);
|
|
builder.addField("end|terminer|finir","Termine un poll\n"
|
|
+"\t\t\"pollName\"",false);
|
|
builder.addField("warn|rappeler|prévenir","WIP : Envoie le message de vote en MP a eux qui n'ont pas encore voté\n"
|
|
+"\t\t\"pollName\"",false);
|
|
builder.addField("help|aide|ausecours|?|42|quoi?","Je suis sur que vous savez ce que ca veut dire !\n"
|
|
+"\t\t Vous voulez mettre quoi comme paramètres à un help ?!",false);
|
|
|
|
builder.setColor(Color.getHSBColor((float)Math.random(), 1.0f, 1.0f));
|
|
|
|
chan.sendMessage(builder.build()).complete();
|
|
|
|
break;
|
|
default:
|
|
commandReturn(m.getAuthor(), "Je ne connais pas cette action, regarde le (!!poll help) pour t'aider", chan);
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static void commandReturn(IMentionable sender, String text, TextChannel chan) {
|
|
MessageBuilder builder = new MessageBuilder();
|
|
builder.append(sender);
|
|
builder.append(':');
|
|
builder.append(text);
|
|
chan.sendMessage(builder.build()).complete().delete().completeAfter(15, TimeUnit.SECONDS);
|
|
}
|
|
|
|
}
|