Petite mise à jour des évenements

This commit is contained in:
Mysaa 2021-05-23 02:47:29 +02:00
parent 59f22e5edb
commit c456837148
10 changed files with 805 additions and 363 deletions

View File

@ -73,14 +73,14 @@ dependencies {
compile 'org.slf4j:slf4j-nop:1.7.25'
compile 'net.dv8tion:JDA:3.8.1_439'
compile 'net.dv8tion:JDA:4.2.0_189'
compile 'com.thedeanda:lorem:2.1'
compile 'commons-io:commons-io:2.6'
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.15'
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.21'
}

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,29 @@
package com.bernard.juliabot;
import java.io.*;
import java.lang.reflect.*;
import java.sql.*;
import java.util.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Scanner;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
@ -12,11 +32,15 @@ import org.apache.commons.io.output.TeeOutputStream;
import com.bernard.juliabot.JuliaAddon.JarWithMultipleAddonsException;
import com.bernard.juliabot.JuliaAddon.JuliaClassLoader;
import com.bernard.juliabot.api.*;
import com.bernard.juliabot.api.CCommande;
import com.bernard.juliabot.api.Command;
import com.bernard.juliabot.api.Discord;
import com.thedeanda.lorem.LoremIpsum;
import net.dv8tion.jda.core.*;
import net.dv8tion.jda.core.events.Event;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.events.Event;
import net.dv8tion.jda.api.events.GenericEvent;
public class Julia {
@ -24,20 +48,20 @@ public class Julia {
public static final String sysinPipe = "/home/julia/entree";
private static final String token = "";
public static final String CONNECTION_DATA_FILEPATH = "/var/julia/julia.conn";
static Julia theJulia;
public JDA jda;
JDA jda;
Map<String,JuliaAddon> avalivableAddons;// pkg+version->addon
Map<Long,Character> laboratoriesIdentifieurs;//Store channels IDs ...
public Map<Character,Laboratory> laboratoires;
Map<Character,Laboratory> laboratoires;
Map<String,Long> fileTrack;//<File name in juliaddonFolder, lastModified>
public EcouteurDEvents lecouteur;
EcouteurDEvents lecouteur;
ReadLoop sysinator;
Connection eventDatabase;
@ -57,21 +81,29 @@ public class Julia {
}
public synchronized void startup() {
BufferedReader bis;
try {
bis = new BufferedReader(new FileReader(CONNECTION_DATA_FILEPATH));
String token = bis.readLine();
//Set up mysql cnnections (events & data)
Properties connectionProps = new Properties();
connectionProps.put("user", "julia");
connectionProps.put("password", "juliabestbotever");
connectionProps.put("user", bis.readLine());
connectionProps.put("password", bis.readLine());
connectionProps.put("serverTimezone", "UTC");
connectionProps.put("verifyServerCertificate", "false");
connectionProps.put("useSSL", "true");
connectionProps.put("requireSSL", "true");
String urlAndPort = bis.readLine();
try {
eventDatabase = DriverManager.getConnection("jdbc:mysql://192.168.1.41:3306/juliaEvents", connectionProps);
juliaDatabase = DriverManager.getConnection("jdbc:mysql://192.168.1.41:3306/julia", connectionProps);
eventDatabase = DriverManager.getConnection("jdbc:mysql://"+urlAndPort+"/juliaEvents", connectionProps);
juliaDatabase = DriverManager.getConnection("jdbc:mysql://"+urlAndPort+"/julia", connectionProps);
} catch (SQLException e) {
System.err.println("Impossible de se connecter a la BDD, ca ne sers a rien de lancer JuL'IA du coup ... Bonne nuit");
e.printStackTrace();
syserr.flush();
bis.close();
return;
}
@ -81,18 +113,24 @@ public class Julia {
// Démarrage de JDA et du Bot
try {
lecouteur = new EcouteurDEvents(this);
jda = new JDABuilder(AccountType.BOT).setToken(token).addEventListener(lecouteur).build();
jda.awaitReady();
jda = JDABuilder.createDefault(token).addEventListeners(getLecouteur()).build();
getJda().awaitReady();
} catch(Exception e) {
System.err.println("Impossible de démarrer JuL'IA");
e.printStackTrace();
syserr.flush();
bis.close();
return;
}
jda.getGuildById(222947179017404416L).getTextChannelById(460935684669046784L).sendMessage(LoremIpsum.getInstance().getWords(10, 15)).complete();
long guildHello = Long.parseLong(bis.readLine());
long textChannelHello = Long.parseLong(bis.readLine());
getJda().getGuildById(guildHello).getTextChannelById(textChannelHello).sendMessage(LoremIpsum.getInstance().getWords(10, 15)).complete();
syserr = new JuliaErrPrintStream(jda.getGuildById(222947179017404416L).getTextChannelById(576469792735756309L));
long guildErr = Long.parseLong(bis.readLine());
long textChannelErr = Long.parseLong(bis.readLine());
syserr = new JuliaErrPrintStream(getJda().getGuildById(guildErr).getTextChannelById(textChannelErr));
TeeOutputStream tos = new TeeOutputStream(System.err, syserr.printStream());
System.setErr(new PrintStream(tos));
@ -103,6 +141,15 @@ public class Julia {
public void run() {Julia.theJulia().syserr.flushMessage();}
} , 0, 1000);
bis.close();
} catch (FileNotFoundException e1) {
System.err.println("Euhhhhh je n'ai pas trouvé le fichier "+CONNECTION_DATA_FILEPATH+" et je ne peux donc pas lire ma carte d'identitée");
e1.printStackTrace();
} catch (IOException e1) {
System.err.println("Euhhhhh je n'ai pas réussi à lire le fichier "+CONNECTION_DATA_FILEPATH+" et je ne peux donc pas lire ma carte d'identitée");
e1.printStackTrace();
}
//Launch update to see every addon in juliaddon/
update();//Va remplir la map avalivableAddons
@ -143,9 +190,9 @@ public class Julia {
}
for(String l : toLoad) {
if(!laboratoires.containsKey(l.charAt(0)))
laboratoires.put(l.charAt(0), new Laboratory(l.charAt(0)));
Laboratory labo = laboratoires.get(l.charAt(0));
if(!getLaboratoires().containsKey(l.charAt(0)))
getLaboratoires().put(l.charAt(0), new Laboratory(l.charAt(0)));
Laboratory labo = getLaboratoires().get(l.charAt(0));
labo.loadAddon(l.substring(1, l.lastIndexOf(":")), l.substring(l.lastIndexOf(":")+1), false);//Pas besoin d'update, il viens d'etre fait
}
@ -402,7 +449,7 @@ public class Julia {
params[i] = Julia.this.juliaDatabase;
break;
case "net.dv8tion.jda.core.JDA":
params[i] = Julia.this.jda;
params[i] = Julia.this.getJda();
break;
//TODO completer les paramètres
@ -426,8 +473,8 @@ public class Julia {
}
}
public void trigger(Event event) {
Class<? extends Event> eventClass = event.getClass();
public void trigger(GenericEvent event) {
Class<? extends GenericEvent> eventClass = event.getClass();
Set<Method> toCall = loadedEvents.get(eventClass);
for(Method m : toCall) {
Object callable = callerObjects.get(m.getDeclaringClass());
@ -542,4 +589,16 @@ public class Julia {
return theJulia;
}
public JDA getJda() {
return jda;
}
public Map<Character,Laboratory> getLaboratoires() {
return laboratoires;
}
public EcouteurDEvents getLecouteur() {
return lecouteur;
}
}

View File

@ -161,7 +161,7 @@ public class JuliaAddon {
}
}
//Toutes les classes du Jar ont <EFBFBD><EFBFBD>t<EFBFBD><EFBFBD> lues : Si plusieurs addons, on rentre
//Toutes les classes du Jar ont été lues : Si plusieurs addons, on rentre
//dans le if pour lever l'exception, sinon, on initialise le JuliaClassLoader
if(juliaddons.size() > 1) {
Set<Set<JarEntry>> addonsEntries = new HashSet<>();//<pkg,Enum of classes associated with this plugin>
@ -227,7 +227,7 @@ public class JuliaAddon {
try {
return cl.loadClass(name, resolve);
}catch(ClassNotFoundException e) {
throw new ClassNotFoundException("J'ai d<EFBFBD><EFBFBD>l<EFBFBD><EFBFBD>gu<EFBFBD><EFBFBD> le chargement de la classe mais j'aurais pas du ... cette classe ne doit pas exister dans l'addon "+cl.getAddon().getName(),e);
throw new ClassNotFoundException("J'ai délégué le chargement de la classe mais j'aurais pas du ... cette classe ne doit pas exister dans l'addon "+cl.getAddon().getName(),e);
}
else
try {
@ -248,7 +248,7 @@ public class JuliaAddon {
Collections.addAll(toWatch, loadClass(clazz.getName()).getMethods());//loadClass(clazz.getName()) poir eviter les references cassees par les classloaders
} catch (SecurityException | ClassNotFoundException e) {
e.printStackTrace();
System.err.println("L'addon "+JuliaAddon.this.addonClass.getName()+" sp<EFBFBD><EFBFBD>cifie la classe "+clazz.getName());
System.err.println("L'addon "+JuliaAddon.this.addonClass.getName()+" spécifie la classe "+clazz.getName());
}
for(Method m : toWatch) {
Command c = m.getAnnotation(Command.class);

View File

@ -6,8 +6,8 @@ import java.io.PrintStream;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import net.dv8tion.jda.core.MessageBuilder;
import net.dv8tion.jda.core.entities.MessageChannel;
import net.dv8tion.jda.api.MessageBuilder;
import net.dv8tion.jda.api.entities.MessageChannel;
public class JuliaErrPrintStream extends OutputStream {

View File

@ -2,7 +2,10 @@ package com.bernard.juliabot.api;
import java.time.OffsetDateTime;
import net.dv8tion.jda.core.entities.*;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.User;
public interface DiscordCCommande extends StringCCommande {
@ -12,6 +15,8 @@ public interface DiscordCCommande extends StringCCommande {
public User getUser();
public JDA getJDA();
public String getContentStripped();
public OffsetDateTime getPostDate();

View File

@ -11,9 +11,20 @@ import java.lang.annotation.Target;
@Retention(RUNTIME)
@Target(TYPE)
public @interface JuLIAddon {
/**
* Le nom de l'addon
*/
String name();
/**
* La version de l'addon
*/
String version() default "";
/**
* Différentes classes, en plus de celle , dans laquelle chercher des méthodes définissant des événements ou des commandes
*/
Class<?>[] searchPath() default {};
/**
* La liste des personnes ayant dévellopé cet addon
*/
public String[] devs();
public boolean important() default false;
}

View File

@ -4,12 +4,12 @@ import java.sql.Connection;
import com.bernard.juliabot.Julia;
import net.dv8tion.jda.core.JDA;
import net.dv8tion.jda.api.JDA;
public class Trukilie {
public static JDA jda() {
return Julia.theJulia().jda;
return Julia.theJulia().getJda();
}
public static Connection juliaDB() {

View File

@ -22,12 +22,14 @@ import com.bernard.juliabot.api.JuLIAddon;
import com.bernard.juliabot.api.StringCCommande;
import com.thedeanda.lorem.LoremIpsum;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.core.entities.MessageChannel;
import net.dv8tion.jda.core.entities.User;
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.MessageBuilder;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
@JuLIAddon(name="internaddon", devs = "Bernard", version="beta", important=true)
@JuLIAddon(name="internaddon", devs = "Bernard", version="beta")
public class Internaddon {
public static final String COMMANDEUR = "!!";
@ -37,7 +39,7 @@ public class Internaddon {
if(e.getMessage().getContentRaw().startsWith(COMMANDEUR)) {
String name = e.getMessage().getContentRaw().split(" ")[0].substring(COMMANDEUR.length());
Laboratory labo = Julia.theJulia().laboratoires.get(Julia.theJulia().lecouteur.getLabo(e));//Récupére le labo de l'évent
Laboratory labo = Julia.theJulia().getLaboratoires().get(Julia.theJulia().getLecouteur().getLabo(e));//Récupére le labo de l'évent
InternalddonCCommande ccommande = new InternalddonCCommande(e,labo);
try {
CommandCalled called = labo.executeCommand(name, ccommande);
@ -133,6 +135,17 @@ public class Internaddon {
}
@Command(name="guilds", description = "Liste les guildes disponibles",admin=true)
public void guilds(DiscordCCommande commande) {
MessageBuilder mb = new MessageBuilder();
mb.append("Liste des serveurs :\n");
Julia.theJulia().getJda().getGuilds()
.stream()
.map(g -> g.getName() + "(" + g.getMemberCount() + " membres, id:" + g.getId() + ")\n")
.forEach(mb::append);;
commande.getChannel().sendMessage(mb.build()).complete();
}
@Command(name="update",description = "Met a jour le dossier des addons",admin=true)
public void update(StringCCommande commande) {
@ -220,6 +233,11 @@ public class Internaddon {
return m.getAuthor();
}
@Override
public JDA getJDA() {
return m.getJDA();
}
@Override
public String getContentStripped() {
return m.getContentStripped();
@ -227,7 +245,7 @@ public class Internaddon {
@Override
public OffsetDateTime getPostDate() {
return m.getCreationTime();
return m.getTimeCreated();
}
@Override
@ -235,6 +253,7 @@ public class Internaddon {
return labo;
}
}
public static CommandArguments parseCommandArguments(String raw) {

View File

@ -1,9 +1,10 @@
package com.bernard.juliabot.internaddon;
import java.io.*;
import java.io.IOException;
import java.io.OutputStream;
import java.util.function.Consumer;
import net.dv8tion.jda.core.entities.Message;
import net.dv8tion.jda.api.entities.Message;
public class UnstableMessage extends OutputStream{