From 634d96b426fdf953dcc2968d2ca72a8241e75445 Mon Sep 17 00:00:00 2001 From: Dalton Date: Thu, 28 Sep 2017 19:27:07 -0400 Subject: [PATCH] More object oriented programming --- build.gradle | 2 +- .../java/me/majrly/database/Database.java | 23 ++-- .../me/majrly/database/DatabaseManager.java | 40 ------- src/main/java/me/savvy/rixa/Rixa.java | 44 ++++++-- .../rixa/commands/admin/AddRoleCommand.java | 6 +- .../rixa/commands/admin/BatchMoveCommand.java | 3 +- .../rixa/commands/admin/ConfigCommand.java | 23 ++-- .../commands/admin/RemoveRoleCommand.java | 3 +- .../commands/admin/RoleRewardsCommand.java | 7 +- .../rixa/commands/admin/TwitterCommand.java | 3 - .../rixa/commands/general/InfoCommand.java | 1 - .../commands/general/LeaderboardCommand.java | 8 +- .../rixa/commands/general/LevelsCommand.java | 8 +- .../rixa/commands/general/MusicCommand.java | 11 +- .../rixa/commands/general/PingCommand.java | 2 - .../commands/general/ServerInfoCommand.java | 3 +- .../commands/mod/DeleteMessagesCommand.java | 3 +- .../savvy/rixa/commands/mod/MuteCommand.java | 3 +- .../commands/mod/PurgeMessagesCommand.java | 3 +- .../rixa/commands/mod/RaidModeCommand.java | 3 +- .../commands/owner/CheckGuildCommand.java | 1 - .../database/sql/other/DatabaseTables.java | 2 +- .../java/me/savvy/rixa/events/BotEvent.java | 3 +- .../me/savvy/rixa/events/MemberEvent.java | 4 +- .../me/savvy/rixa/events/MessageEvent.java | 74 +++++++------ .../java/me/savvy/rixa/events/Shutdown.java | 22 ++++ .../java/me/savvy/rixa/extras/polls/Poll.java | 1 - .../java/me/savvy/rixa/guild/RixaGuild.java | 54 ++++----- .../rixa/guild/management/GuildSettings.java | 6 +- .../savvy/rixa/guild/management/Guilds.java | 35 ++++++ .../me/savvy/rixa/guild/user/UserData.java | 5 +- .../me/savvy/rixa/modules/RixaModule.java | 6 + .../savvy/rixa/modules/economy/Economy.java | 49 ++++++++- .../rixa/modules/economy/EconomyData.java | 25 +++++ .../rixa/modules/levels/LevelsModule.java | 103 ++++++------------ .../modules/music/AudioPlayerSendHandler.java | 2 +- .../savvy/rixa/modules/music/MusicModule.java | 81 ++++---------- .../reactions/react/ConfigReaction.java | 3 +- .../modules/reactions/react/HelpReaction.java | 4 +- .../reactions/react/LeaderboardReaction.java | 9 +- .../rixa/modules/twitter/TwitterModule.java | 13 ++- .../me/savvy/rixa/utils/DatabaseUtils.java | 29 +++++ .../me/savvy/rixa/utils/MessageBuilder.java | 5 +- 43 files changed, 407 insertions(+), 328 deletions(-) delete mode 100644 src/main/java/me/majrly/database/DatabaseManager.java create mode 100644 src/main/java/me/savvy/rixa/events/Shutdown.java create mode 100644 src/main/java/me/savvy/rixa/guild/management/Guilds.java create mode 100644 src/main/java/me/savvy/rixa/modules/economy/EconomyData.java diff --git a/build.gradle b/build.gradle index 771a601..bd78fb0 100644 --- a/build.gradle +++ b/build.gradle @@ -38,4 +38,4 @@ compileJava.options.encoding = 'UTF-8' compileJava.options.fork = true // Change this if you are getting errors building -compileJava.options.forkOptions.executable = 'C:\\Program Files\\Java\\jdk1.8.0_144\\bin\\javac.exe' \ No newline at end of file +compileJava.options.forkOptions.executable = 'C:\\Program Files\\Java\\jdk1.8.0_131\\bin\\javac.exe' \ No newline at end of file diff --git a/src/main/java/me/majrly/database/Database.java b/src/main/java/me/majrly/database/Database.java index 077cd54..39aaaf9 100644 --- a/src/main/java/me/majrly/database/Database.java +++ b/src/main/java/me/majrly/database/Database.java @@ -26,7 +26,6 @@ public class Database { private String database; private int port = 3306; - private int timeout = 60 * 1000; private HikariDataSource source; private HikariConfig config = new HikariConfig(); @@ -49,6 +48,8 @@ public class Database { this.config.setUsername(this.username = username); this.config.setPassword(this.password = password); this.source = new HikariDataSource(config); + this.source.setPoolName("rixa"); + this.source.setMaximumPoolSize(400); } /** @@ -61,15 +62,6 @@ public class Database { return new DatabaseOptions(); } - /** - * Connects to the database - * - * @return Whether it connected or not - * @since 1.0.0 - */ - public void init() { - } - /** * Sends a query to the database * @@ -88,7 +80,7 @@ public class Database { return Optional.of(preparedStatement.executeUpdate()); } } catch (SQLException exception) { - exception.printStackTrace(); + System.out.println("INFO: Couldn't send update / query! : " + exception.getLocalizedMessage()); return Optional.empty(); } } @@ -106,7 +98,7 @@ public class Database { } } catch (SQLException e) { // Can't handle closing statement - e.printStackTrace(); + System.out.println("INFO: Close connection! : " + e.getLocalizedMessage()); } } @@ -119,7 +111,7 @@ public class Database { */ public Optional prepare(Statement statement) { try { - PreparedStatement preparedStatement = source.getConnection().prepareStatement(statement.getSQL()); + PreparedStatement preparedStatement = getConnection().get().prepareStatement(statement.getSQL()); for (Map.Entry parameter : statement.getParameters().entrySet()) { switch (parameter.getValue().getType()) { case STRING: @@ -156,7 +148,7 @@ public class Database { } return Optional.of(preparedStatement); } catch (SQLException exception) { - exception.printStackTrace(); + System.out.println("INFO: Couldn't prepare statement : " + exception.getLocalizedMessage()); return Optional.empty(); } } @@ -172,7 +164,7 @@ public class Database { try { return Optional.of(source.getConnection().prepareStatement(sql)); } catch (SQLException exception) { - exception.printStackTrace(); + System.out.println("INFO: Couldn't send update / query! : " + exception.getLocalizedMessage()); return Optional.empty(); } } @@ -187,6 +179,7 @@ public class Database { try { return Optional.of(source.getConnection()); } catch (SQLException e) { + System.out.println("INFO: Couldn't get connection : " + e.getLocalizedMessage()); return Optional.empty(); } } diff --git a/src/main/java/me/majrly/database/DatabaseManager.java b/src/main/java/me/majrly/database/DatabaseManager.java deleted file mode 100644 index 060108f..0000000 --- a/src/main/java/me/majrly/database/DatabaseManager.java +++ /dev/null @@ -1,40 +0,0 @@ -package me.majrly.database; - -import java.util.HashMap; - -/** - * Apart of the database api to manage all your databases - * - * @author Majrly - * @since 1.0.0 - */ -public class DatabaseManager { - - private static HashMap databases = new HashMap<>(); - - /** - * Add a database to {@link #databases} - * - * @param database The database you want to add - * @since 1.0.0 - */ - public static void addDatabase(Database database) { - databases.put(database.getName(), database); - } - - /** - * Get a database with specified name from {@link #databases} - * - * @param name The name of the database you want to obtain - * @return The database wrapper - * @since 1.0.0 - */ - public static Database getDatabase(String name) { - return databases.get(name); - } - - // Getters - public static HashMap getDatabases() { - return databases; - } -} \ No newline at end of file diff --git a/src/main/java/me/savvy/rixa/Rixa.java b/src/main/java/me/savvy/rixa/Rixa.java index 5688d5d..a8a4ef1 100644 --- a/src/main/java/me/savvy/rixa/Rixa.java +++ b/src/main/java/me/savvy/rixa/Rixa.java @@ -25,7 +25,9 @@ import me.savvy.rixa.data.filemanager.LanguageManager; import me.savvy.rixa.events.BotEvent; import me.savvy.rixa.events.MemberEvent; import me.savvy.rixa.events.MessageEvent; +import me.savvy.rixa.events.Shutdown; import me.savvy.rixa.events.VoiceChannel; +import me.savvy.rixa.guild.management.Guilds; import me.savvy.rixa.modules.reactions.handlers.React; import me.savvy.rixa.modules.reactions.handlers.ReactionManager; import me.savvy.rixa.modules.reactions.react.ConfigReaction; @@ -38,6 +40,7 @@ import net.dv8tion.jda.core.OnlineStatus; import net.dv8tion.jda.core.entities.Game; import net.dv8tion.jda.core.exceptions.RateLimitedException; import net.dv8tion.jda.core.hooks.AnnotatedEventManager; +import net.dv8tion.jda.core.requests.Route; import javax.security.auth.login.LoginException; import java.io.File; @@ -65,9 +68,6 @@ public class Rixa { @Getter @Setter private static Database database; - private static ChatterBotFactory factory; - private static ChatterBotSession chatBotSession; - private static ChatterBot chatBot; @Getter @Setter private LanguageManager languageManager; @@ -75,17 +75,28 @@ public class Rixa { @Setter private ScheduledExecutorService executorService; + private static ChatterBotFactory factory; + private static ChatterBotSession chatBotSession; + private static ChatterBot chatBot; + // String search = event.getMessage().getContent().substring(event.getMessage().getContent().indexOf(" ") + 1); public static void main(String[] args) { instance = new Rixa(); shardsList = new LinkedList<>(); - // config = new ConfigManager(); config = new ConfigManager(new File("Rixa/config.json")); load(); + + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + getShardsList().forEach(JDA::shutdown); + } + }); } private static void load() { getInstance().setExecutorService(Executors.newSingleThreadScheduledExecutor()); + database = Database.options() .type("mysql") .hostname(String.valueOf(config.getJsonObject().getJSONObject("sql").getString("hostName")), config.getJsonObject().getJSONObject("sql").getInt("portNumber")) @@ -97,6 +108,7 @@ public class Rixa { database.send(new Update(databaseTables.getQuery())); getInstance().getLogger().info("Done checking " + databaseTables.toString()); }); + getInstance().setLanguageManager(new LanguageManager(new File("Rixa/languages/language.json"))); try { int shards = 5; @@ -109,6 +121,7 @@ public class Rixa { .addEventListener(new BotEvent()) .addEventListener(new MemberEvent()) .addEventListener(new VoiceChannel()) + .addEventListener(new Shutdown()) .setGame(Game.of(config.getJsonObject().getString("botGame"))) .setAutoReconnect(true) .setStatus(OnlineStatus.ONLINE) @@ -120,15 +133,17 @@ public class Rixa { } catch (LoginException | InterruptedException | RateLimitedException e) { e.printStackTrace(); } + + Guilds.getGuilds().values().parallelStream().forEach((rixaGuild) -> rixaGuild.load()); + timeUp = System.currentTimeMillis(); - register(new CommandExec[]{ - new InfoCommand(), new ServerInfoCommand(), new HelpCommand(), + register(new InfoCommand(), new ServerInfoCommand(), new HelpCommand(), new DeleteMessagesCommand(), new PingCommand(), new PurgeMessagesCommand(), new BatchMoveCommand(), new MuteCommand(), new MusicCommand(), new ConfigCommand(), new UrbanDictionaryCommand(), new YoutubeCommand(), new AddRoleCommand(), new RemoveRoleCommand(), new LevelsCommand(), - new LeaderboardCommand(), new RaidModeCommand()}); - register(new React[]{new HelpReaction(), new ConfigReaction(), new LeaderboardReaction()}); + new LeaderboardCommand(), new RaidModeCommand()); + register(new HelpReaction(), new ConfigReaction(), new LeaderboardReaction()); try { factory = new ChatterBotFactory(); chatBot = factory.create(ChatterBotType.PANDORABOTS, "b0dafd24ee35a477"); @@ -138,13 +153,13 @@ public class Rixa { } } - private static void register(CommandExec commandExecs[]) { + private static void register(CommandExec... commandExecs) { for (CommandExec command : commandExecs) { CommandHandler.registerCommand(command); } } - private static void register(React react[]) { + private static void register(React... react) { for (React reaction : react) { ReactionManager.registerReaction(reaction); } @@ -159,10 +174,15 @@ public class Rixa { } public void exit() { + getShardsList().forEach(JDA::shutdown); + } + + public void close() { try { + Guilds.getGuilds().values().parallelStream().forEach((rixaGuild) -> rixaGuild.save()); + Thread.sleep(1200); database.close(); - getShardsList().forEach(JDA::shutdown); - Thread.sleep(5000); + Thread.sleep(200); System.exit(0); } catch (InterruptedException ex) { getLogger().severe("Could not shutdown Rixa instance."); diff --git a/src/main/java/me/savvy/rixa/commands/admin/AddRoleCommand.java b/src/main/java/me/savvy/rixa/commands/admin/AddRoleCommand.java index 02dcede..0cfc5b4 100644 --- a/src/main/java/me/savvy/rixa/commands/admin/AddRoleCommand.java +++ b/src/main/java/me/savvy/rixa/commands/admin/AddRoleCommand.java @@ -5,15 +5,13 @@ import me.savvy.rixa.commands.handlers.CommandExec; import me.savvy.rixa.commands.handlers.CommandType; import me.savvy.rixa.commands.handlers.RixaPermission; import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; import me.savvy.rixa.utils.MessageBuilder; import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.Role; -import net.dv8tion.jda.core.entities.TextChannel; import net.dv8tion.jda.core.entities.User; import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent; import net.dv8tion.jda.core.exceptions.PermissionException; -import net.dv8tion.jda.core.managers.GuildManager; -import net.dv8tion.jda.core.requests.restaction.InviteAction; import java.util.List; @@ -28,7 +26,7 @@ public class AddRoleCommand implements CommandExec { channelType = ChannelType.TEXT, usage = "%paddrole", mainCommand = "addrole", aliases = {"ar", "addroles", "ars"}) public void execute(GuildMessageReceivedEvent event) { - RixaGuild rixaGuild = RixaGuild.getGuild(event.getGuild()); + RixaGuild rixaGuild = Guilds.getGuild(event.getGuild()); if(!rixaGuild.hasPermission(event.getMember(), RixaPermission.ADD_ROLE)) { new MessageBuilder(event.getMember().getAsMention() + ", you do not have permission for this command.").setColor(event.getMember().getColor()).queue(event.getChannel()); return; diff --git a/src/main/java/me/savvy/rixa/commands/admin/BatchMoveCommand.java b/src/main/java/me/savvy/rixa/commands/admin/BatchMoveCommand.java index 8e9c147..70a2911 100644 --- a/src/main/java/me/savvy/rixa/commands/admin/BatchMoveCommand.java +++ b/src/main/java/me/savvy/rixa/commands/admin/BatchMoveCommand.java @@ -4,6 +4,7 @@ import me.savvy.rixa.commands.handlers.Command; import me.savvy.rixa.commands.handlers.CommandExec; import me.savvy.rixa.commands.handlers.RixaPermission; import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; import me.savvy.rixa.utils.MessageBuilder; import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.Member; @@ -25,7 +26,7 @@ public class BatchMoveCommand implements CommandExec { description = "Move users within one role to another!", channelType = ChannelType.TEXT) public void execute(GuildMessageReceivedEvent event) { - RixaGuild rixaGuild = RixaGuild.getGuild(event.getGuild()); + RixaGuild rixaGuild = Guilds.getGuild(event.getGuild()); if(!rixaGuild.hasPermission(event.getMember(), RixaPermission.BATCH_MOVE)) { new MessageBuilder(event.getMember().getAsMention() + ", you do not have permission for this command.").setColor(event.getMember().getColor()).queue(event.getChannel()); return; diff --git a/src/main/java/me/savvy/rixa/commands/admin/ConfigCommand.java b/src/main/java/me/savvy/rixa/commands/admin/ConfigCommand.java index 7055f39..4fa834c 100644 --- a/src/main/java/me/savvy/rixa/commands/admin/ConfigCommand.java +++ b/src/main/java/me/savvy/rixa/commands/admin/ConfigCommand.java @@ -5,6 +5,9 @@ import me.savvy.rixa.commands.handlers.CommandExec; import me.savvy.rixa.commands.handlers.CommandType; import me.savvy.rixa.commands.handlers.RixaPermission; import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; +import me.savvy.rixa.modules.levels.LevelsModule; +import me.savvy.rixa.modules.music.MusicModule; import me.savvy.rixa.utils.MessageBuilder; import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.Member; @@ -55,7 +58,7 @@ public class ConfigCommand implements CommandExec { type = CommandType.ADMIN, channelType = ChannelType.TEXT) public void execute(GuildMessageReceivedEvent event) { - RixaGuild rixaGuild = RixaGuild.getGuild(event.getGuild()); + RixaGuild rixaGuild = Guilds.getGuild(event.getGuild()); if (!rixaGuild.hasPermission(event.getMember(), RixaPermission.ACCESS_CONFIG)) { new MessageBuilder(event.getMember().getAsMention() + ", you do not have permission for this command.") .setColor(event.getMember().getColor()).queue(event.getChannel()); @@ -166,34 +169,34 @@ public class ConfigCommand implements CommandExec { return; } Role role = event.getMessage().getMentionedRoles().get(0); - rixaGuild.getMusicModule().setRole(role.getId()); + ((MusicModule) rixaGuild.getModule("Music")).setMusicRole(role.getId()); new MessageBuilder("Successfully set music role to " + role.getName() + "!").setColor(event.getMember().getColor()).queue(event.getChannel()); } } else if (messages[1].equalsIgnoreCase("enable")) { if (messages[2].equalsIgnoreCase("music")) { - RixaGuild.getGuild(event.getGuild()).getMusicModule().setEnabled(true); + ((MusicModule) rixaGuild.getModule("Music")).setEnabled(true); new MessageBuilder("Successfully enabled the music module!").setColor(event.getMember().getColor()).queue(event.getChannel()); } else if (messages[2].equalsIgnoreCase("levels")) { - RixaGuild.getGuild(event.getGuild()).getLevelsModule().setEnabled(true); + ((LevelsModule) rixaGuild.getModule("Levels")).setEnabled(true); event.getChannel().sendMessage("Successfully enabled the levels module").queue(); } else if (messages[2].equalsIgnoreCase("joinverification")) { - RixaGuild.getGuild(event.getGuild()).getGuildSettings().setJoinVerification(true); + Guilds.getGuild(event.getGuild()).getGuildSettings().setJoinVerification(true); new MessageBuilder("Successfully enabled Join Verification!").setColor(event.getMember().getColor()).queue(event.getChannel()); } } else if (messages[1].equalsIgnoreCase("disable")) { if (messages[2].equalsIgnoreCase("music")) { - RixaGuild.getGuild(event.getGuild()).getMusicModule().setEnabled(false); + ((MusicModule) rixaGuild.getModule("Music")).setEnabled(false); new MessageBuilder("Successfully disabled the music module!").setColor(event.getMember().getColor()).queue(event.getChannel()); } else if (messages[2].equalsIgnoreCase("levels")) { - RixaGuild.getGuild(event.getGuild()).getLevelsModule().setEnabled(false); + ((MusicModule) rixaGuild.getModule("Music")).setEnabled(false); event.getChannel().sendMessage("Successfully disabled the levels module").queue(); } else if (messages[2].equalsIgnoreCase("joinverification")) { - RixaGuild.getGuild(event.getGuild()).getGuildSettings().setJoinVerification(false); + Guilds.getGuild(event.getGuild()).getGuildSettings().setJoinVerification(false); new MessageBuilder("Successfully disabled Join Verification!").setColor(event.getMember().getColor()).queue(event.getChannel()); } else if (messages[2].equalsIgnoreCase("joinmessage")) { - RixaGuild.getGuild(event.getGuild()).getGuildSettings().setJoinMessageChannel("default_value"); + Guilds.getGuild(event.getGuild()).getGuildSettings().setJoinMessageChannel("default_value"); } else if (messages[2].equalsIgnoreCase("quitmessage")) { - RixaGuild.getGuild(event.getGuild()).getGuildSettings().setQuitMessageChannel("default_value"); + Guilds.getGuild(event.getGuild()).getGuildSettings().setQuitMessageChannel("default_value"); } } else if (messages[1].equalsIgnoreCase("addperm") || messages[1].equalsIgnoreCase("addpermission") || messages[1].equalsIgnoreCase("aperm")) { String permission = "notFound"; diff --git a/src/main/java/me/savvy/rixa/commands/admin/RemoveRoleCommand.java b/src/main/java/me/savvy/rixa/commands/admin/RemoveRoleCommand.java index 8c5c4f7..354b421 100644 --- a/src/main/java/me/savvy/rixa/commands/admin/RemoveRoleCommand.java +++ b/src/main/java/me/savvy/rixa/commands/admin/RemoveRoleCommand.java @@ -5,6 +5,7 @@ import me.savvy.rixa.commands.handlers.CommandExec; import me.savvy.rixa.commands.handlers.CommandType; import me.savvy.rixa.commands.handlers.RixaPermission; import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; import me.savvy.rixa.utils.MessageBuilder; import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.Role; @@ -25,7 +26,7 @@ public class RemoveRoleCommand implements CommandExec { channelType = ChannelType.TEXT, usage = "%premoverole", mainCommand = "removerole", aliases = {"rr", "removeroles"}) public void execute(GuildMessageReceivedEvent event) { - RixaGuild rixaGuild = RixaGuild.getGuild(event.getGuild()); + RixaGuild rixaGuild = Guilds.getGuild(event.getGuild()); if(!rixaGuild.hasPermission(event.getMember(), RixaPermission.REMOVE_ROLE)) { new MessageBuilder(event.getMember().getAsMention() + ", you do not have permission for this command.").setColor(event.getMember().getColor()).queue(event.getChannel()); return; diff --git a/src/main/java/me/savvy/rixa/commands/admin/RoleRewardsCommand.java b/src/main/java/me/savvy/rixa/commands/admin/RoleRewardsCommand.java index 3a32669..4c0c23e 100644 --- a/src/main/java/me/savvy/rixa/commands/admin/RoleRewardsCommand.java +++ b/src/main/java/me/savvy/rixa/commands/admin/RoleRewardsCommand.java @@ -4,6 +4,7 @@ import me.savvy.rixa.commands.handlers.Command; import me.savvy.rixa.commands.handlers.CommandExec; import me.savvy.rixa.commands.handlers.CommandType; import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; import me.savvy.rixa.utils.MessageBuilder; import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent; @@ -20,7 +21,7 @@ public class RoleRewardsCommand implements CommandExec { usage = "%prolerewards", mainCommand = "rolerewards", aliases = {"rolereward", "rw"}) public void execute(GuildMessageReceivedEvent event) { - RixaGuild rixaGuild = RixaGuild.getGuild(event.getGuild()); + RixaGuild rixaGuild = Guilds.getGuild(event.getGuild()); String[] args = event.getMessage().getContent().split(" "); // ?rw [level] [role] @@ -31,7 +32,7 @@ public class RoleRewardsCommand implements CommandExec { setColor(event.getMember().getColor()).queue(event.getChannel()); return; } - /*if (rixaGuild.getLevelsModule().getRoleRewards().containsKey(Integer.parseInt(args[2]))) { + /*if (((LevelsModule) rixaGuild.getModule("Levels")).getRoleRewards().containsKey(Integer.parseInt(args[2]))) { new MessageBuilder(event.getMember().getAsMention() + ", incorrect usage try [" + args[0] + " [level] [role]."). setColor(event.getMember().getColor()).queue(event.getChannel()); return; @@ -51,7 +52,7 @@ public class RoleRewardsCommand implements CommandExec { switch(args[1].toLowerCase()) { case "list": Map rewards = new HashMap<>(); - /* rixaGuild.getLevelsModule().getRoleRewards().forEach((integer, s) -> { + /* ((LevelsModule) rixaGuild.getModule("Levels")).getRoleRewards().forEach((integer, s) -> { });*/ break; diff --git a/src/main/java/me/savvy/rixa/commands/admin/TwitterCommand.java b/src/main/java/me/savvy/rixa/commands/admin/TwitterCommand.java index 265d1e9..0bcf505 100644 --- a/src/main/java/me/savvy/rixa/commands/admin/TwitterCommand.java +++ b/src/main/java/me/savvy/rixa/commands/admin/TwitterCommand.java @@ -4,10 +4,7 @@ import me.savvy.rixa.commands.handlers.Command; import me.savvy.rixa.commands.handlers.CommandExec; import me.savvy.rixa.commands.handlers.CommandType; import net.dv8tion.jda.core.entities.ChannelType; -import net.dv8tion.jda.core.entities.TextChannel; -import net.dv8tion.jda.core.entities.User; import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent; -import net.dv8tion.jda.core.requests.restaction.InviteAction; /** * Created by savit on 7/14/2017. diff --git a/src/main/java/me/savvy/rixa/commands/general/InfoCommand.java b/src/main/java/me/savvy/rixa/commands/general/InfoCommand.java index d3a604a..27071f7 100644 --- a/src/main/java/me/savvy/rixa/commands/general/InfoCommand.java +++ b/src/main/java/me/savvy/rixa/commands/general/InfoCommand.java @@ -6,7 +6,6 @@ import me.savvy.rixa.commands.handlers.CommandExec; import me.savvy.rixa.commands.handlers.CommandType; import net.dv8tion.jda.core.EmbedBuilder; import net.dv8tion.jda.core.JDA; -import net.dv8tion.jda.core.audit.ActionType; import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.Member; import net.dv8tion.jda.core.entities.MessageEmbed; diff --git a/src/main/java/me/savvy/rixa/commands/general/LeaderboardCommand.java b/src/main/java/me/savvy/rixa/commands/general/LeaderboardCommand.java index 2a2a49e..52bb8ca 100644 --- a/src/main/java/me/savvy/rixa/commands/general/LeaderboardCommand.java +++ b/src/main/java/me/savvy/rixa/commands/general/LeaderboardCommand.java @@ -3,6 +3,8 @@ package me.savvy.rixa.commands.general; import me.savvy.rixa.commands.handlers.Command; import me.savvy.rixa.commands.handlers.CommandExec; import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; +import me.savvy.rixa.modules.levels.LevelsModule; import me.savvy.rixa.utils.MessageBuilder; import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.Message; @@ -16,13 +18,13 @@ public class LeaderboardCommand implements CommandExec { aliases = {"leaderboards", "levels"}, channelType = ChannelType.TEXT) public void execute(GuildMessageReceivedEvent event) { - RixaGuild rixaGuild = RixaGuild.getGuild(event.getGuild()); - if (!rixaGuild.getLevelsModule().isEnabled()) { + RixaGuild rixaGuild = Guilds.getGuild(event.getGuild()); + if (!((LevelsModule) rixaGuild.getModule("Levels")).isEnabled()) { new MessageBuilder("Levels are not enabled on this server!").setColor(event.getMember().getColor()).queue(event.getChannel()); return; } Message message = event.getChannel().sendMessage - (rixaGuild.getLevelsModule().leaderboard + (((LevelsModule) rixaGuild.getModule("Levels")).leaderboard (event.getMember(), 1).getBuilder().build()).complete(); message.addReaction("\u2B05").complete(); message.addReaction("\u27A1").complete(); diff --git a/src/main/java/me/savvy/rixa/commands/general/LevelsCommand.java b/src/main/java/me/savvy/rixa/commands/general/LevelsCommand.java index f34df2e..6252507 100644 --- a/src/main/java/me/savvy/rixa/commands/general/LevelsCommand.java +++ b/src/main/java/me/savvy/rixa/commands/general/LevelsCommand.java @@ -4,7 +4,9 @@ import me.savvy.rixa.Rixa; import me.savvy.rixa.commands.handlers.Command; import me.savvy.rixa.commands.handlers.CommandExec; import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; import me.savvy.rixa.guild.user.UserData; +import me.savvy.rixa.modules.levels.LevelsModule; import me.savvy.rixa.utils.MessageBuilder; import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.Member; @@ -24,8 +26,8 @@ public class LevelsCommand implements CommandExec { description = "View your levels!", channelType = ChannelType.TEXT) public void execute(GuildMessageReceivedEvent event) { - RixaGuild rixaGuild = RixaGuild.getGuild(event.getGuild()); - if (!rixaGuild.getLevelsModule().isEnabled()) { + RixaGuild rixaGuild = Guilds.getGuild(event.getGuild()); + if (!((LevelsModule) rixaGuild.getModule("Levels")).isEnabled()) { new MessageBuilder("Levels are not enabled on this server!").setColor(event.getMember().getColor()).queue(event.getChannel()); return; } @@ -47,7 +49,7 @@ public class LevelsCommand implements CommandExec { public MessageBuilder getInfo(RixaGuild rixaGuild, Member member) { User author = member.getUser(); - UserData data = rixaGuild.getLevelsModule().getUserData(author.getId()); + UserData data = ((LevelsModule) rixaGuild.getModule("Levels")).getUserData(author.getId()); String query = "SELECT * FROM `levels` WHERE `guild_id` = '" + rixaGuild.getGuild().getId() + "' ORDER BY `experience` DESC"; ResultSet rs = null; try { diff --git a/src/main/java/me/savvy/rixa/commands/general/MusicCommand.java b/src/main/java/me/savvy/rixa/commands/general/MusicCommand.java index 94dd983..78ea0ad 100644 --- a/src/main/java/me/savvy/rixa/commands/general/MusicCommand.java +++ b/src/main/java/me/savvy/rixa/commands/general/MusicCommand.java @@ -20,7 +20,9 @@ import me.savvy.rixa.commands.handlers.Command; import me.savvy.rixa.commands.handlers.CommandExec; import me.savvy.rixa.commands.handlers.CommandType; import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; import me.savvy.rixa.modules.music.MusicManager; +import me.savvy.rixa.modules.music.MusicModule; import me.savvy.rixa.modules.music.TrackScheduler; import me.savvy.rixa.utils.MessageBuilder; import me.savvy.rixa.utils.YoutubeSearch; @@ -67,13 +69,14 @@ public class MusicCommand implements CommandExec { usage = "%pmusic", mainCommand = "music") public void execute(GuildMessageReceivedEvent event) { Guild guild = event.getGuild(); - RixaGuild rixaGuild = RixaGuild.getGuild(guild); - if(!rixaGuild.getMusicModule().isEnabled()) { + RixaGuild rixaGuild = Guilds.getGuild(guild); + MusicModule module = ((MusicModule) rixaGuild.getModule("Music")); + if(!module.isEnabled()) { new MessageBuilder("Sorry music is not enabled on `" + guild.getName() + "`!").setColor(event.getMember().getColor()).queue(event.getChannel()); return; } - if(rixaGuild.getMusicModule().isRoleRequired()) { - Role role = event.getGuild().getRoleById(rixaGuild.getMusicModule().getMusicRole()); + if(module.isRoleRequired()) { + Role role = event.getGuild().getRoleById(module.getMusicRole()); boolean hasRole = false; for (Role roleItem : event.getMember().getRoles()) { if (roleItem.getId().equalsIgnoreCase(role.getId())) { diff --git a/src/main/java/me/savvy/rixa/commands/general/PingCommand.java b/src/main/java/me/savvy/rixa/commands/general/PingCommand.java index 7762ed8..be9814d 100644 --- a/src/main/java/me/savvy/rixa/commands/general/PingCommand.java +++ b/src/main/java/me/savvy/rixa/commands/general/PingCommand.java @@ -6,8 +6,6 @@ import me.savvy.rixa.utils.MessageBuilder; import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent; -import java.time.temporal.ChronoUnit; - /** * Created by Timber on 5/23/2017. */ diff --git a/src/main/java/me/savvy/rixa/commands/general/ServerInfoCommand.java b/src/main/java/me/savvy/rixa/commands/general/ServerInfoCommand.java index 6f7913b..1faaf1b 100644 --- a/src/main/java/me/savvy/rixa/commands/general/ServerInfoCommand.java +++ b/src/main/java/me/savvy/rixa/commands/general/ServerInfoCommand.java @@ -3,6 +3,7 @@ package me.savvy.rixa.commands.general; import me.savvy.rixa.commands.handlers.Command; import me.savvy.rixa.commands.handlers.CommandExec; import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; import net.dv8tion.jda.core.EmbedBuilder; import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.User; @@ -20,7 +21,7 @@ public class ServerInfoCommand implements CommandExec { description = "Receive information about the server!", aliases = "sinfo", mainCommand = "serverinfo") public void execute(GuildMessageReceivedEvent event) { - RixaGuild rixaGuild = RixaGuild.getGuild(event.getGuild()); + RixaGuild rixaGuild = Guilds.getGuild(event.getGuild()); EmbedBuilder messageEmbed = new EmbedBuilder(); User owner = event.getGuild().getOwner().getUser(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy HH:mm:ss"); diff --git a/src/main/java/me/savvy/rixa/commands/mod/DeleteMessagesCommand.java b/src/main/java/me/savvy/rixa/commands/mod/DeleteMessagesCommand.java index 2783356..60edee1 100644 --- a/src/main/java/me/savvy/rixa/commands/mod/DeleteMessagesCommand.java +++ b/src/main/java/me/savvy/rixa/commands/mod/DeleteMessagesCommand.java @@ -5,6 +5,7 @@ import me.savvy.rixa.commands.handlers.CommandExec; import me.savvy.rixa.commands.handlers.CommandType; import me.savvy.rixa.commands.handlers.RixaPermission; import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; import me.savvy.rixa.utils.MessageBuilder; import me.savvy.rixa.utils.Utils; import net.dv8tion.jda.core.entities.ChannelType; @@ -28,7 +29,7 @@ public class DeleteMessagesCommand implements CommandExec { type = CommandType.MOD, usage = "%pclear") public void execute(GuildMessageReceivedEvent event) { - RixaGuild rixaGuild = RixaGuild.getGuild(event.getGuild()); + RixaGuild rixaGuild = Guilds.getGuild(event.getGuild()); if(!rixaGuild.hasPermission(event.getMember(), RixaPermission.CLEAR_CHAT)) { new MessageBuilder(event.getMember().getAsMention() + ", you do not have permission for this command.").setColor(event.getMember().getColor()).queue(event.getChannel()); return; diff --git a/src/main/java/me/savvy/rixa/commands/mod/MuteCommand.java b/src/main/java/me/savvy/rixa/commands/mod/MuteCommand.java index 8fbd9a9..baee4b6 100644 --- a/src/main/java/me/savvy/rixa/commands/mod/MuteCommand.java +++ b/src/main/java/me/savvy/rixa/commands/mod/MuteCommand.java @@ -5,6 +5,7 @@ import me.savvy.rixa.commands.handlers.CommandExec; import me.savvy.rixa.commands.handlers.CommandType; import me.savvy.rixa.commands.handlers.RixaPermission; import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; import me.savvy.rixa.utils.MessageBuilder; import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.Member; @@ -25,7 +26,7 @@ public class MuteCommand implements CommandExec { type = CommandType.MOD, channelType = ChannelType.TEXT) public void execute(GuildMessageReceivedEvent event) { - RixaGuild rixaGuild = RixaGuild.getGuild(event.getGuild()); + RixaGuild rixaGuild = Guilds.getGuild(event.getGuild()); if(!rixaGuild.hasPermission(event.getMember(), RixaPermission.MUTE)) { new MessageBuilder(event.getMember().getAsMention() + ", you do not have permission for this command.").setColor(event.getMember().getColor()).queue(event.getChannel()); return; diff --git a/src/main/java/me/savvy/rixa/commands/mod/PurgeMessagesCommand.java b/src/main/java/me/savvy/rixa/commands/mod/PurgeMessagesCommand.java index 783ef10..3b0e0ee 100644 --- a/src/main/java/me/savvy/rixa/commands/mod/PurgeMessagesCommand.java +++ b/src/main/java/me/savvy/rixa/commands/mod/PurgeMessagesCommand.java @@ -5,6 +5,7 @@ import me.savvy.rixa.commands.handlers.CommandExec; import me.savvy.rixa.commands.handlers.CommandType; import me.savvy.rixa.commands.handlers.RixaPermission; import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; import me.savvy.rixa.utils.MessageBuilder; import me.savvy.rixa.utils.Utils; import net.dv8tion.jda.core.entities.ChannelType; @@ -31,7 +32,7 @@ public class PurgeMessagesCommand implements CommandExec { type = CommandType.MOD, usage = "%ppurge") public void execute(GuildMessageReceivedEvent event) { - RixaGuild rixaGuild = RixaGuild.getGuild(event.getGuild()); + RixaGuild rixaGuild = Guilds.getGuild(event.getGuild()); if(!rixaGuild.hasPermission(event.getMember(), RixaPermission.CLEAR_CHAT)) { new MessageBuilder(event.getMember().getAsMention() + ", you do not have permission for this command.").setColor(event.getMember().getColor()).queue(event.getChannel()); return; diff --git a/src/main/java/me/savvy/rixa/commands/mod/RaidModeCommand.java b/src/main/java/me/savvy/rixa/commands/mod/RaidModeCommand.java index 1c7559d..adcc50f 100644 --- a/src/main/java/me/savvy/rixa/commands/mod/RaidModeCommand.java +++ b/src/main/java/me/savvy/rixa/commands/mod/RaidModeCommand.java @@ -4,6 +4,7 @@ import me.savvy.rixa.commands.handlers.Command; import me.savvy.rixa.commands.handlers.CommandExec; import me.savvy.rixa.commands.handlers.RixaPermission; import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; import me.savvy.rixa.utils.MessageBuilder; import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent; @@ -19,7 +20,7 @@ public class RaidModeCommand implements CommandExec { channelType = ChannelType.TEXT, aliases = {"raidmode", "trm", "toggleraid"}) public void execute(GuildMessageReceivedEvent event) { - RixaGuild rixaGuild = RixaGuild.getGuild(event.getGuild()); + RixaGuild rixaGuild = Guilds.getGuild(event.getGuild()); if(!rixaGuild.hasPermission(event.getMember(), RixaPermission.TOGGLE_RAIDMODE)) { new MessageBuilder(event.getMember().getAsMention() + ", you do not have permission for this command.").setColor(event.getMember().getColor()).queue(event.getChannel()); return; diff --git a/src/main/java/me/savvy/rixa/commands/owner/CheckGuildCommand.java b/src/main/java/me/savvy/rixa/commands/owner/CheckGuildCommand.java index c0ab63d..69f9695 100644 --- a/src/main/java/me/savvy/rixa/commands/owner/CheckGuildCommand.java +++ b/src/main/java/me/savvy/rixa/commands/owner/CheckGuildCommand.java @@ -4,7 +4,6 @@ import me.savvy.rixa.Rixa; import me.savvy.rixa.commands.handlers.Command; import me.savvy.rixa.commands.handlers.CommandExec; import me.savvy.rixa.commands.handlers.CommandType; -import me.savvy.rixa.enums.Result; import me.savvy.rixa.utils.MessageBuilder; import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.Member; diff --git a/src/main/java/me/savvy/rixa/data/database/sql/other/DatabaseTables.java b/src/main/java/me/savvy/rixa/data/database/sql/other/DatabaseTables.java index 7536298..3c2aab2 100644 --- a/src/main/java/me/savvy/rixa/data/database/sql/other/DatabaseTables.java +++ b/src/main/java/me/savvy/rixa/data/database/sql/other/DatabaseTables.java @@ -2,7 +2,7 @@ package me.savvy.rixa.data.database.sql.other; public enum DatabaseTables { - CORE("CREATE TABLE IF NOT EXISTS `core` ( `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `guild_id` varchar(255) NOT NULL, `guild_name` varchar(255) NOT NULL," + + CORE("CREATE TABLE IF NOT EXISTS `core` ( `guild_id` varchar(255) NOT NULL PRIMARY KEY, `guild_name` varchar(255) NOT NULL," + " `description` text, `enlisted` tinyint(1) NOT NULL DEFAULT '0', `icon` varchar(255) DEFAULT NULL, `link` varchar(255) DEFAULT NULL," + " `keywords` text, `creation_date` varchar(255) NOT NULL DEFAULT 'N/A', `guild_region` varchar(255) NOT NULL DEFAULT 'N/A'," + " `guild_owner` varchar(255) NOT NULL DEFAULT 'N/A'\n" + diff --git a/src/main/java/me/savvy/rixa/events/BotEvent.java b/src/main/java/me/savvy/rixa/events/BotEvent.java index 8b478ac..631191d 100644 --- a/src/main/java/me/savvy/rixa/events/BotEvent.java +++ b/src/main/java/me/savvy/rixa/events/BotEvent.java @@ -2,6 +2,7 @@ package me.savvy.rixa.events; import me.savvy.rixa.Rixa; import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; import net.dv8tion.jda.core.events.ReadyEvent; import net.dv8tion.jda.core.events.guild.GuildJoinEvent; import net.dv8tion.jda.core.events.guild.GuildLeaveEvent; @@ -29,6 +30,6 @@ public class BotEvent { @SubscribeEvent public void onQuit(GuildLeaveEvent event) { - RixaGuild.removeGuild(RixaGuild.getGuild(event.getGuild())); + Guilds.removeGuild(Guilds.getGuild(event.getGuild())); } } diff --git a/src/main/java/me/savvy/rixa/events/MemberEvent.java b/src/main/java/me/savvy/rixa/events/MemberEvent.java index 0972b5e..22fc604 100644 --- a/src/main/java/me/savvy/rixa/events/MemberEvent.java +++ b/src/main/java/me/savvy/rixa/events/MemberEvent.java @@ -1,8 +1,8 @@ package me.savvy.rixa.events; import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; import me.savvy.rixa.utils.MessageBuilder; -import net.dv8tion.jda.core.Permission; import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.Role; import net.dv8tion.jda.core.events.guild.member.GuildMemberJoinEvent; @@ -22,7 +22,7 @@ public class MemberEvent { @SubscribeEvent public void onMember(GuildMemberJoinEvent event) { - RixaGuild rixaGuild = RixaGuild.getGuild(event.getGuild()); + RixaGuild rixaGuild = Guilds.getGuild(event.getGuild()); /* if (rixaGuild.getGuildSettings().isRaidMode()) { if(event.getGuild().getSelfMember().hasPermission(Permission.KICK_MEMBERS)) { diff --git a/src/main/java/me/savvy/rixa/events/MessageEvent.java b/src/main/java/me/savvy/rixa/events/MessageEvent.java index 3bf5aef..03401d2 100644 --- a/src/main/java/me/savvy/rixa/events/MessageEvent.java +++ b/src/main/java/me/savvy/rixa/events/MessageEvent.java @@ -1,11 +1,12 @@ package me.savvy.rixa.events; -import com.google.code.chatterbotapi.ChatterBotFactory; import com.mysql.jdbc.StringUtils; import me.savvy.rixa.Rixa; import me.savvy.rixa.commands.handlers.CommandHandler; import me.savvy.rixa.commands.handlers.CommandRegistrar; import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; +import me.savvy.rixa.modules.levels.LevelsModule; import me.savvy.rixa.modules.reactions.handlers.ReactRegistrar; import me.savvy.rixa.modules.reactions.handlers.ReactionManager; import me.savvy.rixa.utils.MessageBuilder; @@ -34,7 +35,7 @@ import java.util.regex.Pattern; */ public class MessageEvent { - private final Pattern INVITE = Pattern.compile("discord(?:\\.gg|app.com\\/invite)\\/([A-Z0-9-]{2,16})",Pattern.CASE_INSENSITIVE); + private final Pattern INVITE = Pattern.compile("discord(?:\\.gg|app.com\\/invite)\\/([A-Z0-9-]{2,16})", Pattern.CASE_INSENSITIVE); @SubscribeEvent @@ -43,42 +44,44 @@ public class MessageEvent { if (event.getAuthor().isBot()) return; if (event.getMessage().getContent().startsWith - ("@" + event.getGuild().getSelfMember().getEffectiveName())) { + ("@" + event.getGuild().getSelfMember().getEffectiveName())) { try { String s = event.getMessage().getContent().replace - ("@" + event.getGuild().getSelfMember().getEffectiveName()+ " ", ""); + ("@" + event.getGuild().getSelfMember().getEffectiveName() + " ", ""); if (s.isEmpty()) return; s = Rixa.getChatBotSession().think(s); if (s.isEmpty()) return; event.getChannel().sendMessage(s).queue(); return; - } catch (Exception ex) { ex.printStackTrace(); } + } catch (Exception ex) { + ex.printStackTrace(); + } } - RixaGuild rixaGuild = RixaGuild.getGuild(event.getGuild()); + RixaGuild rixaGuild = Guilds.getGuild(event.getGuild()); String prefix = rixaGuild .getGuildSettings() .getPrefix(); //checkMessage(event.getMessage()); - if (!event.getMessage().getContent().startsWith(prefix)) { - if (!(rixaGuild.getLevelsModule().isEnabled())) { + if (!event.getMessage().getContent().startsWith(prefix)) { + if (!(((LevelsModule) rixaGuild.getModule("Levels")).isEnabled())) { return; } /*if(!event.getAuthor().getId().equalsIgnoreCase("202944101333729280") && !event.getAuthor().getId().equalsIgnoreCase("207322957075185665")) { return; }*/ - if(rixaGuild.getLevelsModule().getUserData(event.getAuthor().getId()).awardIfCan()) { + if (((LevelsModule) rixaGuild.getModule("Levels")).getUserData(event.getAuthor().getId()).awardIfCan()) { new MessageBuilder(event.getAuthor().getAsMention() + " has leveled up to level " + - rixaGuild.getLevelsModule().getUserData(event.getAuthor().getId()).getLevel()) + ((LevelsModule) rixaGuild.getModule("Levels")).getUserData(event.getAuthor().getId()).getLevel()) .setColor(event.getMember().getColor()).queue(event.getChannel()); } return; } String[] splitContent = event.getMessage().getContent().replace(prefix, "").split(" "); - if(!CommandHandler.hasCommand(splitContent[0])) { + if (!CommandHandler.hasCommand(splitContent[0])) { return; } CommandRegistrar cmd = CommandHandler.get(splitContent[0]); @@ -93,18 +96,19 @@ public class MessageEvent { private void checkMessage(Message message) { List invites = new ArrayList<>(); Matcher matcher = INVITE.matcher(message.getRawContent()); - while(matcher.find()) { + while (matcher.find()) { invites.add(matcher.group(1)); } - if(invites.size() == 0) { + if (invites.size() == 0) { return; } - for(String inviteCode : invites) { + for (String inviteCode : invites) { Invite invite = null; try { invite = Invite.resolve(message.getJDA(), inviteCode).complete(); - } catch(Exception e) {} - if(invite !=null && !invite.getGuild().getId().equals(message.getGuild().getId())) { + } catch (Exception e) { + } + if (invite != null && !invite.getGuild().getId().equals(message.getGuild().getId())) { new MessageBuilder(String.format("Advertising is not allowed, %s!", message.getAuthor().getAsMention())).setColor(message.getMember().getColor()).queue(message.getTextChannel()); message.delete().reason("Advertising is not allowed!").queue(); @@ -116,7 +120,7 @@ public class MessageEvent { public void onMemberJoin(PrivateMessageReceivedEvent event) { RixaGuild rixaGuild; if (MemberEvent.joinMembers.containsKey(event.getAuthor().getId())) { - rixaGuild = RixaGuild.getGuild(MemberEvent.joinMembers.get(event.getAuthor().getId())); + rixaGuild = Guilds.getGuild(MemberEvent.joinMembers.get(event.getAuthor().getId())); if (event.getMessage().getContent().equalsIgnoreCase("I agree") || event.getMessage().getContent().equalsIgnoreCase("I accept") || event.getMessage().getContent().equalsIgnoreCase("Yes")) { @@ -128,9 +132,9 @@ public class MessageEvent { new MessageBuilder(String.format("You have been promoted on %s!", rixaGuild.getGuild().getName())) .setColor(rixaGuild.getGuild().getMember(event.getAuthor()).getColor()).send(event.getAuthor()); rixaGuild.getGuildSettings().setLastJoin(System.currentTimeMillis()); - } catch(PermissionException ex) { - new MessageBuilder(String.format("I do not have permission for %s in %s", ex.getPermission().getName(), rixaGuild.getGuild().getName())) - .setColor(Color.RED).send(rixaGuild.getGuild().getOwner().getUser()); + } catch (PermissionException ex) { + new MessageBuilder(String.format("I do not have permission for %s in %s", ex.getPermission().getName(), rixaGuild.getGuild().getName())) + .setColor(Color.RED).send(rixaGuild.getGuild().getOwner().getUser()); } } } else if (event.getMessage().getContent().equalsIgnoreCase("I disagree") || @@ -142,7 +146,7 @@ public class MessageEvent { MemberEvent.joinMembers.remove(event.getAuthor().getId()); rixaGuild.getGuild().getController().kick(rixaGuild.getGuild().getMember(event.getAuthor())).complete(); } catch (PermissionException ex) { - if(ex.getPermission() == Permission.KICK_MEMBERS) { + if (ex.getPermission() == Permission.KICK_MEMBERS) { new MessageBuilder(String.format("I do not have permission to kick %s from %s", event.getAuthor().getName(), rixaGuild.getGuild().getName())) .setColor(Color.RED).send(rixaGuild.getGuild().getOwner().getUser()); } else { @@ -152,7 +156,7 @@ public class MessageEvent { } } } else { - if(!event.getAuthor().isBot()) + if (!event.getAuthor().isBot()) new MessageBuilder("Private messages are currently disabled!").setColor(Color.RED).send(event.getAuthor()); } } @@ -160,33 +164,33 @@ public class MessageEvent { @SubscribeEvent public void onReact(MessageReactionAddEvent event) { Message message = event.getChannel().getMessageById(event.getMessageId()).complete(); - if(message == null || message.getEmbeds().size() != 1) return; + if (message == null || message.getEmbeds().size() != 1) return; MessageEmbed embed = message.getEmbeds().get(0); - if(StringUtils.isNullOrEmpty(embed.getTitle())) return; + if (StringUtils.isNullOrEmpty(embed.getTitle())) return; String[] titleSplit = embed.getTitle().split(": "); if (titleSplit[0].equalsIgnoreCase("Leaderboard")) return; - if(!ReactionManager.getReactions().containsKey(titleSplit[0])) return; + if (!ReactionManager.getReactions().containsKey(titleSplit[0])) return; - ReactRegistrar reactRegistrar = ReactionManager.getReactions().get(titleSplit[0]); - Method m = reactRegistrar.getMethod(); - try { - m.invoke(reactRegistrar.getExecutor(), event); - } catch (Exception e) { - e.printStackTrace(); - } + ReactRegistrar reactRegistrar = ReactionManager.getReactions().get(titleSplit[0]); + Method m = reactRegistrar.getMethod(); + try { + m.invoke(reactRegistrar.getExecutor(), event); + } catch (Exception e) { + e.printStackTrace(); } + } @SubscribeEvent public void onGuildReact(GuildMessageReactionAddEvent event) { if (event.getGuild() == null) return; if (event.getUser().isBot()) return; Message message = event.getChannel().getMessageById(event.getMessageId()).complete(); - if(message == null || message.getEmbeds().size() != 1) return; + if (message == null || message.getEmbeds().size() != 1) return; MessageEmbed embed = message.getEmbeds().get(0); - if(StringUtils.isNullOrEmpty(embed.getTitle())) return; + if (StringUtils.isNullOrEmpty(embed.getTitle())) return; String[] titleSplit = embed.getTitle().split(": "); System.out.println(Arrays.toString(titleSplit)); - if(ReactionManager.getReactions().containsKey(titleSplit[0])) { + if (ReactionManager.getReactions().containsKey(titleSplit[0])) { ReactRegistrar reactRegistrar = ReactionManager.getReactions().get(titleSplit[0]); Method m = reactRegistrar.getMethod(); try { diff --git a/src/main/java/me/savvy/rixa/events/Shutdown.java b/src/main/java/me/savvy/rixa/events/Shutdown.java new file mode 100644 index 0000000..3b7c5dc --- /dev/null +++ b/src/main/java/me/savvy/rixa/events/Shutdown.java @@ -0,0 +1,22 @@ +package me.savvy.rixa.events; + +import me.savvy.rixa.Rixa; +import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; +import net.dv8tion.jda.core.events.ReadyEvent; +import net.dv8tion.jda.core.events.ShutdownEvent; +import net.dv8tion.jda.core.events.guild.GuildJoinEvent; +import net.dv8tion.jda.core.events.guild.GuildLeaveEvent; +import net.dv8tion.jda.core.hooks.SubscribeEvent; + +/** + * Created by Timber on 5/23/2017. + */ +public class Shutdown { + + @SubscribeEvent + public void onShutdown(ShutdownEvent event) { + System.out.println("Test"); + Rixa.getInstance().close(); + } +} diff --git a/src/main/java/me/savvy/rixa/extras/polls/Poll.java b/src/main/java/me/savvy/rixa/extras/polls/Poll.java index c5cb406..3bae54f 100644 --- a/src/main/java/me/savvy/rixa/extras/polls/Poll.java +++ b/src/main/java/me/savvy/rixa/extras/polls/Poll.java @@ -6,7 +6,6 @@ import me.savvy.rixa.utils.MessageBuilder; import net.dv8tion.jda.core.EmbedBuilder; import java.awt.*; -import java.util.ArrayList; import java.util.LinkedList; import java.util.List; diff --git a/src/main/java/me/savvy/rixa/guild/RixaGuild.java b/src/main/java/me/savvy/rixa/guild/RixaGuild.java index c6e759d..1db53fb 100644 --- a/src/main/java/me/savvy/rixa/guild/RixaGuild.java +++ b/src/main/java/me/savvy/rixa/guild/RixaGuild.java @@ -9,9 +9,10 @@ import me.savvy.rixa.Rixa; import me.savvy.rixa.commands.handlers.RixaPermission; import me.savvy.rixa.enums.Result; import me.savvy.rixa.guild.management.GuildSettings; +import me.savvy.rixa.guild.management.Guilds; +import me.savvy.rixa.modules.RixaModule; import me.savvy.rixa.modules.levels.LevelsModule; import me.savvy.rixa.modules.music.MusicModule; -import me.savvy.rixa.modules.twitter.TwitterModule; import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.Member; import net.dv8tion.jda.core.entities.Role; @@ -19,7 +20,10 @@ import net.dv8tion.jda.core.entities.User; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Optional; /** * Created by Timber on 5/23/2017. @@ -33,26 +37,20 @@ public class RixaGuild { @Setter private GuildSettings guildSettings; @Getter - @Setter - private MusicModule musicModule; - @Getter - @Setter - private TwitterModule twitterModule; - @Getter private List mutedMembers = new ArrayList<>(); @Getter - @Setter - private LevelsModule levelsModule; + private HashMap modules; public RixaGuild(Guild guild) { this.guild = guild; + this.modules = new HashMap<>(); this.db = Rixa.getDatabase(); - setMusicModule(new MusicModule(guild)); - setLevelsModule(new LevelsModule(this)); + modules.put("Music", new MusicModule()); + modules.put("Levels", new LevelsModule()); load(); } - private void load() { + public void load() { if (!(checkExists())) { Update update = new Update("INSERT INTO `core` (`guild_id`, `guild_name`, `description`, `keywords`) VALUES (?, ?, 'Description not set.', 'No Keywords Found.')"); update.setString(guild.getId()); @@ -60,7 +58,7 @@ public class RixaGuild { db.send(update); } setGuildSettings(new GuildSettings(this.guild)); - addGuild(this); + Guilds.addGuild(this); } public GuildSettings getGuildSettings() { @@ -177,28 +175,16 @@ public class RixaGuild { mutedMembers.add(user.getId()); } - @Getter - private static Map guilds = new HashMap<>(); - - private static void addGuild(RixaGuild guild) { - if (check(guild.getGuild())) return; - guilds.put(guild.getGuild().getId(), guild); - } - - public static RixaGuild getGuild(Guild guild) { - if (!check(guild)) { - addGuild(new RixaGuild(guild)); + public void save() { + for (RixaModule module : modules.values()) { + if (!module.isEnabled()) { + return; + } + module.save(); } - return guilds.get(guild.getId()); } - public static void removeGuild(RixaGuild guild) { - if (!check(guild.getGuild())) return; - guilds.remove(guild.getGuild().getId()); + public RixaModule getModule(String levels) { + return this.modules.get(levels); } - - private static boolean check(Guild guild) { - return guilds.containsKey(guild.getId()); - } - } diff --git a/src/main/java/me/savvy/rixa/guild/management/GuildSettings.java b/src/main/java/me/savvy/rixa/guild/management/GuildSettings.java index b49fb3a..00c1046 100644 --- a/src/main/java/me/savvy/rixa/guild/management/GuildSettings.java +++ b/src/main/java/me/savvy/rixa/guild/management/GuildSettings.java @@ -25,7 +25,7 @@ public class GuildSettings { @Getter private boolean enlisted, joinVerification; @Getter - private String prefix = "/", defaultRole, muteRole, joinMessage, quitMessage, joinPrivateMessage, description; + private String prefix = "/", defaultRole, muteRole, joinMessage, quitMessage, joinPrivateMessage, description, currency; @Getter private TextChannel joinMessageChannel, quitMessageChannel; @Getter @@ -48,9 +48,9 @@ public class GuildSettings { private void load() throws SQLException { if (!checkExists()) { Update update = new Update("INSERT INTO `settings` (`guild_id`, `log_enabled`, `log_channel`, `joinMessage`, `quitMessage`, `greetings`, `farewell`," + - " `prefix`, `joinPm`, `joinVerification`, `defaultRole`, `muteRole`)" + + " `prefix`, `joinPm`, `joinVerification`, `defaultRole`, `muteRole`, `currency`)" + " VALUES ('" + guild.getId() + "', '0', 'default_value', 'default_value', 'default_value', 'default_value', 'default_value', '/'," + - " 'default', '0', 'default_value', 'default_value');"); + " 'default', '0', 'default_value', 'default_value', ' tokens');"); Rixa.getDatabase().send(update); return; } diff --git a/src/main/java/me/savvy/rixa/guild/management/Guilds.java b/src/main/java/me/savvy/rixa/guild/management/Guilds.java new file mode 100644 index 0000000..93899e1 --- /dev/null +++ b/src/main/java/me/savvy/rixa/guild/management/Guilds.java @@ -0,0 +1,35 @@ +package me.savvy.rixa.guild.management; + +import lombok.Getter; +import me.savvy.rixa.guild.RixaGuild; +import net.dv8tion.jda.core.entities.Guild; + +import java.util.HashMap; +import java.util.Map; + +public class Guilds { + + @Getter + private static Map guilds = new HashMap<>(); + + public static void addGuild(RixaGuild guild) { + if (check(guild.getGuild())) return; + guilds.put(guild.getGuild().getId(), guild); + } + + public static RixaGuild getGuild(Guild guild) { + if (!check(guild)) { + addGuild(new RixaGuild(guild)); + } + return guilds.get(guild.getId()); + } + + public static void removeGuild(RixaGuild guild) { + if (!check(guild.getGuild())) return; + guilds.remove(guild.getGuild().getId()); + } + + public static boolean check(Guild guild) { + return guilds.containsKey(guild.getId()); + } +} diff --git a/src/main/java/me/savvy/rixa/guild/user/UserData.java b/src/main/java/me/savvy/rixa/guild/user/UserData.java index 4c43bd5..ba78253 100644 --- a/src/main/java/me/savvy/rixa/guild/user/UserData.java +++ b/src/main/java/me/savvy/rixa/guild/user/UserData.java @@ -5,7 +5,8 @@ import me.majrly.database.statements.Query; import me.majrly.database.statements.Update; import me.savvy.rixa.Rixa; import me.savvy.rixa.enums.Result; -import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; +import me.savvy.rixa.modules.levels.LevelsModule; import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.User; @@ -39,7 +40,7 @@ public class UserData { } private void register(UserData userData) { - RixaGuild.getGuild(guild).getLevelsModule().registerUser(userData); + ((LevelsModule) Guilds.getGuild(guild).getModule("Levels")).registerUser(userData); } private void load() { diff --git a/src/main/java/me/savvy/rixa/modules/RixaModule.java b/src/main/java/me/savvy/rixa/modules/RixaModule.java index 63cbf43..1e0074d 100644 --- a/src/main/java/me/savvy/rixa/modules/RixaModule.java +++ b/src/main/java/me/savvy/rixa/modules/RixaModule.java @@ -1,5 +1,7 @@ package me.savvy.rixa.modules; +import me.savvy.rixa.guild.RixaGuild; + /** * Created by Timber on 5/23/2017. */ @@ -10,4 +12,8 @@ public interface RixaModule { String getDescription(); boolean isEnabled(); + + void load(RixaGuild guild); + + void save(); } diff --git a/src/main/java/me/savvy/rixa/modules/economy/Economy.java b/src/main/java/me/savvy/rixa/modules/economy/Economy.java index ede68c6..b893f99 100644 --- a/src/main/java/me/savvy/rixa/modules/economy/Economy.java +++ b/src/main/java/me/savvy/rixa/modules/economy/Economy.java @@ -1,4 +1,49 @@ package me.savvy.rixa.modules.economy; -public class Economy { -} +import lombok.Getter; +import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.modules.RixaModule; +import me.savvy.rixa.utils.DatabaseUtils; + +import java.util.HashMap; +import java.util.Map; + +public class Economy implements RixaModule { + + @Getter + private RixaGuild rixaGuild; + @Getter + private Map userData = new HashMap<>(); + + private boolean enabled; + + @Override + public String getName() { + return "Economy"; + } + + @Override + public String getDescription() { + return "Rixa Economy Module"; + } + + @Override + public boolean isEnabled() { + return enabled; + } + + @Override + public void load(RixaGuild rixaGuild) { + this.rixaGuild = rixaGuild; + } + + @Override + public void save() { + + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + DatabaseUtils.update("modules", "levels", "guild_id", enabled, rixaGuild.getGuild().getId()); + } +} \ No newline at end of file diff --git a/src/main/java/me/savvy/rixa/modules/economy/EconomyData.java b/src/main/java/me/savvy/rixa/modules/economy/EconomyData.java new file mode 100644 index 0000000..1079b08 --- /dev/null +++ b/src/main/java/me/savvy/rixa/modules/economy/EconomyData.java @@ -0,0 +1,25 @@ +package me.savvy.rixa.modules.economy; + +import lombok.Getter; +import lombok.Setter; +import net.dv8tion.jda.core.entities.Guild; +import net.dv8tion.jda.core.entities.User; + +public class EconomyData { + + @Getter + @Setter + private int amount; + @Getter + @Setter + private Guild guild; + @Getter + @Setter + private User user; + + + public EconomyData(User user, Guild guild) { + setUser(user); + setGuild(guild); + } +} diff --git a/src/main/java/me/savvy/rixa/modules/levels/LevelsModule.java b/src/main/java/me/savvy/rixa/modules/levels/LevelsModule.java index 534e247..d104deb 100644 --- a/src/main/java/me/savvy/rixa/modules/levels/LevelsModule.java +++ b/src/main/java/me/savvy/rixa/modules/levels/LevelsModule.java @@ -1,11 +1,11 @@ package me.savvy.rixa.modules.levels; import lombok.Getter; +import lombok.Setter; import me.majrly.database.Database; import me.majrly.database.statements.Query; import me.majrly.database.statements.Update; import me.savvy.rixa.Rixa; -import me.savvy.rixa.enums.Result; import me.savvy.rixa.guild.RixaGuild; import me.savvy.rixa.guild.user.UserData; import me.savvy.rixa.modules.RixaModule; @@ -13,7 +13,6 @@ import me.savvy.rixa.utils.DatabaseUtils; import me.savvy.rixa.utils.MessageBuilder; import net.dv8tion.jda.core.entities.Member; -import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; @@ -24,18 +23,13 @@ import java.util.*; public class LevelsModule implements RixaModule { @Getter - private final RixaGuild rixaGuild; + private RixaGuild rixaGuild; @Getter private Map userData = new HashMap<>(); @Getter + @Setter private boolean enabled; - public LevelsModule(RixaGuild rixaGuild) { - this.rixaGuild = rixaGuild; - enabled = true; - load(); - } - private List leaderboard(Member member) { Database db = Rixa.getDatabase(); ResultSet rs = null; @@ -50,7 +44,7 @@ public class LevelsModule implements RixaModule { try { while (rs != null && rs.next()) { if (member.getGuild().getMemberById(rs.getString("user_id")) == null) continue; - UserData userData = rixaGuild.getLevelsModule().getUserData(rs.getString("user_id")); + UserData userData = ((LevelsModule) rixaGuild.getModule("Levels")).getUserData(rs.getString("user_id")); userDataList.add(userData); } rs.getStatement().close(); @@ -104,6 +98,35 @@ public class LevelsModule implements RixaModule { return "Rixa levels module."; } + @Override + public void load(RixaGuild rixaGuild) { + try { + this.rixaGuild = rixaGuild; + Query query = new Query("SELECT * FROM `modules` WHERE `guild_id`=?;"); + query.setString(rixaGuild.getGuild().getId()); + Optional o = Rixa.getDatabase().send(query); + if (!o.isPresent()) return; + else if (!(o.get() instanceof ResultSet)) return; + ResultSet set = (ResultSet) o.get(); + if (set.next()) { + setEnabled(set.getBoolean("levels")); + } else { + Update update = new Update("INSERT INTO `modules` (`guild_id`) VALUES (?);"); + update.setString(rixaGuild.getGuild().getId()); + Rixa.getDatabase().send(update); + setEnabled(true); + } + set.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @Override + public void save() { + DatabaseUtils.update("modules", "levels", "guild_id", enabled, rixaGuild.getGuild().getId()); + } + public void registerUser(UserData userData) { if (getUserData().containsKey(userData.getUser().getId())) { return; @@ -124,62 +147,4 @@ public class LevelsModule implements RixaModule { (getRixaGuild().getGuild().getJDA().getUserById(key), getRixaGuild().getGuild()); } - - private void load() { - if (!(checkExists())) { - this.enabled = true; - insert(); - } - String query = "SELECT `levels` FROM `modules` WHERE `guild_id` = ?;"; - PreparedStatement ps; - ResultSet rs; - try { - ps = Rixa.getDatabase().getConnection().get().prepareStatement(query); - ps.setString(1, getRixaGuild().getGuild().getId()); - rs = ps.executeQuery(); - if (rs.next()) { - this.enabled = rs.getBoolean("levels"); - } - ps.close(); - rs.close(); - } catch (SQLException e) { - e.printStackTrace(); - } - } - - private boolean checkExists() { - Result r = Result.FALSE; - try { - Query query = new Query("SELECT `guild_id` FROM `modules` WHERE `guild_id` = ?;"); - query.setString(rixaGuild.getGuild().getId()); - Optional optional = Rixa.getDatabase().send(query); - if (!optional.isPresent()) r = Result.ERROR; - if (!(optional.get() instanceof ResultSet)) r = Result.ERROR; - ResultSet set = (ResultSet) optional.get(); - if (r != Result.ERROR) { - if (set.next()) { - r = Result.TRUE; - } else { - r = Result.FALSE; - } - } - set.close(); - return r == Result.TRUE; - } catch (SQLException e) { - e.printStackTrace(); - return false; - } - } - - private void insert() { - String query = "INSERT INTO `modules` (`guild_id`) VALUES (?);"; - Update update = new Update(query); - update.setString(rixaGuild.getGuild().getId()); - Rixa.getDatabase().send(update); - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - DatabaseUtils.update("modules", "levels", "guild_id", enabled, rixaGuild.getGuild().getId()); - } -} +} \ No newline at end of file diff --git a/src/main/java/me/savvy/rixa/modules/music/AudioPlayerSendHandler.java b/src/main/java/me/savvy/rixa/modules/music/AudioPlayerSendHandler.java index 515af4e..a3eba41 100644 --- a/src/main/java/me/savvy/rixa/modules/music/AudioPlayerSendHandler.java +++ b/src/main/java/me/savvy/rixa/modules/music/AudioPlayerSendHandler.java @@ -1,8 +1,8 @@ package me.savvy.rixa.modules.music; +import com.sedmelluq.discord.lavaplayer.player.AudioPlayer; import com.sedmelluq.discord.lavaplayer.track.playback.AudioFrame; import net.dv8tion.jda.core.audio.AudioSendHandler; -import com.sedmelluq.discord.lavaplayer.player.AudioPlayer; /** * Created by Timber on 3/13/2017. diff --git a/src/main/java/me/savvy/rixa/modules/music/MusicModule.java b/src/main/java/me/savvy/rixa/modules/music/MusicModule.java index ddda698..3828c4c 100644 --- a/src/main/java/me/savvy/rixa/modules/music/MusicModule.java +++ b/src/main/java/me/savvy/rixa/modules/music/MusicModule.java @@ -1,16 +1,16 @@ package me.savvy.rixa.modules.music; import lombok.Getter; +import lombok.Setter; import me.majrly.database.Database; import me.majrly.database.statements.Query; import me.majrly.database.statements.Update; import me.savvy.rixa.Rixa; -import me.savvy.rixa.enums.Result; +import me.savvy.rixa.guild.RixaGuild; import me.savvy.rixa.modules.RixaModule; import me.savvy.rixa.utils.DatabaseUtils; import net.dv8tion.jda.core.entities.Guild; -import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Optional; @@ -19,31 +19,28 @@ import java.util.Optional; * Created by Timber on 5/23/2017. */ public class MusicModule implements RixaModule { + private Database db; @Getter + @Setter private boolean enabled; @Getter + @Setter private String musicRole; @Getter private Guild guild; - public MusicModule(Guild guild) { - this.guild = guild; - this.enabled = false; - this.musicRole = "default_value"; - db = Rixa.getDatabase(); - load(); - } - - public void load() { - Update music = new Update("CREATE TABLE IF NOT EXISTS `music` (`guild_id` varchar(255) NOT NULL, `music_role` varchar(255) NOT NULL, `enabled` INT(11) NOT NULL, PRIMARY KEY (`guild_id`));"); - db.send(music); - if (!checkExists()) { - Update update = new Update("INSERT INTO `music` (`guild_id`, `music_role`, `enabled`)" + - " VALUES ('" + guild.getId() + "', 'default_value', '0');"); - db.send(update); - } + @Override + public void load(RixaGuild rixaGuild) { try { + this.guild = rixaGuild.getGuild(); + this.enabled = false; + this.musicRole = "default_value"; + db = Rixa.getDatabase(); + if (!DatabaseUtils.checkExists("music", guild)) { + Update update = new Update("INSERT INTO `music` (`guild_id`, `music_role`, `enabled`) VALUES ('" + guild.getId() + "', 'default_value', '0');"); + db.send(update); + } Query query = new Query("SELECT * FROM `modules` WHERE `guild_id` = ?"); query.setString(guild.getId()); Optional optional = Rixa.getDatabase().send(query); @@ -51,8 +48,8 @@ public class MusicModule implements RixaModule { if (!(optional.get() instanceof ResultSet)) return; ResultSet set = (ResultSet) optional.get(); if (set.next()) { - this.musicRole = set.getString("music_role"); - this.enabled = set.getBoolean("enabled"); + setMusicRole(set.getString("music_role")); + setEnabled(set.getBoolean("enabled")); } set.close(); } catch (SQLException e) { @@ -60,6 +57,12 @@ public class MusicModule implements RixaModule { } } + @Override + public void save() { + DatabaseUtils.update("music", "enabled", "guild_id", enabled, guild.getId()); + DatabaseUtils.update("music", "music_role", "guild_id", musicRole, guild.getId()); + } + @Override public String getName() { return "Music"; @@ -70,43 +73,7 @@ public class MusicModule implements RixaModule { return "Listen to music in your voice channel."; } - - public Result setEnabled(boolean val) { - this.enabled = val; - return DatabaseUtils.update("music", "enabled", "guild_id", val, guild.getId()); - } - public boolean isRoleRequired() { - return (!musicRole.equalsIgnoreCase("default_value")); - } - - - public Result setRole(String newRole) { - this.musicRole = newRole; - return DatabaseUtils.update("music", "music_role", "guild_id", newRole, guild.getId()); - } - - public boolean checkExists() { - Result r = Result.FALSE; - try { - Query query = new Query("SELECT `guild_id` FROM `music` WHERE `guild_id` = '" + - guild.getId() + "';"); - Optional optional = Rixa.getDatabase().send(query); - if (!optional.isPresent()) r = Result.ERROR; - if (!(optional.get() instanceof ResultSet)) r = Result.ERROR; - ResultSet set = (ResultSet) optional.get(); - if (r != Result.ERROR) { - if (set.next()) { - r = Result.TRUE; - } else { - r = Result.FALSE; - } - } - set.close(); - return r == Result.TRUE; - } catch (SQLException e) { - e.printStackTrace(); - return false; - } + return (musicRole != null && !musicRole.equalsIgnoreCase("default_value")); } } diff --git a/src/main/java/me/savvy/rixa/modules/reactions/react/ConfigReaction.java b/src/main/java/me/savvy/rixa/modules/reactions/react/ConfigReaction.java index b23110b..f63e297 100644 --- a/src/main/java/me/savvy/rixa/modules/reactions/react/ConfigReaction.java +++ b/src/main/java/me/savvy/rixa/modules/reactions/react/ConfigReaction.java @@ -2,6 +2,7 @@ package me.savvy.rixa.modules.reactions.react; import me.savvy.rixa.commands.admin.ConfigCommand; import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; import me.savvy.rixa.modules.reactions.handlers.React; import me.savvy.rixa.modules.reactions.handlers.ReactHandle; import me.savvy.rixa.utils.MessageBuilder; @@ -30,7 +31,7 @@ public class ConfigReaction implements React { if(guild == null) { return; } - RixaGuild rixaGuild = RixaGuild.getGuild(guild); + RixaGuild rixaGuild = Guilds.getGuild(guild); String prefix = rixaGuild.getGuildSettings().getPrefix(); MessageBuilder builder = null; int page = 500; diff --git a/src/main/java/me/savvy/rixa/modules/reactions/react/HelpReaction.java b/src/main/java/me/savvy/rixa/modules/reactions/react/HelpReaction.java index 504ef1a..588c537 100644 --- a/src/main/java/me/savvy/rixa/modules/reactions/react/HelpReaction.java +++ b/src/main/java/me/savvy/rixa/modules/reactions/react/HelpReaction.java @@ -3,7 +3,7 @@ package me.savvy.rixa.modules.reactions.react; import me.savvy.rixa.commands.handlers.CommandHandler; import me.savvy.rixa.commands.handlers.CommandType; import me.savvy.rixa.guild.RixaGuild; -import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; import me.savvy.rixa.modules.reactions.handlers.React; import me.savvy.rixa.modules.reactions.handlers.ReactHandle; import net.dv8tion.jda.core.EmbedBuilder; @@ -26,7 +26,7 @@ public class HelpReaction implements React { } Message message = event.getChannel().getMessageById(event.getMessageId()).complete(); String title = message.getEmbeds().get(0).getTitle().split(": ")[1]; - RixaGuild rixaGuild = RixaGuild.getGuild(event.getJDA().getGuildById(title)); + RixaGuild rixaGuild = Guilds.getGuild(event.getJDA().getGuildById(title)); String prefix = rixaGuild.getGuildSettings().getPrefix(); EmbedBuilder embedBuilder; try { diff --git a/src/main/java/me/savvy/rixa/modules/reactions/react/LeaderboardReaction.java b/src/main/java/me/savvy/rixa/modules/reactions/react/LeaderboardReaction.java index 3e85c6c..a0bcf5a 100644 --- a/src/main/java/me/savvy/rixa/modules/reactions/react/LeaderboardReaction.java +++ b/src/main/java/me/savvy/rixa/modules/reactions/react/LeaderboardReaction.java @@ -1,13 +1,12 @@ package me.savvy.rixa.modules.reactions.react; import me.savvy.rixa.guild.RixaGuild; +import me.savvy.rixa.guild.management.Guilds; +import me.savvy.rixa.modules.levels.LevelsModule; import me.savvy.rixa.modules.reactions.handlers.React; import me.savvy.rixa.modules.reactions.handlers.ReactHandle; -import net.dv8tion.jda.core.MessageBuilder; -import net.dv8tion.jda.core.entities.ChannelType; import net.dv8tion.jda.core.entities.Guild; import net.dv8tion.jda.core.entities.Message; -import net.dv8tion.jda.core.entities.MessageEmbed; import net.dv8tion.jda.core.events.message.guild.react.GuildMessageReactionAddEvent; import net.dv8tion.jda.core.events.message.react.MessageReactionAddEvent; @@ -27,7 +26,7 @@ public class LeaderboardReaction implements React { if(guild == null) { return; } - RixaGuild rixaGuild = RixaGuild.getGuild(guild); + RixaGuild rixaGuild = Guilds.getGuild(guild); int page = 500; switch (event.getReaction().getEmote().getName()) { case "\u2B05":// previous @@ -38,7 +37,7 @@ public class LeaderboardReaction implements React { break; } if(page != 500) { - me.savvy.rixa.utils.MessageBuilder builder = rixaGuild.getLevelsModule().leaderboard + me.savvy.rixa.utils.MessageBuilder builder = ((LevelsModule) rixaGuild.getModule("Levels")).leaderboard (event.getMember(), page); if (builder == null) return; message.editMessage(builder.getBuilder().build()).queue(); diff --git a/src/main/java/me/savvy/rixa/modules/twitter/TwitterModule.java b/src/main/java/me/savvy/rixa/modules/twitter/TwitterModule.java index cfed97f..2814907 100644 --- a/src/main/java/me/savvy/rixa/modules/twitter/TwitterModule.java +++ b/src/main/java/me/savvy/rixa/modules/twitter/TwitterModule.java @@ -4,7 +4,10 @@ import lombok.Getter; import lombok.Setter; import me.savvy.rixa.guild.RixaGuild; import me.savvy.rixa.modules.RixaModule; -import twitter4j.*; +import twitter4j.Twitter; +import twitter4j.TwitterFactory; +import twitter4j.TwitterStream; +import twitter4j.TwitterStreamFactory; import twitter4j.conf.ConfigurationBuilder; /** @@ -57,4 +60,10 @@ public class TwitterModule implements RixaModule { public String getDescription() { return "Twitter feed, tweet & more."; } -} + + @Override + public void load(RixaGuild guild) { } + + @Override + public void save() { } +} \ No newline at end of file diff --git a/src/main/java/me/savvy/rixa/utils/DatabaseUtils.java b/src/main/java/me/savvy/rixa/utils/DatabaseUtils.java index 7376702..aababce 100644 --- a/src/main/java/me/savvy/rixa/utils/DatabaseUtils.java +++ b/src/main/java/me/savvy/rixa/utils/DatabaseUtils.java @@ -1,8 +1,14 @@ package me.savvy.rixa.utils; +import me.majrly.database.statements.Query; import me.majrly.database.statements.Update; import me.savvy.rixa.Rixa; import me.savvy.rixa.enums.Result; +import net.dv8tion.jda.core.entities.Guild; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Optional; public class DatabaseUtils { @@ -13,4 +19,27 @@ public class DatabaseUtils { Rixa.getDatabase().send(update); return Result.TRUE; } + + public static boolean checkExists(String table, Guild guild) { + Result r = Result.FALSE; + try { + Query query = new Query("SELECT `guild_id` FROM `" + table + "` WHERE `guild_id` = '" + guild.getId() + "';"); + Optional optional = Rixa.getDatabase().send(query); + if (!optional.isPresent()) r = Result.ERROR; + if (!(optional.get() instanceof ResultSet)) r = Result.ERROR; + ResultSet set = (ResultSet) optional.get(); + if (r != Result.ERROR) { + if (set.next()) { + r = Result.TRUE; + } else { + r = Result.FALSE; + } + } + set.close(); + return r == Result.TRUE; + } catch (SQLException e) { + System.out.println("INFO: Failed to check if exists : " + e.getLocalizedMessage()); + return false; + } + } } \ No newline at end of file diff --git a/src/main/java/me/savvy/rixa/utils/MessageBuilder.java b/src/main/java/me/savvy/rixa/utils/MessageBuilder.java index 16d081b..efc5bc1 100644 --- a/src/main/java/me/savvy/rixa/utils/MessageBuilder.java +++ b/src/main/java/me/savvy/rixa/utils/MessageBuilder.java @@ -2,7 +2,10 @@ package me.savvy.rixa.utils; import lombok.Getter; import net.dv8tion.jda.core.EmbedBuilder; -import net.dv8tion.jda.core.entities.*; +import net.dv8tion.jda.core.entities.Message; +import net.dv8tion.jda.core.entities.MessageEmbed; +import net.dv8tion.jda.core.entities.TextChannel; +import net.dv8tion.jda.core.entities.User; import java.awt.*;