First Slash command + Move to java 17

This commit is contained in:
SebClem 2022-05-13 17:54:56 +02:00
parent e67e2ecda3
commit 9d50e4c95f
14 changed files with 221 additions and 66 deletions

View File

@ -1,19 +0,0 @@
#!/bin/bash
#This script download the last stable build on jenkins
echo "Branch: "$1
if [[ $1 = "master" ]]
then
base_url="https://jenkins.seb6596.ovh/job/Bot%20Discord%20Gradle/lastStableBuild"
else
base_url="https://jenkins.seb6596.ovh/job/Bot%20Discord%20Gradle%20Devel/lastStableBuild/"
fi
data=$(curl -s -g ${base_url}"/api/xml?xpath=/freeStyleBuild/artifact&wrapper=artifacts")
relativePath=$(grep -oPm1 "(?<=<relativePath>)[^<]+" <<< "$data")
jarFile=$(grep -oPm1 "(?<=<fileName>)[^<]+" <<< "$data")
wget ${base_url}"/artifact/"${relativePath} -O bot.jar -nv

View File

@ -1,5 +1,5 @@
plugins {
id 'org.springframework.boot' version '2.4.5'
id 'org.springframework.boot' version '2.6.7'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
id 'groovy'
@ -12,8 +12,8 @@ group = "net.broken"
archivesBaseName = "ClaptrapBot"
version = "$versionObj"
sourceCompatibility = 1.8
targetCompatibility = 1.8
sourceCompatibility = '17'
@ -35,47 +35,38 @@ repositories {
}
}
javadoc {
source = sourceSets.main.allJava
classpath = configurations.compile
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web"){
implementation("org.springframework.boot:spring-boot-starter-web"){
exclude group:"org.springframework.boot", module: "spring-boot-starter-logging"
exclude group: "org.springframework.boot", module :"spring-boot-starter-tomcat"
}
compile("org.springframework.boot:spring-boot-starter-undertow")
compile("org.springframework.boot:spring-boot-starter-log4j2")
implementation("org.springframework.boot:spring-boot-starter-log4j2")
implementation 'org.codehaus.groovy:groovy-all:3.0.8'
implementation 'com.sedmelluq:lavaplayer:1.3.77'
compile 'net.dv8tion:JDA:4.2.1_266'
compile group: 'org.json', name: 'json', version: '20210307'
compile 'org.springframework.security:spring-security-web:5.5.0'
implementation 'net.dv8tion:JDA:4.4.0_350'
implementation group: 'org.json', name: 'json', version: '20210307'
implementation 'org.springframework.security:spring-security-web:5.5.0'
// JPA Data (We are going to use Repositories, Entities, Hibernate, etc...)
compile("org.springframework.boot:spring-boot-starter-data-jpa") {
implementation("org.springframework.boot:spring-boot-starter-data-jpa") {
exclude group:"org.springframework.boot", module: "spring-boot-starter-logging"
}
implementation(platform("org.apache.logging.log4j:log4j-bom:2.17.1"))
// Use MySQL Connector-J
compile 'mysql:mysql-connector-java'
compile 'org.reflections:reflections:0.9.12'
compile 'org.apache.commons:commons-lang3:3.12.0'
compile 'com.google.api-client:google-api-client:1.31.5'
compile 'com.google.apis:google-api-services-youtube:v3-rev20210410-1.31.0'
implementation 'mysql:mysql-connector-java'
implementation 'org.reflections:reflections:0.9.12'
implementation 'org.apache.commons:commons-lang3:3.12.0'
implementation 'com.google.api-client:google-api-client:1.31.5'
implementation 'com.google.apis:google-api-services-youtube:v3-rev20210410-1.31.0'
compile group: 'org.jsoup', name: 'jsoup', version: '1.13.1'
implementation group: 'org.jsoup', name: 'jsoup', version: '1.13.1'
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('com.jayway.jsonpath:json-path')
compile("org.springframework.boot:spring-boot-starter-thymeleaf") {
implementation("org.springframework.boot:spring-boot-starter-thymeleaf") {
exclude group:"org.springframework.boot", module: "spring-boot-starter-logging"
}
}

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@ -18,6 +18,7 @@ import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceJoinEvent;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceMoveEvent;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
@ -29,6 +30,7 @@ import org.springframework.context.ApplicationContext;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -216,6 +218,14 @@ public class BotListener extends ListenerAdapter {
}
@Override
public void onSlashCommand(@NotNull SlashCommandEvent event) {
HashMap<String, SlashCommand> commands = MainBot.slashCommands;
super.onSlashCommand(event);
if(commands.containsKey(event.getName())){
commands.get(event.getName()).action(event);
}
}
@Override
public void onGuildJoin(GuildJoinEvent event) {

View File

@ -8,7 +8,6 @@ import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
import java.text.DateFormat;
import java.text.ParseException;

View File

@ -4,6 +4,7 @@ import net.Broken.DB.Entity.UserEntity;
import net.Broken.DB.Repository.UserRepository;
import net.Broken.RestApi.ApiCommandLoader;
import net.Broken.Tools.Command.CommandLoader;
import net.Broken.Tools.Command.SlashCommandLoader;
import net.Broken.Tools.DayListener.DayListener;
import net.Broken.Tools.DayListener.Listeners.DailyMadame;
import net.Broken.Tools.DayListener.Listeners.ResetSpam;
@ -60,12 +61,6 @@ public class Init {
*************************************/
jda.getPresence().setPresence(OnlineStatus.DO_NOT_DISTURB, Activity.playing("Loading..."));
jda.getTextChannels().forEach(textChannel -> {
if (textChannel.canTalk())
textChannel.sendTyping().queue();
});
//On recupere le l'id serveur
@ -91,6 +86,8 @@ public class Init {
logger.info("Check database...");
checkDatabase();
CommandLoader.load();
SlashCommandLoader.load();
SlashCommandLoader.registerSlashCommands(jda.updateCommands());
ApiCommandLoader.load();
DayListener dayListener = DayListener.getInstance();
dayListener.addListener(new ResetSpam());

View File

@ -30,6 +30,7 @@ import java.util.HashMap;
public class MainBot {
public static HashMap<String, Commande> commandes = new HashMap<>();
public static HashMap<String, SlashCommand> slashCommands = new HashMap<>();
public static HashMap<Member, ArrayList<Message>> historique =new HashMap<>();
public static HashMap<Member, Integer> message_compteur =new HashMap<>();
public static HashMap<String, Integer> mutualGuildCount =new HashMap<>();
@ -89,14 +90,11 @@ public class MainBot {
public static void handleCommand(CommandParser.CommandContainer cmd, UserEntity user)
{
if(!ready)
{
if(!ready){
return;
}
if (commandes.containsKey(cmd.commande))
{
if (commandes.containsKey(cmd.commande)){
Commande cmdObj = commandes.get(cmd.commande);
boolean isAdmin;
boolean isBotAdmin = user != null && user.isBotAdmin();

View File

@ -0,0 +1,36 @@
package net.Broken;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import java.util.List;
/**
* Interface that define command structure.
*/
public interface SlashCommand {
/**
* Main action of command
* @param args Command args.
* @param event Command MessageReceivedEvent
*/
void action(SlashCommandEvent event);
String getDescription();
List<OptionData> getOptions();
/**
* Determines if the command is usable only by bot level admin user
* @return boolean
*/
boolean isBotAdminCmd();
/**
* Determines if the command is only usable on NSFW channels
* @return boolean
*/
boolean isNSFW();
}

View File

@ -0,0 +1,81 @@
package net.Broken.SlashCommands;
import net.Broken.Commande;
import net.Broken.SlashCommand;
import net.Broken.Tools.EmbedMessageUtils;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.MessageBuilder;
import net.dv8tion.jda.api.entities.ChannelType;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
/**
* Command that return a random picture of cat.
*/
public class Cat implements SlashCommand {
private Logger logger = LogManager.getLogger();;
@Override
public void action(SlashCommandEvent event) {
try {
URL urlC = new URL("http://aws.random.cat/meo");
URLConnection yc = urlC.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
yc.getInputStream(), "UTF-8"));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)
a.append(inputLine);
in.close();
JSONObject json = new JSONObject(a.toString());
event.reply(json.getString("file")).queue();
} catch (IOException e) {
logger.catching(e);
event.reply(new MessageBuilder().setEmbeds(EmbedMessageUtils.getInternalError()).build()).queue();
}
}
@Override
public String getDescription() {
return "Return a nice Cat !";
}
@Override
public List<OptionData> getOptions() {
return new ArrayList<>();
}
/**
* Determines if the command is usable only by bot level admin user
*
* @return boolean
*/
@Override
public boolean isBotAdminCmd() {
return false;
}
@Override
public boolean isNSFW() {
return false;
}
}

View File

@ -0,0 +1,70 @@
package net.Broken.Tools.Command;
import net.Broken.Commande;
import net.Broken.MainBot;
import net.Broken.SlashCommand;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.requests.restaction.CommandListUpdateAction;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jboss.jandex.Main;
import org.reflections.Reflections;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import java.lang.reflect.InvocationTargetException;
import java.util.Set;
/**
* Find and load bot's command
*/
public class SlashCommandLoader {
private static Logger logger = LogManager.getLogger();
/**
* Search all implemented Command interface class and add it to MainBot.commands HashMap
*/
public static void load() {
logger.info("Loading Slash Command...");
Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage(
"net.Broken.SlashCommands",
ClasspathHelper.contextClassLoader(),
ClasspathHelper.staticClassLoader()))
);
Set<Class<? extends SlashCommand>> modules = reflections.getSubTypesOf(SlashCommand.class);
logger.info("Find " + modules.size() + " Command:");
for (Class<? extends SlashCommand> command : modules) {
String reference = command.getName();
String[] splited = reference.split("\\.");
String name = splited[splited.length - 1].toLowerCase();
if (!command.isAnnotationPresent(Ignore.class)) {
logger.info("..." + name);
if (command.isAnnotationPresent(NoDev.class) && MainBot.dev) {
logger.warn("Command disabled in dev mode");
}else{
try {
MainBot.slashCommands.put(name, command.getDeclaredConstructor().newInstance());
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
logger.error("Failed to load " + name + "!");
}
}
} else {
logger.trace("Ignored command: " + name);
}
}
}
public static void registerSlashCommands(CommandListUpdateAction commandListUpdateAction){
MainBot.slashCommands.forEach((k,v)->{
commandListUpdateAction.addCommands(new CommandData(k, v.getDescription()).addOptions(v.getOptions()));
});
commandListUpdateAction.queue();
}
}

View File

@ -1,6 +1,5 @@
package net.Broken.Tools.UserManager;
import jdk.nashorn.internal.parser.JSONParser;
import net.Broken.DB.Entity.UserEntity;
import net.Broken.DB.Repository.UserRepository;
import net.Broken.MainBot;

View File

@ -26,7 +26,6 @@ import org.springframework.web.bind.annotation.ResponseStatus;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
/**

View File

@ -3,10 +3,8 @@ package net.Broken.webView;
import net.Broken.DB.Entity.UserEntity;
import net.Broken.DB.Repository.UserRepository;
import net.Broken.MainBot;
import net.Broken.Tools.SettingsUtils;
import net.Broken.Tools.UserManager.Exceptions.UnknownTokenException;
import net.Broken.Tools.UserManager.UserUtils;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.User;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

View File

@ -41,8 +41,4 @@ public class MyErrorController implements ErrorController {
return "error";
}
@Override
public String getErrorPath() {
return "/error";
}
}