Skip to content

Commit

Permalink
Merge #4b48f3 to master/origin
Browse files Browse the repository at this point in the history
  • Loading branch information
Flxma committed May 31, 2020
2 parents 8eedf7d + 4b48f36 commit c52a49a
Show file tree
Hide file tree
Showing 16 changed files with 408 additions and 575 deletions.
2 changes: 2 additions & 0 deletions .idea/JDA-CMDH.iml

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

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

import net.dv8tion.jda.api.events.message.MessageReceivedEvent;

public interface BeforeCommandExecution{
public interface BeforeCommandExecution {
boolean onBeforeExecution(MessageReceivedEvent event, CommandPreferences preferences);
}
72 changes: 36 additions & 36 deletions src/main/java/de/flexusma/jdacmdh/CommandInitBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,75 +14,75 @@
import de.flexusma.jdacmdh.exception.DatabaseInitializationFailedException;
import net.dv8tion.jda.api.entities.Activity;

import java.nio.file.Path;

public class CommandInitBuilder {

LogType logLevel;
boolean writeToFile = true;
String path = null;
Database db = null;
boolean isDatabase = false;
CommandPreferences commandPreferences;
Activity activity=null;
BeforeCommandExecution listener;
IntiCommands cmds = null;
LogType logLevel;
boolean writeToFile = true;
String path = null;
Database db;
boolean isDatabase;
CommandPreferences commandPreferences;
Activity activity;
BeforeCommandExecution listener;
IntiCommands cmds;

//default overrides
Command MsgPrivateOnGuildOnly=null;
Command helpCommand=null;
//default overrides
Command MsgPrivateOnGuildOnly = null;
Command helpCommand = null;

public CommandInitBuilder log(LogType logLevel, boolean writeToFile, String folder){
this.logLevel=logLevel;
this.writeToFile=writeToFile;
this.path= folder;
public CommandInitBuilder log(LogType logLevel, boolean writeToFile, String folder) {
this.logLevel = logLevel;
this.writeToFile = writeToFile;
this.path = folder;

Logger.setup(logLevel, writeToFile, path);
return this;
}

public CommandInitBuilder activity(Activity activity){
this.activity=activity ;
public CommandInitBuilder activity(Activity activity) {
this.activity = activity;
return this;
}

public CommandInitBuilder addBeforeCommandListener(BeforeCommandExecution listener){
this.listener=listener ;
public CommandInitBuilder addBeforeCommandListener(BeforeCommandExecution listener) {
this.listener = listener;
return this;
}

public CommandInitBuilder MsgPrivateOnGuildOnly(Command override){
MsgPrivateOnGuildOnly=override;
public CommandInitBuilder MsgPrivateOnGuildOnly(Command override) {
MsgPrivateOnGuildOnly = override;
return this;
}

public CommandInitBuilder HelpCommand(Command override){
helpCommand=override;
public CommandInitBuilder HelpCommand(Command override) {
helpCommand = override;
return this;
}

public CommandInitBuilder preferences(CommandPreferences preferences){
this.commandPreferences=preferences;
public CommandInitBuilder preferences(CommandPreferences preferences) {
this.commandPreferences = preferences;
return this;
}

public CommandInitBuilder database(Database database) {
try {
if (isDatabase = database.initDB(commandPreferences))
if (isDatabase = database.initDB(commandPreferences)) {
this.db = database;
else
} else {
throw new DatabaseInitializationFailedException("Initialization Failed");
}catch (DatabaseInitializationFailedException r){
Logger.log(LogType.ERROR,r.getMessage());
}
} catch (DatabaseInitializationFailedException r) {
Logger.log(LogType.ERROR, r.getMessage());
}
return this;
}
public CommandInitBuilder commands(Command... commands){
this.cmds=new IntiCommands(commands);

public CommandInitBuilder commands(Command... commands) {
this.cmds = new IntiCommands(commands);
return this;
}

public CommandListener build(){
public CommandListener build() {
return new CommandListener(this);
}

}
112 changes: 33 additions & 79 deletions src/main/java/de/flexusma/jdacmdh/CommandListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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.EventListener;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import javax.annotation.Nonnull;
Expand All @@ -34,56 +33,40 @@ public class CommandListener extends ListenerAdapter {
CommandPreferences preferences;
private boolean isDatabase;
private CommandInitBuilder builder;
private Activity customActivity = null;
private Activity customActivity;
BeforeCommandExecution listener;

private static IntiCommands cmd = new IntiCommands(
new Help()
/* new About(),
new Delete(),
new Prefix(),
new Play(),
new SkipTrack(),
new StopPlayback(),
new Resume(),
new Pause(),
new AutoDelete(),
new Watch2Gether(),
new Queue(),
new Volume(),
new Lewd(),
new de.flexusma.wavybot.cmd.Activity()*/
);

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

this.isDatabase = cmbdBuilder.isDatabase;
this.preferences = cmbdBuilder.commandPreferences;
builder=cmbdBuilder;
builder = cmbdBuilder;

if(cmbdBuilder.helpCommand!=null)
cmd.cmds.replace("help",cmbdBuilder.helpCommand);

customActivity=cmbdBuilder.activity;
if (cmbdBuilder.helpCommand != null) {
cmd.cmds.replace("help", cmbdBuilder.helpCommand);
}

listener=cmbdBuilder.listener;
customActivity = cmbdBuilder.activity;

listener = cmbdBuilder.listener;
}

//guild messages
@Override
public void onMessageReceived(MessageReceivedEvent event) {
if (event.isFromGuild() && isDatabase)
if (event.isFromGuild() && isDatabase) {
preferences = Database.initPref(event.getJDA(), event.getGuild().getId());
}

Logger.log(LogType.DEBUG, "onMessagerecieved: Sender: "+event.getMessage().getAuthor().getName() +"["+event.getMessage().getAuthor().getId()+"]");
Logger.log(LogType.DEBUG, "onMessagerecieved: Sender: " + event.getMessage().getAuthor().getName() + "[" + event.getMessage().getAuthor().getId() + "]");
if (!event.getMessage().getAuthor().getId().equals(event.getJDA().getSelfUser().getId())) {
Logger.log(LogType.DEBUG, "Message recieved: " + event.getMessage().getContentRaw());
if (event.getMessage().getContentRaw().startsWith(preferences.getPrefix()) ||
event.getMessage().getContentRaw().replace("!", "").startsWith(event.getJDA().getSelfUser().getAsMention()) ){
if (event.getMessage().getContentRaw().startsWith(preferences.getPrefix()) || event.getMessage().getContentRaw().replace("!", "").startsWith(event.getJDA().getSelfUser().getAsMention())) {
Logger.log(LogType.DEBUG, "Command detected");
handlercommand(event, preferences);
}
Expand All @@ -106,8 +89,9 @@ private void handlercommand(MessageReceivedEvent event, CommandPreferences comma
//Logger.log(LogType.DEBUG,command+" "+registercmds);
if (registercmds.equals(command)) {
String args = String.join(" ", Arrays.copyOfRange(raw, 1, raw.length)).replaceFirst(registercmds, "");
if (event.getMessage().getContentRaw().startsWith(event.getJDA().getSelfUser().getAsMention()))
if (event.getMessage().getContentRaw().startsWith(event.getJDA().getSelfUser().getAsMention())) {
args = args.replaceFirst(" ", "");
}
Logger.log(LogType.DEBUG, "Command Arguments: " + args);
Command command1 = cmd.cmds.get(registercmds);
List<Permission> missingPerms = new ArrayList<>();
Expand All @@ -127,71 +111,41 @@ private void handlercommand(MessageReceivedEvent event, CommandPreferences comma
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());
} else if (!event.isFromGuild() && command1.guildOnly) {
if (builder.MsgPrivateOnGuildOnly != null) builder.MsgPrivateOnGuildOnly.execute(event1);
else new MsgPrivateOnGuildOnly().execute(event1);
} else

if (!(event.getAuthor().isBot() && command1.ignoreOtherBot)) {
executeCommand(command1,event1,event,commandPreferences);
if (builder.MsgPrivateOnGuildOnly != null) {
builder.MsgPrivateOnGuildOnly.execute(event1);
} else {
new MsgPrivateOnGuildOnly().execute(event1);
}
} else if (!(event.getAuthor().isBot() && command1.ignoreOtherBot)) {
executeCommand(command1, event1, event, commandPreferences);
}
}
}
}


void executeCommand(Command c, CommandEvent e, MessageReceivedEvent me, CommandPreferences pref){
if(listener!=null) {
void executeCommand(Command c, CommandEvent e, MessageReceivedEvent me, CommandPreferences pref) {
if (listener != null) {
if (listener.onBeforeExecution(me, pref)) {
Logger.log(LogType.DEBUG, "BeforeListener returned true, executing command!");
c.execute(e);
} else Logger.log(LogType.DEBUG, "BeforeListener returned false, throwing away command request!");
}else c.execute(e);
} else {
Logger.log(LogType.DEBUG, "BeforeListener returned false, throwing away command request!");
}
} else {
c.execute(e);
}
}


@Override
public void onReady(@Nonnull ReadyEvent event) {
if(customActivity==null)
event.getJDA().getPresence().setActivity(Activity.of(Activity.ActivityType.DEFAULT,""));
else event.getJDA().getPresence().setActivity(customActivity);
super.onReady(event);
}


/* public void onUserActivityStart(@Nonnull UserActivityStartEvent event) {
Game now = Database.getGame(event.getJDA(),event.getMember().getId(),event.getNewActivity().getName());
if(now!=null){
now.startPlay(event.getNewActivity().getName(),System.currentTimeMillis());
if (customActivity != null) {
event.getJDA().getPresence().setActivity(customActivity);
}else{
now=new Game();
now.startPlay(event.getNewActivity().getName(),System.currentTimeMillis());
event.getJDA().getPresence().setActivity(Activity.of(Activity.ActivityType.DEFAULT,""));
}
Database.saveGame(event.getJDA(),event.getMember().getId(),now);
super.onUserActivityStart(event);
}*/

/* @Override
public void onUserActivityEnd(@Nonnull UserActivityEndEvent event) {
Logger.log(LogType.DEBUG,"Activity: "+event.getOldActivity().getName()+" | "+ event.getOldActivity().getTimestamps().getElapsedTime(ChronoUnit.SECONDS));

if(now!=null){
now.addPlayTime(Duration.ofMillis(event.getOldActivity().getTimestamps().getElapsedTime(ChronoUnit.MILLIS)));
/*
now.startPlay(event.getOldActivity().getName(),event.getOldActivity().getTimestamps().getStart());
now.stopPlay(event.getOldActivity().getTimestamps().getEnd());
}else{
now=new Game();
now.addPlayTime(Duration.ofMillis(event.getOldActivity().getTimestamps().getElapsedTime(ChronoUnit.MILLIS)));
/*now.startPlay(event.getOldActivity().getName(),event.getOldActivity().getTimestamps().getStart());
now.stopPlay(event.getOldActivity().getTimestamps().getEnd());
}
Database.saveGame(event.getJDA(),event.getMember().getId(),now);
super.onUserActivityEnd(event);
}*/
super.onReady(event);
}
}
18 changes: 9 additions & 9 deletions src/main/java/de/flexusma/jdacmdh/CommandPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@
import javax.annotation.Nullable;
import java.io.Serializable;

public class CommandPreferences implements Serializable {
public class CommandPreferences implements Serializable {

public String Prefix = "!";
public int volume = 100;
private Emoticons emoticons = new Emoticons("✔","⚠","✘");
private Emoticons emoticons = new Emoticons("✔", "⚠", "✘");

public CommandPreferences(String prefix, int volume){
this.Prefix=prefix;
this.volume=volume;
public CommandPreferences(String prefix, int volume) {
this.Prefix = prefix;
this.volume = volume;
}


public CommandPreferences(@Nullable String prefix, @Nullable Emoticons emoticons) {
if(prefix!=null) this.setPrefix(prefix);
if(emoticons!=null) this.setEmoticons(emoticons);
if (prefix != null) this.setPrefix(prefix);
if (emoticons != null) this.setEmoticons(emoticons);
}


public CommandPreferences(@Nullable String prefix) {
if(prefix!=null) this.setPrefix(prefix);
if (prefix != null) this.setPrefix(prefix);
}


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

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/de/flexusma/jdacmdh/IntiCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@

class IntiCommands {
HashMap<String, Command> cmds = new HashMap<>();

IntiCommands(Command... t) {
for (Command cmd:t) {
cmds.put(cmd.name , cmd);
for (Command cmd : t) {
cmds.put(cmd.name, cmd);
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/de/flexusma/jdacmdh/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import net.dv8tion.jda.api.Permission;

public abstract class Command {
public String name;
public String help;
public Permission[] botPermissions;
public boolean guildOnly;
public boolean ignoreOtherBot= true;
public String usage;
public String name;
public String help;
public Permission[] botPermissions;
public boolean guildOnly;
public boolean ignoreOtherBot= true;
public String usage;

public abstract void execute(CommandEvent event);

Expand Down
Loading

0 comments on commit c52a49a

Please sign in to comment.