From eff3689cc5c9623a3d4a2d6996201294d5bd05d2 Mon Sep 17 00:00:00 2001 From: waxsd100 Date: Wed, 17 Jun 2020 01:21:04 +0900 Subject: [PATCH] =?UTF-8?q?=E7=8B=AC=E8=87=AA=E5=AE=9F=E8=A3=85=E3=82=A8?= =?UTF-8?q?=E3=83=A9=E3=83=BC=E3=83=A1=E3=83=83=E3=82=BB=E3=83=BC=E3=82=B8?= =?UTF-8?q?=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + config/database.properties | 4 + .../jp/skyblock/Command/CommandExecIf.java | 35 ++----- .../jp/skyblock/Command/DefaultCommand.java | 5 +- .../java/jp/skyblock/Command/PingCommand.java | 24 +++-- .../java/jp/skyblock/Command/RoleCommand.java | 52 +++++----- src/main/java/jp/skyblock/Core/Config.java | 2 - .../java/jp/skyblock/Core/Const/Constant.java | 9 +- .../Core/Observer/Message/Received.java | 94 ++++++++++++++----- .../java/jp/skyblock/TusbChangRoleBot.java | 14 ++- .../java/jp/skyblock/Utility/ExceptionIf.java | 35 ++++--- .../skyblock/Utility/db/ConnectionPool.java | 19 ++++ .../skyblock/Utility/db/ConnectionPool4J.java | 27 ++++++ src/test/data/database.properties | 0 14 files changed, 215 insertions(+), 106 deletions(-) create mode 100644 config/database.properties create mode 100644 src/main/java/jp/skyblock/Utility/db/ConnectionPool.java create mode 100644 src/main/java/jp/skyblock/Utility/db/ConnectionPool4J.java create mode 100644 src/test/data/database.properties diff --git a/build.gradle b/build.gradle index 5ecfe1b..3e08c90 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/config/database.properties b/config/database.properties new file mode 100644 index 0000000..0aa4e1e --- /dev/null +++ b/config/database.properties @@ -0,0 +1,4 @@ +user="root" +password="none" +host="localhost" +port="3306" diff --git a/src/main/java/jp/skyblock/Command/CommandExecIf.java b/src/main/java/jp/skyblock/Command/CommandExecIf.java index cbcc33e..ba0e65d 100644 --- a/src/main/java/jp/skyblock/Command/CommandExecIf.java +++ b/src/main/java/jp/skyblock/Command/CommandExecIf.java @@ -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: @@ -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; - } - } } diff --git a/src/main/java/jp/skyblock/Command/DefaultCommand.java b/src/main/java/jp/skyblock/Command/DefaultCommand.java index 3c5d5fa..744af7c 100644 --- a/src/main/java/jp/skyblock/Command/DefaultCommand.java +++ b/src/main/java/jp/skyblock/Command/DefaultCommand.java @@ -12,9 +12,9 @@ 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() { } @@ -22,4 +22,5 @@ public void execute() throws ExceptionIf { public Object executeResponse(Object obj) { return null; } + } diff --git a/src/main/java/jp/skyblock/Command/PingCommand.java b/src/main/java/jp/skyblock/Command/PingCommand.java index b31fa84..c617373 100644 --- a/src/main/java/jp/skyblock/Command/PingCommand.java +++ b/src/main/java/jp/skyblock/Command/PingCommand.java @@ -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; @@ -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(); @@ -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; + } + + } diff --git a/src/main/java/jp/skyblock/Command/RoleCommand.java b/src/main/java/jp/skyblock/Command/RoleCommand.java index a0e6455..2e1d3d2 100644 --- a/src/main/java/jp/skyblock/Command/RoleCommand.java +++ b/src/main/java/jp/skyblock/Command/RoleCommand.java @@ -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; @@ -18,8 +18,6 @@ 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; @@ -27,26 +25,21 @@ 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 @@ -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; + } + } diff --git a/src/main/java/jp/skyblock/Core/Config.java b/src/main/java/jp/skyblock/Core/Config.java index 9ad5c64..72312ce 100644 --- a/src/main/java/jp/skyblock/Core/Config.java +++ b/src/main/java/jp/skyblock/Core/Config.java @@ -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; diff --git a/src/main/java/jp/skyblock/Core/Const/Constant.java b/src/main/java/jp/skyblock/Core/Const/Constant.java index 4cbd4bb..0d41b2c 100644 --- a/src/main/java/jp/skyblock/Core/Const/Constant.java +++ b/src/main/java/jp/skyblock/Core/Const/Constant.java @@ -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); @@ -32,7 +36,4 @@ public class Constant { // Emoji public static final String EmojiSeparator = ":"; - //TODO enum にすべき - - } diff --git a/src/main/java/jp/skyblock/Core/Observer/Message/Received.java b/src/main/java/jp/skyblock/Core/Observer/Message/Received.java index d928ff1..63906c5 100644 --- a/src/main/java/jp/skyblock/Core/Observer/Message/Received.java +++ b/src/main/java/jp/skyblock/Core/Observer/Message/Received.java @@ -1,7 +1,6 @@ package jp.skyblock.Core.Observer.Message; import jp.skyblock.Command.CommandExecIf; -import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.ChannelType; import net.dv8tion.jda.api.entities.Guild; @@ -14,11 +13,9 @@ import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import org.apache.commons.lang3.StringUtils; -import java.awt.*; -import java.util.Arrays; import java.util.Objects; -import static jp.skyblock.Command.CommandExecIf.getCommandExec; +import static jp.skyblock.Command.CommandExecIf.getInstance; import static jp.skyblock.Core.Const.Constant.PREFIX; import static jp.skyblock.Core.Const.Constant.logger; @@ -32,20 +29,80 @@ private Boolean isCommand(String msg) { return msg.length() > 1 && PREFIX.equals(StringUtils.left(msg, 1)); } + /** + * @return event + */ + + private static String[] cmdParam; + private static MessageReceivedEvent event; + + public static Guild getGuild() { + return guild; + } + + public static User getAuthor() { + return author; + } + + public static Member getMember() { + return member; + } + + public static Message getMessage() { + return message; + } + + public static MessageChannel getChannel() { + return channel; + } + + public MessageChannel getTextChannel() { + return textChannel; + } + + + public String getMsg() { + return msg; + } + + private static Guild guild; + private static User author; + private static Member member; + private static Message message; + private static MessageChannel channel; + private static TextChannel textChannel; + private static String msg; + + public static MessageReceivedEvent getEvent() { + return event; + } + public void setEvent(MessageReceivedEvent event) { + Received.event = event; + } + public static String[] getCmdParam() { return cmdParam; } + public void setCmdParam(String[] cmdParam) { + Received.cmdParam = cmdParam; + } + + + + /** * @param event */ public void sendMessage(MessageReceivedEvent event) { try { //これらは JDA のすべてのイベントで提供されます。 + this.setEvent(event); JDA jda = event.getJDA(); long responseNumber = event.getResponseNumber(); - Guild guild = event.getGuild(); - User author = event.getAuthor(); - Message message = event.getMessage(); - MessageChannel channel = event.getChannel(); - TextChannel textChannel = event.getTextChannel(); - Member member = event.getMember(); + guild = event.getGuild(); + author = event.getAuthor(); + member = guild.getMember(author); + message = event.getMessage(); + channel = event.getChannel(); + textChannel = event.getTextChannel(); + member = event.getMember(); String msg = message.getContentDisplay(); String name = message.isWebhookMessage() ? author.getName() : Objects.requireNonNull(member).getEffectiveName(); @@ -54,23 +111,12 @@ public void sendMessage(MessageReceivedEvent event) { logger.info(String.format("(%s)[%s %s]<%s>: %s", responseNumber, guild.getName(), textChannel.getName(), name, msg)); if (isCommand(msg)) { - CommandExecIf command = getCommandExec(msg); + setCmdParam(msg.split(" ")); + CommandExecIf command = getInstance(getCmdParam()); try { - CommandExecIf.CommandEvent.setEvent(event); command.execute(); - } catch (Exception e) { - EmbedBuilder eb = new EmbedBuilder(); - TextChannel c = guild.getTextChannelById(714836325592989737L); - eb.setColor(Color.RED); - eb.setTitle(e.getMessage()); - eb.setAuthor(author.getName()); - eb.setDescription(Arrays.toString(e.getStackTrace())); - eb.setFooter("(c) TUSB ~ 想像を超えた創造を ~", guild.getIconUrl()); - Objects.requireNonNull(c).sendMessage(eb.build()) - .queue(); - eb.clear(); - + e.printStackTrace(); } } } else if (event.isFromType(ChannelType.PRIVATE)) { diff --git a/src/main/java/jp/skyblock/TusbChangRoleBot.java b/src/main/java/jp/skyblock/TusbChangRoleBot.java index 390eec9..22f0440 100644 --- a/src/main/java/jp/skyblock/TusbChangRoleBot.java +++ b/src/main/java/jp/skyblock/TusbChangRoleBot.java @@ -13,13 +13,23 @@ import jp.skyblock.Core.Config; import jp.skyblock.Core.Const.Constant; import jp.skyblock.Executer.BotExecute; +import jp.skyblock.Utility.db.ConnectionPool; import org.apache.log4j.PropertyConfigurator; +import java.io.File; + +import static jp.skyblock.Core.Config.getDir; +import static jp.skyblock.Core.Const.Constant.*; + public class TusbChangRoleBot { + private static final String CONFIG_DIR = getDir() + File.separator + "config"; + private static final String DISCORD_CONFIG_FILE = File.separator + "Discord.properties"; + private static final String DATABASE_CONFIG_FILE = File.separator + "Database.properties"; + public static void main(String[] args) { loadConf(); - Constant.logger.info("Config Load"); + logger.info("Config Load"); new BotExecute(); } @@ -28,7 +38,7 @@ private static void loadConf() { try { PropertyConfigurator.configure("config/log4j.properties"); // Discord 用ConfigファイルをLoad - config.load(Config.CONFIG_DIR + Config.DISCORD_CONFIG_FILE); + config.load(CONFIG_DIR + DISCORD_CONFIG_FILE); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/main/java/jp/skyblock/Utility/ExceptionIf.java b/src/main/java/jp/skyblock/Utility/ExceptionIf.java index 56cb62a..27b19d8 100644 --- a/src/main/java/jp/skyblock/Utility/ExceptionIf.java +++ b/src/main/java/jp/skyblock/Utility/ExceptionIf.java @@ -10,28 +10,41 @@ package jp.skyblock.Utility; +import jp.skyblock.Command.CommandExecIf; import jp.skyblock.Core.Const.Constant; +import jp.skyblock.Core.Observer.Message.Received; 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; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import org.jetbrains.annotations.NotNull; -public class ExceptionIf extends Exception { - final EmojiUtil emj = Constant.emj; +public interface ExceptionIf { - public ExceptionIf() { + + /** + * @param e + * @return + */ + + default Exception commandException(Exception e){ + return e; } - public ExceptionIf(Exception e) { - e.printStackTrace(); - throw new RuntimeException(e); + + default Exception commandException(Exception e, String ErrorMessage){ + return e; } - public Exception commandException(Exception e, MessageReceivedEvent event) { - Guild guild = event.getGuild(); - User author = event.getAuthor(); - String msg = event.getMessage().getContentDisplay(); - MessageChannel channel = event.getChannel(); + Received received = new Received(); + static Exception commandException(Exception e, String ErrorMessage, MessageReceivedEvent event) { + EmojiUtil emj = Constant.emj; + Guild guild = Received.getGuild(); + User author = Received.getAuthor(); + MessageChannel channel = Received.getChannel(); + String msg = Received.getMessage().getContentDisplay(); String errorMes = String.format("(%s)[%s %s]<%s>: %s \n %s", event.getResponseNumber(), guild.getName(), channel.getName(), author, msg, e.getLocalizedMessage()); diff --git a/src/main/java/jp/skyblock/Utility/db/ConnectionPool.java b/src/main/java/jp/skyblock/Utility/db/ConnectionPool.java new file mode 100644 index 0000000..9e50e2a --- /dev/null +++ b/src/main/java/jp/skyblock/Utility/db/ConnectionPool.java @@ -0,0 +1,19 @@ +/* + * ************************************************* + * TusbChangRoleBot + * Copyright (c) 2020 ConnectionPool.java + * + * This software is released under the TUSB. + * see https://github.com/TUSB/TusbChangRoleBot + * ************************************************ + */ + +package jp.skyblock.Utility.db; + +public class ConnectionPool { + + public static void initializeInstance(String path){ + new ConnectionPool4J().configLoader(path); + }; + +} diff --git a/src/main/java/jp/skyblock/Utility/db/ConnectionPool4J.java b/src/main/java/jp/skyblock/Utility/db/ConnectionPool4J.java new file mode 100644 index 0000000..63426bb --- /dev/null +++ b/src/main/java/jp/skyblock/Utility/db/ConnectionPool4J.java @@ -0,0 +1,27 @@ +/* + * ************************************************* + * TusbChangRoleBot + * Copyright (c) 2020 ConnectionPool4G.java + * + * This software is released under the TUSB. + * see https://github.com/TUSB/TusbChangRoleBot + * ************************************************ + */ + +package jp.skyblock.Utility.db; + +import jp.skyblock.Core.Config; +import jp.skyblock.Core.Const.Constant; + +public class ConnectionPool4J { + Config config = Constant.config; + public ConnectionPool4J(){ + + } + protected void configLoader(String baseFile){ + config.load(baseFile); + System.out.println(config.getPropertyList("user")); + + } + +} diff --git a/src/test/data/database.properties b/src/test/data/database.properties new file mode 100644 index 0000000..e69de29