From 12d3d1e486ebb44a08fcf192e6f9d8d6fc30592b Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 27 Mar 2022 22:42:03 -0700 Subject: [PATCH] More command work, shitloads of commentation, and version bump, oh also LEAGUE OF LEGENDS PRENVETION HAHAHAHHAHAHA --- pom.xml | 2 +- src/main/java/moe/oko/Kiafumi/Kiafumi.java | 9 +++- .../java/moe/oko/Kiafumi/KiafumiConfig.java | 14 ++++- .../oko/Kiafumi/listener/MainListener.java | 51 ++++++++++++++++++- .../java/moe/oko/Kiafumi/model/KiafumiDB.java | 4 ++ .../java/moe/oko/Kiafumi/model/Server.java | 18 ++++++- .../moe/oko/Kiafumi/model/ServerManager.java | 28 ++++++++++ src/main/resources/config.yml | 6 +++ 8 files changed, 127 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 424fdf3..cbb1fda 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ moe.oko Kiafumi - 0.1 + 0.3 8 diff --git a/src/main/java/moe/oko/Kiafumi/Kiafumi.java b/src/main/java/moe/oko/Kiafumi/Kiafumi.java index 524e077..6c6bda5 100644 --- a/src/main/java/moe/oko/Kiafumi/Kiafumi.java +++ b/src/main/java/moe/oko/Kiafumi/Kiafumi.java @@ -72,7 +72,7 @@ public class Kiafumi { * Ran on program start. Anything in here can determine whether the program will start. */ public void start() { - + //All commands to be loaded on startup! activeCommands = new ArrayList<>(); activeCommands.add(new PingCommand()); @@ -166,6 +166,9 @@ public class Kiafumi { JDA.getTextChannelById(config.getLogChannel()).sendMessageEmbeds(eb.build()).queue(); } + /** + * Quick method to register commands in all servers. + */ private void registerAllCommands() { for(Guild guild : JDA.getGuilds()) { registerForGuild(guild); @@ -176,6 +179,10 @@ public class Kiafumi { } } + /** + * Registers all bot commands with the guild provided + * @param guild - guild to have commands provided to + */ public void registerForGuild(Guild guild) { info("Registering commands for " + guild.getId()); for(CommandClass cmd : activeCommands) { diff --git a/src/main/java/moe/oko/Kiafumi/KiafumiConfig.java b/src/main/java/moe/oko/Kiafumi/KiafumiConfig.java index 57fcc89..286da32 100644 --- a/src/main/java/moe/oko/Kiafumi/KiafumiConfig.java +++ b/src/main/java/moe/oko/Kiafumi/KiafumiConfig.java @@ -37,6 +37,9 @@ public class KiafumiConfig { private String activityMsg; private String statusType; private List pingResponses; + private boolean gamePrevention; + private int gameCheckTime; + private String gameToPrevent; /* SQL Variable Section @@ -70,13 +73,16 @@ public class KiafumiConfig { info("Invite link - " + assembleDefaultInvite()); prefix = discord.getString("prefix"); info("Prefix - " + prefix); - //Shiki loaders + //Kia loaders ConfigurationSection main = configuration.getConfigurationSection("main"); sharded = main.getBoolean("sharded"); activityType = main.getString("activityType"); activityMsg = main.getString("activityMsg"); statusType = main.getString("statusType"); pingResponses = main.getStringList("pingResponses"); + gamePrevention = main.getBoolean("gamePrevention"); + gameCheckTime = main.getInt("gameCheckTime"); + gameToPrevent = main.getString("gameToPrevent"); //SQL loaders ConfigurationSection sql = configuration.getConfigurationSection("sql"); host = sql.getString("host"); @@ -111,6 +117,12 @@ public class KiafumiConfig { public String getLogChannel() { return logChannel; } + public boolean isGamePreventionEnabled() { return gamePrevention; } + + public int getGameCheckTime() { return gameCheckTime; } + + public String getGameToPrevent() { return gameToPrevent; } + public List getPingResponses() { return pingResponses; } public KiafumiDB createDb() { return new KiafumiDB(username, password, host, port, database); } diff --git a/src/main/java/moe/oko/Kiafumi/listener/MainListener.java b/src/main/java/moe/oko/Kiafumi/listener/MainListener.java index beadd6e..7d3f6c6 100644 --- a/src/main/java/moe/oko/Kiafumi/listener/MainListener.java +++ b/src/main/java/moe/oko/Kiafumi/listener/MainListener.java @@ -1,8 +1,16 @@ package moe.oko.Kiafumi.listener; import moe.oko.Kiafumi.Kiafumi; +import net.dv8tion.jda.api.entities.Activity; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.events.ReadyEvent; import net.dv8tion.jda.api.events.guild.GuildJoinEvent; +import net.dv8tion.jda.api.events.user.update.GenericUserPresenceEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; +import org.jetbrains.annotations.NotNull; + +import java.util.Timer; +import java.util.TimerTask; import static moe.oko.Kiafumi.Kiafumi.info; @@ -17,11 +25,52 @@ public class MainListener extends ListenerAdapter { * @param event - event to be handled... */ @Override - public void onGuildJoin(GuildJoinEvent event) { + public void onGuildJoin(@NotNull GuildJoinEvent event) { //Automatically create our default information for the server if we don't have it already. info("Joined a new guild, NAME: " + event.getGuild().getName() + " ID: " + event.getGuild().getId()); Kiafumi.instance.getServerManager().createNewDefaultServer(event.getGuild()); Kiafumi.instance.registerForGuild(event.getGuild()); } + /** + * Shoots a message into console when the bot is defined as "Ready" by Discord. + */ + @Override + public void onReady(@NotNull ReadyEvent event) { + info("Received READY signal from Discord, bot is now logged in." + + " Guilds Active: " + event.getGuildAvailableCount() + " Guilds Unavailable: " + event.getGuildUnavailableCount()); + } + + /** + * Game Prevention, is a global mechanic :^) + * @param event - GenericUserPresenceEvent event to be used. + */ + @Override + public void onGenericUserPresence(@NotNull GenericUserPresenceEvent event) { + if(Kiafumi.instance.config.isGamePreventionEnabled()) { + //Proceed. + if(event.getMember().getActivities().contains(Activity.playing(Kiafumi.instance.config.getGameToPrevent()))) { + //THEY ARE PLAYING THE BIG BAD GAME!!!! WARN THE MIMMEDIATELYOYUITHTHHT + event.getMember().getUser().openPrivateChannel().complete().sendMessage("**YOU ARE PLAYING THE BIG BAD GAME STOP STOP STOP** " + + "(in " + Kiafumi.instance.config.getGameCheckTime() + " minutes i wll EVAPORATE you from every server i am IN.").queue(); + Timer timer = new Timer(); + int timeInMilisToKill = (Kiafumi.instance.config.getGameCheckTime() * 60) * 1000; + timer.schedule(new TimerTask() { + @Override + public void run() { + if(event.getMember().getActivities().contains(Activity.playing(Kiafumi.instance.config.getGameToPrevent()))) { + event.getMember().getUser().openPrivateChannel().complete().sendMessage("**I warned you, NOW SUFFER MY RAGE**").queue(); + for(Guild guild : event.getJDA().getGuilds()) { + if(guild.isMember(event.getMember().getUser())) { + guild.ban(guild.getMember(event.getMember().getUser()), 0, "PLAYED A HORRIBLE GAME").queue(); + } + } + } else { + event.getMember().getUser().openPrivateChannel().complete().sendMessage("Thank you for not playing that shitty game anymore.").queue(); + } + } + }, timeInMilisToKill); + } + } + } } diff --git a/src/main/java/moe/oko/Kiafumi/model/KiafumiDB.java b/src/main/java/moe/oko/Kiafumi/model/KiafumiDB.java index dbc9628..07abfa5 100644 --- a/src/main/java/moe/oko/Kiafumi/model/KiafumiDB.java +++ b/src/main/java/moe/oko/Kiafumi/model/KiafumiDB.java @@ -89,6 +89,10 @@ public class KiafumiDB { } } + /** + * Loads all the server information from MySQL into memory. + * @return - a list of all servers loaded from MySQL. + */ public List loadServerInformation() { List servers = new ArrayList<>(); try { diff --git a/src/main/java/moe/oko/Kiafumi/model/Server.java b/src/main/java/moe/oko/Kiafumi/model/Server.java index 6152e47..19be5c3 100644 --- a/src/main/java/moe/oko/Kiafumi/model/Server.java +++ b/src/main/java/moe/oko/Kiafumi/model/Server.java @@ -7,14 +7,23 @@ import javax.annotation.Nullable; * Used for in-memory data storage. Loaded from Database later. */ public class Server { + //Guild ID private String id; + //Whether the welcome feature is enabled (Join/Leave Logs) private boolean welcomeEnabled; + //If enabled, the channel for logs to be posted to. private String welcomeChannel; + //The role to be assigned on join, if null ignored. private String joinRole; + //TODO Whether the server has protections enabled private boolean serverProtected; - + //If the server has been modified in memory, for saving persistently. private boolean modified; + /** + * Default constructor, used for new servers + * @param id - the guild id to have server constructed for. + */ public Server(String id) { this.id = id; this.welcomeEnabled = false; @@ -40,8 +49,15 @@ public class Server { this.modified = false; } + /** + * @return - the guild id the server is assigned to + */ public String getId() { return id; } + /** + * Checks the modification of the server file in memory + * @return - whether the server settings have been modified + */ public boolean isModified() { return modified; } diff --git a/src/main/java/moe/oko/Kiafumi/model/ServerManager.java b/src/main/java/moe/oko/Kiafumi/model/ServerManager.java index 4da909d..35aa894 100644 --- a/src/main/java/moe/oko/Kiafumi/model/ServerManager.java +++ b/src/main/java/moe/oko/Kiafumi/model/ServerManager.java @@ -9,9 +9,17 @@ import java.util.List; import static moe.oko.Kiafumi.Kiafumi.error; import static moe.oko.Kiafumi.Kiafumi.info; +/** + * ServerManager Class + * Permits the access of servers easily + */ public class ServerManager { + //Server Memory Storage Hashmap, private HashMap servers = new HashMap<>(); + /** + * Constructor, loads servers and initializes the hashmap. + */ public ServerManager() { List loadedServers = Kiafumi.instance.database.loadServerInformation(); if(loadedServers == null) { @@ -27,11 +35,31 @@ public class ServerManager { servers = serverHashMap; } + /** + * Fetches the server via the guild id + * @param id - the id to find a server for + * @return - the server, if non-existent a default profile is created for it. + */ public Server getOrCreateServer(String id) { if(servers.get(id) == null) { createNewDefaultServer(Kiafumi.JDA.getGuildById(id)); } return servers.get(id); } + /** + * Fetches the server via the guild. + * @param guild - the guild to find the server for + * @return - the server, if non-existent a default profile is created for it. + */ + public Server getOrCreateServer(Guild guild) { + if(servers.get(guild.getId()) == null) { createNewDefaultServer(guild); } + return servers.get(guild.getId()); + } + + /** + * Initializes a persistent profile for the server and creates a new one that is loaded into memory. + * @param guild - the guild to have a default profile created for it. + * @return - whether the function succeeded. + */ public boolean createNewDefaultServer(Guild guild) { info("Started default server creation for " + guild.getId()); Server server = new Server(guild.getId()); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 690dcdf..f905272 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -51,6 +51,12 @@ main: - "Pinged a poortea vpn" - "Pinged 'agoo'" - "Pinged Ayana" + #Prevents users in the guild from playing a certain game + gamePrevention: true + #Amount of time in minutes to check after a user starts playing the game until you kick them. + gameCheckTime: 30 + #The game to be prevented + gameToPrevent: "League Of Legends" sql: host: "localhost" port: 3306