Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Flexusma committed Sep 9, 2021
2 parents 6ff8dea + 5ec215c commit 6a00cd0
Show file tree
Hide file tree
Showing 21 changed files with 134 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.flexusma</groupId>
<artifactId>JDA-CMDH</artifactId>
<version>1.1.17.1</version>
<version>1.1.18</version>
<build>
<plugins>
<plugin>
Expand Down Expand Up @@ -46,9 +46,9 @@
</build>
<repositories>
<repository>
<id>jcenter</id>
<name>jcenter-bintray</name>
<url>https://jcenter.bintray.com</url>
<id>dv8tion</id>
<name>m2-dv8tion</name>
<url>https://m2.dv8tion.net/releases</url>
</repository>
</repositories>
<distributionManagement>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.flexusma</groupId>
<artifactId>JDA-CMDH</artifactId>
<version>1.1.17.2</version>
<version>1.1.18</version>

<build>
<plugins>
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/de/flexusma/jdacmdh/CommandInitBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import de.flexusma.jdacmdh.debug.LogType;
import de.flexusma.jdacmdh.debug.Logger;
import de.flexusma.jdacmdh.exception.DatabaseInitializationFailedException;
import net.dv8tion.jda.api.entities.Activity;

public class CommandInitBuilder {

Expand All @@ -23,6 +24,7 @@ public class CommandInitBuilder {
CommandPreferences commandPreferences;
BeforeCommandExecution listener;
IntiCommands cmds;
Activity loadedActivity = null;

//default overrides
Command MsgPrivateOnGuildOnly = null;
Expand Down Expand Up @@ -70,6 +72,12 @@ public CommandInitBuilder database(Database database) {
return this;
}

public CommandInitBuilder readyStatus(Activity activity){
if(activity!=null)
this.loadedActivity = activity;
return this;
}

public CommandInitBuilder commands(Command... commands) {
this.cmds = new IntiCommands(commands);
return this;
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/de/flexusma/jdacmdh/CommandListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
import de.flexusma.jdacmdh.database.Database;
import de.flexusma.jdacmdh.debug.LogType;
import de.flexusma.jdacmdh.debug.Logger;
import de.flexusma.jdacmdh.utils.embeds.EmbeddedBuilder;
import de.flexusma.jdacmdh.utils.PreferenceManager;
import de.flexusma.jdacmdh.utils.embeds.EmbededBuilder;
import de.flexusma.jdacmdh.utils.embeds.MessageEmbedField;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.util.ArrayList;
Expand All @@ -32,13 +36,17 @@ public class CommandListener extends ListenerAdapter {
private CommandInitBuilder builder;
BeforeCommandExecution listener;

private Activity loadedActivity = null;

private static IntiCommands cmd = new IntiCommands(
new Help()
);

public CommandListener(CommandInitBuilder cmbdBuilder) {
cmd = cmbdBuilder.cmds;

loadedActivity = cmbdBuilder.loadedActivity;

this.isDatabase = cmbdBuilder.isDatabase;
this.preferences = cmbdBuilder.commandPreferences;
builder = cmbdBuilder;
Expand All @@ -50,11 +58,20 @@ public CommandListener(CommandInitBuilder cmbdBuilder) {
listener = cmbdBuilder.listener;
}

@Override
public void onReady(@NotNull ReadyEvent event) {
super.onReady(event);
if(loadedActivity!=null)
event.getJDA().getPresence().setActivity(loadedActivity);
Logger.log(LogType.INFO,"Bot ready!");
}

//guild messages
@Override
public void onMessageReceived(MessageReceivedEvent event) {
if (event.isFromGuild() && isDatabase) {
preferences = Database.initPref(event.getJDA(), event.getGuild().getId());
preferences = PreferenceManager.getPref(event.getGuild().getId());
Logger.log(LogType.DEBUG,"Preferences from Database: "+preferences);
}

Logger.log(LogType.DEBUG, "onMessagerecieved: Sender: " + event.getMessage().getAuthor().getName() + "[" + event.getMessage().getAuthor().getId() + "]");
Expand Down Expand Up @@ -103,7 +120,7 @@ private void handlercommand(MessageReceivedEvent event, CommandPreferences comma
fields.add(new MessageEmbedField("" + missingPerms.indexOf(p), p.getName(), missingPerms.indexOf(p) % 2 == 0));
}
Logger.log(LogType.WARN, "Missing Permissions on command:[" + command1.getName() + "] at guild:[" + event.getGuild().getName() + "|" + event.getGuild().getId() + "]");
event1.reply(EmbeddedBuilder.create("Error, not enough Permissions!", "Hey, it seems that I'm missing some Permissions... Please check that!", Color.red, fields).build());
event1.reply(EmbededBuilder.create("Error, not enough Permissions!", "Hey, it seems that I'm missing some Permissions... Please check that!", Color.red, fields).build());
} else if (!event.isFromGuild() && command1.guildOnly) {
if (builder.MsgPrivateOnGuildOnly != null) {
builder.MsgPrivateOnGuildOnly.execute(event1);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/flexusma/jdacmdh/CommandPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public CommandPreferences(@Nullable String prefix) {


public CommandPreferences returnCastedInstance() {
return null;
return (CommandPreferences) this;
}


Expand Down
26 changes: 13 additions & 13 deletions src/main/java/de/flexusma/jdacmdh/command/CommandEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
package de.flexusma.jdacmdh.command;

import de.flexusma.jdacmdh.CommandPreferences;
import de.flexusma.jdacmdh.utils.embeds.EmbeddedBuilder;
import de.flexusma.jdacmdh.utils.embeds.EmbededBuilder;
import de.flexusma.jdacmdh.utils.embeds.MessageEmbedField;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.*;
Expand Down Expand Up @@ -169,51 +169,51 @@ public CommandEvent(MessageReceivedEvent e, CommandPreferences pref, String Args
@Deprecated
public void replySuccessOld(String title, String description, List<MessageEmbed.Field> embds) {
if (mRevE.isFromGuild()) {
textChannel.sendMessage(EmbeddedBuilder.createOld(pref.getEmoticons().success + title, description, Color.GREEN, embds).build()).queue();
textChannel.sendMessage(EmbededBuilder.createOld(pref.getEmoticons().success + title, description, Color.GREEN, embds).build()).queue();
} else {
channel.sendMessage(EmbeddedBuilder.createOld(pref.getEmoticons().success + title, description, Color.GREEN, embds).build()).queue();
channel.sendMessage(EmbededBuilder.createOld(pref.getEmoticons().success + title, description, Color.GREEN, embds).build()).queue();
}
}

@Deprecated
public void replyWarnOld(String title, String description, List<MessageEmbed.Field> embds) {
if (mRevE.isFromGuild()) {
textChannel.sendMessage(EmbeddedBuilder.createOld(pref.getEmoticons().warn + title, description, Color.ORANGE, embds).build()).queue();
textChannel.sendMessage(EmbededBuilder.createOld(pref.getEmoticons().warn + title, description, Color.ORANGE, embds).build()).queue();
} else {
channel.sendMessage(EmbeddedBuilder.createOld(pref.getEmoticons().warn + title, description, Color.ORANGE, embds).build()).queue();
channel.sendMessage(EmbededBuilder.createOld(pref.getEmoticons().warn + title, description, Color.ORANGE, embds).build()).queue();
}
}

@Deprecated
public void replyErrorOld(String title, String description, List<MessageEmbed.Field> embds) {
if (mRevE.isFromGuild()) {
textChannel.sendMessage(EmbeddedBuilder.createOld(pref.getEmoticons().error + title, description, Color.RED, embds).build()).queue();
textChannel.sendMessage(EmbededBuilder.createOld(pref.getEmoticons().error + title, description, Color.RED, embds).build()).queue();
} else {
channel.sendMessage(EmbeddedBuilder.createOld(pref.getEmoticons().error + title, description, Color.RED, embds).build()).queue();
channel.sendMessage(EmbededBuilder.createOld(pref.getEmoticons().error + title, description, Color.RED, embds).build()).queue();
}
}

public void replySuccess(String title, String description, List<MessageEmbedField> embds) {
if (mRevE.isFromGuild()) {
textChannel.sendMessage(EmbeddedBuilder.create(pref.getEmoticons().success + title, description, Color.GREEN, embds).build()).queue();
textChannel.sendMessage(EmbededBuilder.create(pref.getEmoticons().success + title, description, Color.GREEN, embds).build()).queue();
} else {
channel.sendMessage(EmbeddedBuilder.create(pref.getEmoticons().success + title, description, Color.GREEN, embds).build()).queue();
channel.sendMessage(EmbededBuilder.create(pref.getEmoticons().success + title, description, Color.GREEN, embds).build()).queue();
}
}

public void replyWarn(String title, String description, List<MessageEmbedField> embds) {
if (mRevE.isFromGuild()) {
textChannel.sendMessage(EmbeddedBuilder.create(pref.getEmoticons().warn + title, description, Color.ORANGE, embds).build()).queue();
textChannel.sendMessage(EmbededBuilder.create(pref.getEmoticons().warn + title, description, Color.ORANGE, embds).build()).queue();
} else {
channel.sendMessage(EmbeddedBuilder.create(pref.getEmoticons().warn + title, description, Color.ORANGE, embds).build()).queue();
channel.sendMessage(EmbededBuilder.create(pref.getEmoticons().warn + title, description, Color.ORANGE, embds).build()).queue();
}
}

public void replyError(String title, String description, List<MessageEmbedField> embds) {
if (mRevE.isFromGuild()) {
textChannel.sendMessage(EmbeddedBuilder.create(pref.getEmoticons().error + title, description, Color.RED, embds).build()).queue();
textChannel.sendMessage(EmbededBuilder.create(pref.getEmoticons().error + title, description, Color.RED, embds).build()).queue();
} else {
channel.sendMessage(EmbeddedBuilder.create(pref.getEmoticons().error + title, description, Color.RED, embds).build()).queue();
channel.sendMessage(EmbededBuilder.create(pref.getEmoticons().error + title, description, Color.RED, embds).build()).queue();
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/de/flexusma/jdacmdh/command/defaults/Help.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import de.flexusma.jdacmdh.command.Command;
import de.flexusma.jdacmdh.command.CommandEvent;
import de.flexusma.jdacmdh.database.Database;
import de.flexusma.jdacmdh.utils.embeds.EmbeddedBuilder;
import de.flexusma.jdacmdh.utils.PreferenceManager;
import de.flexusma.jdacmdh.utils.embeds.EmbededBuilder;
import de.flexusma.jdacmdh.utils.embeds.MessageEmbedField;
import net.dv8tion.jda.api.Permission;

Expand All @@ -31,7 +32,7 @@ public Help() {
@Override
public void execute(CommandEvent event) {
List<MessageEmbedField> fields = new ArrayList<>();
CommandPreferences pref = Database.initPref(event.getJDA(), event.getGuild().getId());
CommandPreferences pref = PreferenceManager.getPref(event.getGuild().getId());
if (event.getMessageRecieved().isFromGuild()) {
fields.add(new MessageEmbedField("**Prefix:**", "The current prefix on this server is " + pref.getPrefix(), false));
} else {
Expand All @@ -42,6 +43,6 @@ public void execute(CommandEvent event) {
fields.add(new MessageEmbedField("**" + c.getName() + "**\n", "Usage: ```" + c.getUsage() + "``` \n" + c.getHelp(), false));
}

event.reply(EmbeddedBuilder.create("Command Help", "These are all the Commands you can use on this server:", Color.green, fields).build());
event.reply(EmbededBuilder.create("Command Help", "These are all the Commands you can use on this server:", Color.green, fields).build());
}
}
22 changes: 15 additions & 7 deletions src/main/java/de/flexusma/jdacmdh/database/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,19 @@ public static CommandPreferences prefFromDB(Connection connection, String id) th
// preferences.getClass().getMethod("set"+prefStructure.getClass().getFields()[i-1].getName(),prefStructure.getClass().getFields()[i-1].getType()).invoke(preferences,args[i-1]);
}
// Instantiate the object with the converted arguments.
Logger.log(LogType.INFO, "Downloaded + parsed Data from Database");
rs.close();
pstmt.close();
connection.close();
Logger.log(LogType.INFO, "Downloaded + parsed Data from Database ["+args[0]+"]");



try {
return ((CommandPreferences) ctor.newInstance(args)).returnCastedInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
Logger.log(LogType.DEBUG,"Error at instantiating class instance");
CommandPreferences pref = ((CommandPreferences) ctor.newInstance(args)).returnCastedInstance();

rs.close();
pstmt.close();
connection.close();
return pref;
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
Logger.log(LogType.DEBUG,"Error at instantiating class instance: "+e);
}
}
}
Expand Down Expand Up @@ -320,6 +323,11 @@ private static void setPreparedData(Field field, CommandPreferences cpreferences

Logger.log(LogType.DEBUG, cpreferences.getClass().getName() + " class is to be setpreparedData");

if(preferences==null)
Logger.log(LogType.WARN,"Preferences Instance null!");
if(field==null)
Logger.log(LogType.WARN,"Field is null!");

Type target = field.getType();
if (target == Object.class || target == String.class) {
pr.setObject(index, field.get(preferences));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static JDBCType convertType(Type t) {
}

private static boolean check(Type c) {
return type.getTypeName().equals(c.getTypeName());
return type.getTypeName().equalsIgnoreCase(c.getTypeName());
}

}
32 changes: 32 additions & 0 deletions src/main/java/de/flexusma/jdacmdh/utils/PreferenceManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package de.flexusma.jdacmdh.utils;

import de.flexusma.jdacmdh.CommandPreferences;
import de.flexusma.jdacmdh.database.Database;

import java.util.HashMap;

public class PreferenceManager {

private static HashMap<String,Object> preferences = new HashMap<>();

public static CommandPreferences getPref(String guildId){
if(preferences.containsKey(guildId)){
return (CommandPreferences) preferences.get(guildId);
}

CommandPreferences pref = Database.initPref(null,guildId);
if(pref!=null){
preferences.put(guildId,pref);
return pref;
}
return null;
}

public static void savePref(String guildId, CommandPreferences pref){
if(preferences.containsKey(guildId)){
preferences.replace(guildId,pref);
}
Database.savePref(null,guildId,pref);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.awt.Color;
import java.util.List;

public class EmbeddedBuilder {
public class EmbededBuilder {

public static EmbedBuilder create(String title, String desc, Color c, MessageEmbedField... f) {

Expand Down
Binary file added target/JDA-CMDH-1.1.17.2.jar
Binary file not shown.
Binary file added target/JDA-CMDH-1.1.18-shaded.jar
Binary file not shown.
Binary file added target/JDA-CMDH-1.1.18.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion target/maven-archiver/pom.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Created by Apache Maven 3.6.3
groupId=de.flexusma
artifactId=JDA-CMDH
version=1.1.17.1
version=1.1.18
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
de\flexusma\jdacmdh\utils\embeds\EmbeddedBuilder.class
de\flexusma\jdacmdh\command\defaults\Help.class
de\flexusma\jdacmdh\utils\JAVAToSQLDatatype.class
de\flexusma\jdacmdh\exception\DatabaseInitializationFailedException.class
de\flexusma\jdacmdh\utils\embeds\EmbededBuilder.class
de\flexusma\jdacmdh\utils\PreferenceManager.class
de\flexusma\jdacmdh\command\Command.class
de\flexusma\jdacmdh\utils\embeds\MessageEmbedField.class
de\flexusma\jdacmdh\CommandInitBuilder.class
Expand Down
Loading

0 comments on commit 6a00cd0

Please sign in to comment.