From 2b90078aac4d14f4bf3a89b579b107bef655d6ff Mon Sep 17 00:00:00 2001 From: Matthew Cech Date: Wed, 8 May 2019 20:20:02 -0700 Subject: [PATCH] + Added superintendent --- src/dataStructures/KittyTrackedLong.java | 1 - src/main/Main.java | 88 ++-------------------- src/main/Superintendent.java | 95 ++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 82 deletions(-) create mode 100644 src/main/Superintendent.java diff --git a/src/dataStructures/KittyTrackedLong.java b/src/dataStructures/KittyTrackedLong.java index 98f450e..588d64f 100644 --- a/src/dataStructures/KittyTrackedLong.java +++ b/src/dataStructures/KittyTrackedLong.java @@ -1,6 +1,5 @@ package dataStructures; -import core.DatabaseManager; import core.DatabaseTrackedObject; import utils.GlobalLog; import utils.LogFilter; diff --git a/src/main/Main.java b/src/main/Main.java index d18a13d..6ef60c9 100644 --- a/src/main/Main.java +++ b/src/main/Main.java @@ -26,7 +26,7 @@ import net.dv8tion.jda.core.*; public class Main extends ListenerAdapter { // Variables and bot specific objects - private static JDA kitty; + private static JDA kitty; // TODO: Wrap private static DatabaseManager databaseManager; private static CommandEnabler commandEnabler; private static CommandManager commandManager; @@ -34,7 +34,7 @@ public class Main extends ListenerAdapter private static RPManager rpManager; private static PluginManager pluginManager; - // Main test location + // Initialization and setup public static void main(String[] args) throws InterruptedException, LoginException, Exception { // Factory startup. The ordering is intentional. @@ -52,11 +52,12 @@ public class Main extends ListenerAdapter kitty.addEventListener(new Main()); } + // When a message is sent in a server that kitty is in, this is what's called. @Override public void onGuildMessageReceived(GuildMessageReceivedEvent event) { // Tweak the event as necessary - if(!PreProcessSetup(event)) + if(!Superintendent.PreProcessSetup(event, stats)) return; // Factory objects @@ -69,7 +70,7 @@ public class Main extends ListenerAdapter UserInput input = new UserInput(event, guild); // Tweak object construction as necessary - if(!PostProcessSetup(event, user, guild, channel, response, input)) + if(!Superintendent.PostProcessSetup(event, user, guild, channel, response, input)) return; // Track beans! @@ -79,7 +80,7 @@ public class Main extends ListenerAdapter RPManager.instance.addLine(channel, user, input); // Run any pre-emptive upkeep we need to - PerCommandUpkeepPre(); + Superintendent.PerCommandUpkeepPre(); // Attempt to spin up a command. If the command doesn't exist. // Run plugins right before invoking the commands but after all other setup. @@ -92,81 +93,6 @@ public class Main extends ListenerAdapter } // Run any upkeep in post we need to - PerCommandUpkeepPost(); - } - - // This is run on the JDA GuildMessageReceivedEvent before anything else happens. - public static boolean PreProcessSetup(GuildMessageReceivedEvent event) - { - // Verify we have an event - if(event == null) - return false; - - // Track number of messages seen - stats.NoteMessageEvent(); - - // If we're mid-shutdown, no more commands. - if(stats.GetIsShuttingDown()) - return false; - - // Swat down highest ban level immediately before even hitting command parsing. - for(int i = 0; i < Ref.alwaysIgnore.length; ++i) - { - String id = event.getAuthor().getId(); - if(id.equals(Ref.alwaysIgnore[i])) - return false; - } - - return true; - } - - // This runs after all the objects have been constructed! - // Modifications to objects here stick! - public static boolean PostProcessSetup(GuildMessageReceivedEvent event, KittyUser user, KittyGuild guild, KittyChannel channel, Response res, UserInput input) - { - // Verify we have everything. From here out, we promise we have that all. - if(event == null || user == null || guild == null || channel == null || res == null || input == null) - return false; - - // If the guild member is the owner, give them admin role by default. - if(event.getMember().isOwner()) - user.ChangeRole(KittyRole.Admin); - - // Give devs the dev role - for(int i = 0; i < Ref.devIDs.length; ++i) - { - if(event.getAuthor().getId().equals(Ref.devIDs[i])) - { - user.ChangeRole(KittyRole.Dev); - break; - } - } - - return true; - } - - // This is for stuff that we need to do on a regular basis, but don't - // necessarily want running at all points in time. - // Happens just after the command / plugin runs. - private static boolean PerCommandUpkeepPost() - { - // Upkeep database - databaseManager.Upkeep(); - - // Update command-specific - RPManager.Upkeep(kitty); - - return true; - } - - // Only called once per command. Good for lazily updating. - // Happens just before the command / plugin runs. - private static boolean PerCommandUpkeepPre() - { - // Upkeep localization system's file monitoring - LocStrings.Upkeep(); - LocCommands.Upkeep(); - - return true; + Superintendent.PerCommandUpkeepPost(kitty, databaseManager); } } \ No newline at end of file diff --git a/src/main/Superintendent.java b/src/main/Superintendent.java new file mode 100644 index 0000000..84da5fc --- /dev/null +++ b/src/main/Superintendent.java @@ -0,0 +1,95 @@ +package main; + +import core.DatabaseManager; +import core.LocCommands; +import core.LocStrings; +import core.RPManager; +import core.Stats; +import dataStructures.KittyChannel; +import dataStructures.KittyGuild; +import dataStructures.KittyRole; +import dataStructures.KittyUser; +import dataStructures.Response; +import dataStructures.UserInput; +import net.dv8tion.jda.core.JDA; +import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent; +import offline.Ref; + +// Manages the fiddly bits and upkeep of the bot - while not +public class Superintendent +{ + // This is run on the JDA GuildMessageReceivedEvent before anything else happens. + public static boolean PreProcessSetup(GuildMessageReceivedEvent event, Stats stats) + { + // Verify we have an event + if(event == null) + return false; + + // Track number of messages seen + stats.NoteMessageEvent(); + + // If we're mid-shutdown, no more commands. + if(stats.GetIsShuttingDown()) + return false; + + // Swat down highest ban level immediately before even hitting command parsing. + for(int i = 0; i < Ref.alwaysIgnore.length; ++i) + { + String id = event.getAuthor().getId(); + if(id.equals(Ref.alwaysIgnore[i])) + return false; + } + + return true; + } + + // This runs after all the objects have been constructed! + // Modifications to objects here stick! + public static boolean PostProcessSetup(GuildMessageReceivedEvent event, KittyUser user, KittyGuild guild, KittyChannel channel, Response res, UserInput input) + { + // Verify we have everything. From here out, we promise we have that all. + if(event == null || user == null || guild == null || channel == null || res == null || input == null) + return false; + + // If the guild member is the owner, give them admin role by default. + if(event.getMember().isOwner()) + user.ChangeRole(KittyRole.Admin); + + // Give devs the dev role + for(int i = 0; i < Ref.devIDs.length; ++i) + { + if(event.getAuthor().getId().equals(Ref.devIDs[i])) + { + user.ChangeRole(KittyRole.Dev); + break; + } + } + + return true; + } + + // This is for stuff that we need to do on a regular basis, but don't + // necessarily want running at all points in time. + // Happens just after the command / plugin runs. + public static boolean PerCommandUpkeepPost(JDA bot, DatabaseManager databaseManager) + { + // Upkeep database + databaseManager.Upkeep(); + + // Update command-specific + RPManager.Upkeep(bot); + + return true; + } + + // Only called once per command. Good for lazily updating. + // Happens just before the command / plugin runs. + public static boolean PerCommandUpkeepPre() + { + // Upkeep localization system's file monitoring + LocStrings.Upkeep(); + LocCommands.Upkeep(); + + return true; + } +}