Premier commit - Inclusion dans le projet git
This commit is contained in:
commit
39edd9bee6
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
.classpath
|
||||
.project
|
||||
.settings
|
||||
bin/
|
||||
262
src/com/bernard/juliapoll/CommandCommander.java
Normal file
262
src/com/bernard/juliapoll/CommandCommander.java
Normal file
@ -0,0 +1,262 @@
|
||||
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.discord.api.Command;
|
||||
|
||||
import net.dv8tion.jda.core.EmbedBuilder;
|
||||
import net.dv8tion.jda.core.MessageBuilder;
|
||||
import net.dv8tion.jda.core.entities.Guild;
|
||||
import net.dv8tion.jda.core.entities.IMentionable;
|
||||
import net.dv8tion.jda.core.entities.Message;
|
||||
import net.dv8tion.jda.core.entities.TextChannel;
|
||||
|
||||
public class CommandCommander {
|
||||
|
||||
@Command(name="poll",description="Gère les scrutins",admin=false,group="JuLIAPoll",grp="poll")
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
160
src/com/bernard/juliapoll/JuliaPoll.java
Normal file
160
src/com/bernard/juliapoll/JuliaPoll.java
Normal file
@ -0,0 +1,160 @@
|
||||
package com.bernard.juliapoll;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import net.dv8tion.jda.core.EmbedBuilder;
|
||||
import net.dv8tion.jda.core.entities.Guild;
|
||||
import net.dv8tion.jda.core.entities.MessageChannel;
|
||||
import net.dv8tion.jda.core.entities.MessageEmbed;
|
||||
import net.dv8tion.jda.core.entities.User;
|
||||
|
||||
public class JuliaPoll {
|
||||
|
||||
Guild guild;
|
||||
String nom;
|
||||
boolean addable;
|
||||
int voix;
|
||||
Map<String,String> options; // Emoji -> Option
|
||||
long votants;
|
||||
MessageChannel channel;
|
||||
Date finalTime;
|
||||
long creator;
|
||||
Color color;
|
||||
Map<String, Set<User>> results = new HashMap<>();
|
||||
Map<User, Integer> votes = new HashMap<>();
|
||||
|
||||
|
||||
public JuliaPoll(String nom, boolean addable, int voix, Map<String, String> options, long votants,
|
||||
MessageChannel channel, Date finalTime, long creator, Color color,Guild guild) {
|
||||
this.nom = nom;
|
||||
this.addable = addable;
|
||||
this.voix = voix;
|
||||
this.options = options;
|
||||
this.votants = votants;
|
||||
this.channel = channel;
|
||||
this.finalTime = finalTime;
|
||||
this.creator = creator;
|
||||
this.color = color;
|
||||
this.guild = guild;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public MessageEmbed toMessage() {
|
||||
EmbedBuilder builder = new EmbedBuilder();
|
||||
|
||||
builder.setTitle(this.nom);
|
||||
|
||||
builder.setColor(this.color);
|
||||
|
||||
for(Map.Entry<String,String> e : options.entrySet()) {
|
||||
builder.addField(e.getKey() + " : " + e.getValue(), null, true);
|
||||
}
|
||||
|
||||
builder.setAuthor("Poll de "+guild.getMemberById(creator).getEffectiveName()+" proposé par votre BOT préféré",null,null);
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
public MessageEmbed toResultMessage() {
|
||||
EmbedBuilder builder = new EmbedBuilder();
|
||||
|
||||
builder.setTitle(this.nom);
|
||||
|
||||
builder.setColor(this.color);
|
||||
|
||||
for(Map.Entry<String,String> e : options.entrySet()) {
|
||||
builder.addField(e.getKey() + " : " + results.get(e.getKey()).size() + " voix", null, true);
|
||||
}
|
||||
|
||||
builder.setAuthor("Poll de "+guild.getMemberById(creator).getEffectiveName()+" proposé par votre BOT préféré",null,null);
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public String getNom() {
|
||||
return nom;
|
||||
}
|
||||
public void setNom(String nom) {
|
||||
this.nom = nom;
|
||||
}
|
||||
public boolean isAddable() {
|
||||
return addable;
|
||||
}
|
||||
public void setAddable(boolean addable) {
|
||||
this.addable = addable;
|
||||
}
|
||||
public int getVoix() {
|
||||
return voix;
|
||||
}
|
||||
public void setVoix(int voix) {
|
||||
this.voix = voix;
|
||||
}
|
||||
public Map<String, String> getOptions() {
|
||||
return options;
|
||||
}
|
||||
public long getVotants() {
|
||||
return votants;
|
||||
}
|
||||
public void setVotants(long votants) {
|
||||
this.votants = votants;
|
||||
}
|
||||
public MessageChannel getChannel() {
|
||||
return this.channel;
|
||||
}
|
||||
public void setChannel(MessageChannel channel) {
|
||||
this.channel = channel;
|
||||
}
|
||||
public Date getFinalTime() {
|
||||
return finalTime;
|
||||
}
|
||||
public void setFinalTime(Date finalTime) {
|
||||
this.finalTime = finalTime;
|
||||
}
|
||||
public long getCreator() {
|
||||
return creator;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setCreator(long creator) {
|
||||
this.creator = creator;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Color getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setColor(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public Map<String, Set<User>> getResults() {
|
||||
return results;
|
||||
}
|
||||
|
||||
public Map<User, Integer> getVotes() {
|
||||
return votes;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "JuliaPoll [nom=" + nom + ", addable=" + addable + ", voix=" + voix + ", options=" + options
|
||||
+ ", votants=" + votants + ", channel=" + channel + ", finalTime=" + finalTime + ", creator=" + creator
|
||||
+ ", color=" + color + "]";
|
||||
}
|
||||
|
||||
}
|
||||
135
src/com/bernard/juliapoll/PollManager.java
Normal file
135
src/com/bernard/juliapoll/PollManager.java
Normal file
@ -0,0 +1,135 @@
|
||||
package com.bernard.juliapoll;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import net.dv8tion.jda.core.entities.Guild;
|
||||
import net.dv8tion.jda.core.entities.Message;
|
||||
import net.dv8tion.jda.core.entities.MessageChannel;
|
||||
import net.dv8tion.jda.core.entities.MessageEmbed;
|
||||
import net.dv8tion.jda.core.entities.User;
|
||||
|
||||
public class PollManager {
|
||||
|
||||
static Map<Integer,JuliaPoll> polls;
|
||||
static Map<Message,Integer> messages;
|
||||
static Map<Message,Integer> messagesResult;
|
||||
|
||||
public static boolean checkPoll(int ID) {
|
||||
return polls.containsKey(ID);
|
||||
}
|
||||
|
||||
public static Integer pollID(Guild g, String name) {
|
||||
return (g.getId() + name).hashCode();
|
||||
}
|
||||
|
||||
public static void registerPoll(Guild g, JuliaPoll j) {
|
||||
int ID = pollID(g, j.getNom());
|
||||
polls.put(ID, j);
|
||||
postPollMessage(j.getChannel(), ID);
|
||||
}
|
||||
|
||||
public static void endPoll(Guild g, Integer ID) {
|
||||
JuliaPoll j = polls.get(ID);
|
||||
if(!messagesResult.containsValue(ID))
|
||||
printResults(g, ID, j.getChannel());
|
||||
updatePoll(g, ID);
|
||||
|
||||
|
||||
polls.remove(ID);
|
||||
//Dé référence les messages de vote et de résultat
|
||||
messages.values().removeIf(id -> ID==id);
|
||||
messagesResult.values().removeIf(id -> ID==id);
|
||||
}
|
||||
|
||||
public static void printResults(Guild g, Integer ID, MessageChannel messageChannel) {
|
||||
MessageEmbed message = polls.get(ID).toResultMessage();
|
||||
Message sent = messageChannel.sendMessage(message).complete();
|
||||
messagesResult.put(sent, ID);
|
||||
}
|
||||
|
||||
public static void addOption(Integer ID, String emoji, String option) {
|
||||
polls.get(ID).getOptions().put(emoji, option);
|
||||
}
|
||||
|
||||
public static void manageOptions(Integer ID, Map<String, String> addedOptions, Set<String> removedOptions) {
|
||||
JuliaPoll poll = polls.get(ID);
|
||||
poll.getOptions().putAll(addedOptions);
|
||||
for(String emote : removedOptions) {
|
||||
poll.getOptions().remove(emote);
|
||||
}
|
||||
}
|
||||
|
||||
public static void postPollMessage(MessageChannel messageChannel,Integer ID) {
|
||||
MessageEmbed message = polls.get(ID).toMessage();
|
||||
Message sent = messageChannel.sendMessage(message).complete();
|
||||
messages.put(sent, ID);
|
||||
}
|
||||
|
||||
public static void updatePoll(Guild g,Integer ID) {
|
||||
//edit tous les messages de poll
|
||||
List<Message> toUpdate = getKeysFromValue(messages, ID);
|
||||
List<Message> toResultUpdate = getKeysFromValue(messagesResult, ID);
|
||||
MessageEmbed pollMessage = polls.get(ID).toMessage();
|
||||
MessageEmbed pollResultMessage = polls.get(ID).toResultMessage();
|
||||
|
||||
for(Message m : toUpdate)
|
||||
m.editMessage(pollMessage).queue();
|
||||
for(Message m : toResultUpdate)
|
||||
m.editMessage(pollResultMessage).queue();
|
||||
}
|
||||
|
||||
public static int vote(User u,String vote, Integer ID) {
|
||||
JuliaPoll j = polls.get(ID);
|
||||
if(!j.getVotes().containsKey(u))
|
||||
j.getVotes().put(u, 0);
|
||||
if(j.getVotes().get(u) >= j.getVoix())
|
||||
return -1;//A déjà trop voté
|
||||
if(!j.getResults().containsKey(vote))
|
||||
j.getResults().put(vote, new HashSet<>());
|
||||
if(!j.getResults().get(vote).add(u))
|
||||
return -2;//Avait déjà voté
|
||||
j.getVotes().replace(u,j.getVotes().get(u)+1);
|
||||
return 42;//Tout c'est bien passé
|
||||
}
|
||||
|
||||
public static int unvote(User u,String vote, Integer ID) {
|
||||
JuliaPoll j = polls.get(ID);
|
||||
if(!j.getResults().containsKey(vote))
|
||||
j.getResults().put(vote, new HashSet<>());
|
||||
if(!j.getResults().get(vote).remove(u))
|
||||
return -2;//N'avais pas voté
|
||||
j.getVotes().replace(u,j.getVotes().get(u)-1);//Pas possible de ne pas etre set
|
||||
return 42;//Tout c'est bien passé
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static <K, V> List<K> getKeysFromValue(Map<K, V> mapOfWords, V value)
|
||||
{
|
||||
List<K> listOfKeys = new ArrayList<>();
|
||||
|
||||
// Iterate over each entry of map using entrySet
|
||||
for (Map.Entry<K, V> entry : mapOfWords.entrySet())
|
||||
{
|
||||
// Check if value matches with given value
|
||||
if (entry.getValue().equals(value))
|
||||
{
|
||||
// Store the key from entry to the list
|
||||
listOfKeys.add(entry.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
// Return the list of keys whose value matches with given value.
|
||||
return listOfKeys;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user