Skip to content

Commit

Permalink
Improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
DxsSucuk committed Dec 13, 2022
1 parent 45e65ab commit c95e8fb
Show file tree
Hide file tree
Showing 25 changed files with 243 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[contributing]: https://github.com/Ree6-Applications/Ree6-SQL/blob/master/.github/CONTRIBUTING.md
[contributing]: https://github.com/Ree6-Applications/Ree6-Base/blob/master/.github/CONTRIBUTING.md

## Pull Request Etiquette

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sonar-analytics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=Ree6-Applications_Ree6-SQL
run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=Ree6-Applications_Ree6-Base
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ target/
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
.idea/
.idea
*.iws
*.iml
*.ipr
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

[![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/T6T4AC652)

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=Ree6-Applications_Ree6-SQL&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=Ree6-Applications_Ree6-SQL) [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=Ree6-Applications_Ree6-SQL&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=Ree6-Applications_Ree6-SQL) [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=Ree6-Applications_Ree6-SQL&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=Ree6-Applications_Ree6-SQL) [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=Ree6-Applications_Ree6-SQL&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=Ree6-Applications_Ree6-SQL)
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=Ree6-Applications_Ree6-Base&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=Ree6-Applications_Ree6-Base) [![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=Ree6-Applications_Ree6-Base&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=Ree6-Applications_Ree6-Base) [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=Ree6-Applications_Ree6-Base&metric=sqale_rating)](https://sonarcloud.io/summary/new_code?id=Ree6-Applications_Ree6-Base) [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=Ree6-Applications_Ree6-Base&metric=security_rating)](https://sonarcloud.io/summary/new_code?id=Ree6-Applications_Ree6-Base)
</div>

# What is Ree6-SQL?
Ree6-SQL is a SQL-Module for the Ree6 Discord Bot / Webinterface. It is used to store and access data in a Database.
# What is Ree6-Base?
Ree6-Base is the base for the Discord bot Ree6. It contains the core functions.

# Discord
Ree6 has its own support Discord that you can join to help us reach the Partner status on Discord and you to get help!
Expand Down
1 change: 0 additions & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
| Version | Supported |
|---------| ------------------ |
| 1.x.x | :white_check_mark: |
| < 1.1.0 | :x: |

## Reporting a Vulnerability

Expand Down
78 changes: 76 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,86 @@

<groupId>de.presti.ree6</groupId>
<artifactId>Ree6-Base</artifactId>
<version>1.0-SNAPSHOT</version>
<description>This is the JDA-Base for Ree6!</description>
<version>1.0.0</version>

<properties>
<sonar.organization>ree6-applications</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>dv8tion</id>
<name>m2-dv8tion</name>
<url>https://m2.dv8tion.net/releases</url>
</repository>
</repositories>

<dependencies>
<!-- Discord -->
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>5.0.0-beta.2</version>
</dependency>

<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.4.5</version>
</dependency>

<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>

<!-- Utility -->
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry</artifactId>
<version>6.9.2</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.10.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10</version>
</dependency>
<dependency>
<groupId>me.carleslc.Simple-YAML</groupId>
<artifactId>Simple-Yaml</artifactId>
<version>1.8.3</version>
</dependency>

<!-- Common -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>

</project>
18 changes: 12 additions & 6 deletions src/main/java/de/presti/ree6/addons/AddonLoader.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.presti.ree6.addons;

import de.presti.ree6.main.Main;
import de.presti.ree6.utils.data.ArrayUtil;
import de.presti.ree6.util.others.RandomUtils;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.simpleyaml.configuration.file.FileConfiguration;
import org.simpleyaml.configuration.file.YamlConfiguration;
Expand All @@ -21,12 +21,18 @@
@SuppressWarnings("ResultOfMethodCallIgnored")
public class AddonLoader {

/**
* The {@link AddonManager} used to store information about the Addons.
*/
@Getter
private static AddonManager addonManager;

/**
* Constructor should not be called, since it is a utility class that doesn't need an instance.
* @throws IllegalStateException it is a utility class.
*/
private AddonLoader() {
throw new IllegalStateException("Utility class");
private AddonLoader(AddonManager addonManager) {
this.addonManager = addonManager;
}

/**
Expand Down Expand Up @@ -67,7 +73,7 @@ public static void loadAllAddons() {
log.error("Couldn't pre-load the addon {}", file.getName());
}

Main.getInstance().getAddonManager().loadAddon(addon);
getAddonManager().loadAddon(addon);
} catch (Exception ex) {
// If the Methode loadAddon fails notify.
log.error("[AddonManager] Couldn't load the Addon {}\nException: {}", file.getName(), ex.getMessage());
Expand Down Expand Up @@ -106,7 +112,7 @@ public static Addon loadAddon(String fileName) throws IOException {
// If it is the addon.yml then get the Data from it.
if (!entry.isDirectory() && entryName.equalsIgnoreCase("addon.yml")) {
// Create a temporal File to extract the Data from. I'm pretty sure there is a better way but as I said earlier didn't have the time for it.
file = new File("addons/tmp/temp_" + ArrayUtil.getRandomString(9) + ".yml");
file = new File("addons/tmp/temp_" + RandomUtils.randomString(9) + ".yml");

// Create a FileOutputStream of the temporal File and write every bite from the File inside the JAR.
try (FileOutputStream os = new FileOutputStream(file)) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/presti/ree6/addons/AddonManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public Class<?> getClass(URLClassLoader classLoader, String classPath) throws Cl
for (Class<?> anIf : ifs) {

// If it has the AddonInterface mark it as valid.
if (anIf.getName().equalsIgnoreCase("de.presti.ree6.addons.AddonInterface")) {
if (anIf.getName().equalsIgnoreCase(AddonInterface.class.getPackageName() + "." + AddonInterface.class.getSimpleName())) {
return urlCl;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/de/presti/ree6/bot/BotWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import de.presti.ree6.bot.version.BotState;
import de.presti.ree6.bot.version.BotVersion;
import de.presti.ree6.main.Main;
import de.presti.ree6.util.data.Config;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
Expand Down Expand Up @@ -67,7 +67,7 @@ private BotWorker() {
*/
public static void createBot(BotVersion version1) {
version = version1;
token = Main.getInstance().getConfig().getConfiguration().getString(getVersion().getTokenPath());
token = Config.getConfiguration().getString(getVersion().getTokenPath());
state = BotState.INIT;

shardManager = DefaultShardManagerBuilder
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/de/presti/ree6/commands/CommandEvent.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.presti.ree6.commands;

import de.presti.ree6.language.LanguageService;
import de.presti.ree6.main.Main;
import de.presti.ree6.util.data.resolver.ResolverService;
import lombok.Getter;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
Expand Down Expand Up @@ -138,7 +138,7 @@ public void reply(MessageEmbed message, int deleteSecond) {
* @param message the Message to reply with.
*/
public void reply(MessageCreateData message) {
Main.getInstance().getCommandManager().sendMessage(message, getChannel(), getInteractionHook());
ResolverService.getCommandManager().sendMessage(message, getChannel(), getInteractionHook());
}

/**
Expand All @@ -148,7 +148,7 @@ public void reply(MessageCreateData message) {
* @param deleteSecond the Seconds to delete the Message after.
*/
public void reply(MessageCreateData message, int deleteSecond) {
Main.getInstance().getCommandManager().sendMessage(message, deleteSecond, getChannel(), getInteractionHook());
ResolverService.getCommandManager().sendMessage(message, deleteSecond, getChannel(), getInteractionHook());
}

/**
Expand Down
36 changes: 21 additions & 15 deletions src/main/java/de/presti/ree6/commands/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import de.presti.ree6.commands.interfaces.Command;
import de.presti.ree6.commands.interfaces.ICommand;
import de.presti.ree6.language.LanguageService;
import de.presti.ree6.sql.SQLSession;
import de.presti.ree6.utils.data.ArrayUtil;
import de.presti.ree6.utils.others.ThreadUtil;
import de.presti.ree6.util.data.resolver.ResolverService;
import de.presti.ree6.util.others.ThreadUtil;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.JDA;
Expand All @@ -28,11 +27,13 @@
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
import net.dv8tion.jda.internal.interactions.CommandDataImpl;
import org.reflections.Reflections;
import org.reflections.util.ClasspathHelper;

import java.lang.reflect.InvocationTargetException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

Expand All @@ -47,6 +48,11 @@ public class CommandManager {
*/
static final ArrayList<ICommand> commands = new ArrayList<>();

/**
* HashMap used to store a users Ids, to keep them from spamming commands.
*/
static final List<String> commandCooldown = new ArrayList<>();

/**
* Constructor for the Command-Manager used to register every Command.
*
Expand All @@ -60,7 +66,7 @@ public class CommandManager {
public CommandManager() throws CommandInitializerException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
log.info("Initializing Commands!");

Reflections reflections = new Reflections("de.presti.ree6.commands");
Reflections reflections = new Reflections(ClasspathHelper.staticClassLoader());
Set<Class<? extends ICommand>> classes = reflections.getSubTypesOf(ICommand.class);

for (Class<? extends ICommand> aClass : classes) {
Expand Down Expand Up @@ -213,7 +219,7 @@ public boolean perform(Member member, Guild guild, String messageContent, Messag
if (slashCommandInteractionEvent != null) {
sendMessage(LanguageService.getByGuild(guild, "command.perform.cooldown"), 5, textChannel, slashCommandInteractionEvent.getHook().setEphemeral(true));
deleteMessage(message, slashCommandInteractionEvent.getHook().setEphemeral(true));
} else if (messageContent.toLowerCase().startsWith(SQLSession.getSqlConnector().getSqlWorker().getSetting(guild.getId(), "chatprefix").getStringValue().toLowerCase())) {
} else if (messageContent.toLowerCase().startsWith(ResolverService.getPrefixResolver().resolvePrefix(guild.getIdLong()).toLowerCase())) {
sendMessage(LanguageService.getByGuild(guild, "command.perform.cooldown"), 5, textChannel, null);
deleteMessage(message, null);
}
Expand All @@ -235,12 +241,12 @@ public boolean perform(Member member, Guild guild, String messageContent, Messag

// Check if this is a Developer build, if not then cooldown the User.
if (!BotWorker.getVersion().isDebug()) {
ThreadUtil.createThread(x -> ArrayUtil.commandCooldown.remove(member.getUser().getId()), null, Duration.ofSeconds(5), false, false);
ThreadUtil.createThread(x -> commandCooldown.remove(member.getUser().getId()), null, Duration.ofSeconds(5), false, false);
}

// Add them to the Cooldown.
if (!ArrayUtil.commandCooldown.contains(member.getUser().getId()) && !BotWorker.getVersion().isDebug()) {
ArrayUtil.commandCooldown.add(member.getUser().getId());
if (!commandCooldown.contains(member.getUser().getId()) && !BotWorker.getVersion().isDebug()) {
commandCooldown.add(member.getUser().getId());
}

// Return that a command has been performed.
Expand All @@ -265,11 +271,11 @@ private boolean performMessageCommand(Member member, Guild guild, String message
}

// Check if the message starts with the prefix.
if (!messageContent.toLowerCase().startsWith(SQLSession.getSqlConnector().getSqlWorker().getSetting(guild.getId(), "chatprefix").getStringValue().toLowerCase()))
if (!messageContent.toLowerCase().startsWith(ResolverService.getPrefixResolver().resolvePrefix(guild.getIdLong()).toLowerCase()))
return false;

// Parse the Message and remove the prefix from it.
messageContent = messageContent.substring(SQLSession.getSqlConnector().getSqlWorker().getSetting(guild.getId(), "chatprefix").getStringValue().length());
messageContent = messageContent.substring(ResolverService.getPrefixResolver().resolvePrefix(guild.getIdLong()).length());

// Split all Arguments.
String[] arguments = messageContent.split(" ");
Expand All @@ -289,11 +295,11 @@ private boolean performMessageCommand(Member member, Guild guild, String message
}

// Check if the Command is blacklisted.
if (!SQLSession.getSqlConnector().getSqlWorker().getSetting(guild.getId(), "command_" + command.getClass().getAnnotation(Command.class).name().toLowerCase()).getBooleanValue() &&
/*if (!SQLSession.getSqlConnector().getSqlWorker().getSetting(guild.getId(), "command_" + command.getClass().getAnnotation(Command.class).name().toLowerCase()).getBooleanValue() &&
command.getClass().getAnnotation(Command.class).category() != Category.HIDDEN) {
sendMessage(LanguageService.getByGuild(guild, "command.perform.blocked"), 5, textChannel, null);
return false;
}
}*/

// Parse the arguments.
String[] argumentsParsed = Arrays.copyOfRange(arguments, 1, arguments.length);
Expand Down Expand Up @@ -322,10 +328,10 @@ private boolean performSlashCommand(MessageChannelUnion textChannel, SlashComman
}

// Check if the command is blocked or not.
if (!SQLSession.getSqlConnector().getSqlWorker().getSetting(slashCommandInteractionEvent.getGuild().getId(), "command_" + command.getClass().getAnnotation(Command.class).name().toLowerCase()).getBooleanValue() && command.getClass().getAnnotation(Command.class).category() != Category.HIDDEN) {
/*if (!SQLSession.getSqlConnector().getSqlWorker().getSetting(slashCommandInteractionEvent.getGuild().getId(), "command_" + command.getClass().getAnnotation(Command.class).name().toLowerCase()).getBooleanValue() && command.getClass().getAnnotation(Command.class).category() != Category.HIDDEN) {
sendMessage(LanguageService.getByGuild(slashCommandInteractionEvent.getGuild(), "command.perform.blocked"), 5, null, slashCommandInteractionEvent.getHook().setEphemeral(true));
return false;
}
}*/

// Perform the Command.
command.onASyncPerform(new CommandEvent(command.getClass().getAnnotation(Command.class).name(), slashCommandInteractionEvent.getMember(), slashCommandInteractionEvent.getGuild(), null, textChannel, null, slashCommandInteractionEvent));
Expand All @@ -340,7 +346,7 @@ private boolean performSlashCommand(MessageChannelUnion textChannel, SlashComman
* @return true, if yes | false, if not.
*/
public boolean isTimeout(User user) {
return ArrayUtil.commandCooldown.contains(user.getId()) && !BotWorker.getVersion().isDebug();
return commandCooldown.contains(user.getId()) && !BotWorker.getVersion().isDebug();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.presti.ree6.commands.interfaces;

import de.presti.ree6.commands.CommandEvent;
import de.presti.ree6.sql.SQLSession;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -31,8 +30,6 @@ default void onASyncPerform(CommandEvent commandEvent) {
}
return null;
});
// Update Stats.
SQLSession.getSqlConnector().getSqlWorker().addStats(commandEvent.getGuild().getId(), commandEvent.getCommand());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/presti/ree6/game/core/GameManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import de.presti.ree6.game.core.base.GameInfo;
import de.presti.ree6.game.core.base.IGame;
import de.presti.ree6.utils.others.RandomUtils;
import de.presti.ree6.util.others.RandomUtils;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion;
Expand Down
Loading

0 comments on commit c95e8fb

Please sign in to comment.