Skip to content

Commit

Permalink
独自実装エラーメッセージ対応
Browse files Browse the repository at this point in the history
  • Loading branch information
waxsd100 committed Jun 16, 2020
1 parent 9a5fb14 commit eff3689
Show file tree
Hide file tree
Showing 14 changed files with 215 additions and 106 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies {
compile 'org.apache.logging.log4j:log4j-api'
compile 'org.apache.logging.log4j:log4j-core'
compile 'org.slf4j:slf4j-log4j12:1.7.30'
compile 'com.zaxxer:HikariCP:3.4.5'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.10'
compile group: 'org.apache.commons', name: 'commons-configuration2', version: '2.7'
compile group: 'commons-beanutils', name: 'commons-beanutils', version: '1.9.4'
Expand Down
4 changes: 4 additions & 0 deletions config/database.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
user="root"
password="none"
host="localhost"
port="3306"
35 changes: 8 additions & 27 deletions src/main/java/jp/skyblock/Command/CommandExecIf.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@
import static jp.skyblock.Core.Const.Constant.COMMAND_HELLO;
import static jp.skyblock.Core.Const.Constant.COMMAND_ROLE;
import static jp.skyblock.Core.Const.Constant.PREFIX;
import static jp.skyblock.Utility.ExceptionIf.commandException;

public interface CommandExecIf {
public interface CommandExecIf {
/**
* Execメソッドを取得
*
* @param msg
* @param cmdSplit
* @return
*/
static CommandExecIf getCommandExec(String msg) {
static CommandExecIf getInstance(String[] cmdSplit) {
CommandExecIf command;
String[] cmdSplit = msg.split(" ");
CommandEvent.cmdParam = cmdSplit;

switch (cmdSplit[0]) {
case PREFIX + COMMAND_HELLO:
Expand All @@ -43,30 +42,12 @@ static CommandExecIf getCommandExec(String msg) {
return command;
}

default void execute() throws ExceptionIf {
void execute() throws Exception;

}
Object executeResponse(Object obj);

default Object executeResponse(Object obj) {
return obj;
default Exception sendError(Exception e, String ErrorMessage, MessageReceivedEvent event) throws Exception {
return commandException(e, ErrorMessage, event);
}

/**
* @return event
*/
class CommandEvent {
protected static String[] cmdParam;
protected static MessageReceivedEvent event;

public CommandEvent() {
}

protected static MessageReceivedEvent getEvent() {
return event;
}

public static void setEvent(MessageReceivedEvent event) {
CommandEvent.event = event;
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/jp/skyblock/Command/DefaultCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@

import jp.skyblock.Utility.ExceptionIf;

public class DefaultCommand implements CommandExecIf {
public class DefaultCommand extends Throwable implements CommandExecIf {
@Override
public void execute() throws ExceptionIf {
public void execute() {

}

@Override
public Object executeResponse(Object obj) {
return null;
}

}
24 changes: 17 additions & 7 deletions src/main/java/jp/skyblock/Command/PingCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@

package jp.skyblock.Command;

import jp.skyblock.Core.Config;
import jp.skyblock.Core.Const.Constant;
import jp.skyblock.Core.Observer.Message.Received;
import jp.skyblock.Utility.EmojiUtil;
import jp.skyblock.Utility.ExceptionIf;
import jp.skyblock.Utility.RoleUtil;
import jp.skyblock.Utility.WelcomeMessage;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.User;
Expand All @@ -26,18 +30,18 @@

public class PingCommand implements CommandExecIf {
final EmojiUtil emj = Constant.emj;
final ExceptionIf exceptIf = Constant.exceptIf;

final RoleUtil roleUtil = new RoleUtil();
final Received received = new Received();

@Override
public void execute() {
MessageReceivedEvent event = CommandExecIf.CommandEvent.getEvent();

MessageReceivedEvent event = received.getEvent();
String[] cmdParam = received.getCmdParam();
Guild guild = event.getGuild();
User author = event.getAuthor();
Member member = guild.getMember(author);
Message message = event.getMessage();
MessageChannel channel = event.getChannel();
String msg = message.getContentDisplay();

WelcomeMessage wel = new WelcomeMessage(event);
EmbedBuilder eb = wel.getWelcomeMessage();
Expand All @@ -50,8 +54,14 @@ public void execute() {
eb.clear();

} catch (Exception e) {
Exception ex = exceptIf.commandException(e, event);
throw new RuntimeException(ex);

}
}

@Override
public Object executeResponse(Object obj) {
return null;
}


}
52 changes: 25 additions & 27 deletions src/main/java/jp/skyblock/Command/RoleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import jp.skyblock.Core.Config;
import jp.skyblock.Core.Const.Constant;
import jp.skyblock.Core.Observer.Message.Received;
import jp.skyblock.Utility.EmojiUtil;
import jp.skyblock.Utility.ExceptionIf;
import jp.skyblock.Utility.RoleUtil;
import jp.skyblock.Utility.WelcomeMessage;
import net.dv8tion.jda.api.EmbedBuilder;
Expand All @@ -18,35 +18,28 @@

import java.util.ArrayList;

import static jp.skyblock.Core.Config.CONFIG_DIR;
import static jp.skyblock.Core.Config.DISCORD_CONFIG_FILE;
import static jp.skyblock.Core.Const.Enums.EmojiType.TUSB;
import static jp.skyblock.Core.Const.Enums.EmojiType.TUSBCHANG_DOUBT;
import static jp.skyblock.Core.Const.Enums.EmojiType.TUSBCHANG_SAD;
import static jp.skyblock.Core.Const.Enums.EmojiType.TUSBCHANG_TALK;

public class RoleCommand implements CommandExecIf {
final EmojiUtil emj = Constant.emj;
final ExceptionIf exceptIf = Constant.exceptIf;
final RoleUtil roleUtil = new RoleUtil();

@Override
public void execute() throws ExceptionIf {
MessageReceivedEvent event = CommandExecIf.CommandEvent.getEvent();
String[] cmdParam = CommandExecIf.CommandEvent.cmdParam;
Guild guild = event.getGuild();
User author = event.getAuthor();
Member member = guild.getMember(author);
Message message = event.getMessage();
MessageChannel channel = event.getChannel();
public void execute() throws Exception {
MessageReceivedEvent event = Received.getEvent();
String[] cmdParam = Received.getCmdParam();
Guild guild = Received.getGuild();
User author = Received.getAuthor();
Member member = Received.getMember();
Message message = Received.getMessage();
MessageChannel channel = Received.getChannel();

WelcomeMessage wel = new WelcomeMessage(event);
EmbedBuilder eb = wel.getWelcomeMessage();

Config conf = new Config();
String propPath = CONFIG_DIR;
String propName = DISCORD_CONFIG_FILE;


long mesId = 0L;
boolean isForce; // デフォルトTrue
Expand Down Expand Up @@ -86,22 +79,27 @@ public void execute() throws ExceptionIf {
channel.sendMessage("That message doesn't exist !").queue();
}
}
throw new RuntimeException(e);
});

// Save Properties
if (finalIsForce || !new Config().getProperty("OBSERVER_REACTION_CHANNEL_ID").equals("")) {
conf.saveList(propPath + propName, "OBSERVER_REACTION_CHANNEL_ID", String.valueOf(channel.getIdLong()));
}

if (finalIsForce || new Config().getProperty("OBSERVER_REACTION_MESSAGE_ID").equals("")) {
conf.saveList(propPath + propName, "OBSERVER_REACTION_MESSAGE_ID", String.valueOf(mesId));
}

// if (finalIsForce || !new Config().getProperty("OBSERVER_REACTION_CHANNEL_ID").equals("")) {
// conf.saveList(propPath + propName, "OBSERVER_REACTION_CHANNEL_ID", String.valueOf(channel.getIdLong()));
// }
//
// if (finalIsForce || new Config().getProperty("OBSERVER_REACTION_MESSAGE_ID").equals("")) {
// conf.saveList(propPath + propName, "OBSERVER_REACTION_MESSAGE_ID", String.valueOf(mesId));
// }

// throw new Exception("ERROR");
} catch (Exception e) {
Exception ex = exceptIf.commandException(e, event);
throw new ExceptionIf(ex);
e.printStackTrace();
throw sendError(e, "ERROR", event);
}
}

@Override
public Object executeResponse(Object obj) {
return null;
}

}
2 changes: 0 additions & 2 deletions src/main/java/jp/skyblock/Core/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import java.util.HashSet;

public class Config {
public static final String CONFIG_DIR = getDir() + File.separator + "config";
public static final String DISCORD_CONFIG_FILE = File.separator + "Discord.properties";
private static final Parameters params = new Parameters();
private static final Configurations configs = new Configurations();
private static PropertiesConfiguration config;
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/jp/skyblock/Core/Const/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@
import jp.skyblock.Utility.ExceptionIf;
import org.apache.log4j.Logger;

import java.io.File;

import static jp.skyblock.Core.Config.getDir;

public class Constant {
public static final EmojiUtil emj = new EmojiUtil();
public static final ExceptionIf exceptIf = new ExceptionIf();
public static final Config config = new Config();


public static final Logger logger = Logger.getLogger(TusbChangRoleBot.class);
Expand All @@ -32,7 +36,4 @@ public class Constant {
// Emoji
public static final String EmojiSeparator = ":";

//TODO enum にすべき


}
Loading

0 comments on commit eff3689

Please sign in to comment.