Compare commits
6 commits
769f5f7ec8
...
8074ae1971
Author | SHA1 | Date | |
---|---|---|---|
Anya | 8074ae1971 | ||
Anya | 9183d350df | ||
Anya | dfdd032a33 | ||
Anya | ff626019b0 | ||
Anya | ab72638a94 | ||
Anya | 67dafe50bb |
|
@ -14,17 +14,13 @@ import net.dv8tion.jda.api.JDABuilder;
|
|||
import net.dv8tion.jda.api.OnlineStatus;
|
||||
import net.dv8tion.jda.api.entities.Activity;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.SubcommandData;
|
||||
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||
import net.dv8tion.jda.api.utils.ChunkingFilter;
|
||||
import net.dv8tion.jda.api.utils.MemberCachePolicy;
|
||||
import net.dv8tion.jda.api.utils.cache.CacheFlag;
|
||||
import org.simpleyaml.configuration.file.YamlConfiguration;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
|
@ -32,6 +28,8 @@ import java.nio.file.Files;
|
|||
import java.time.ZonedDateTime;
|
||||
import java.util.List;
|
||||
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.*;
|
||||
|
||||
/**
|
||||
* Kiafumi Main Class
|
||||
* @author Kay, oko, Tiddy
|
||||
|
@ -50,8 +48,6 @@ public class Kiafumi {
|
|||
|
||||
public YamlConfiguration yamlConfiguration = new YamlConfiguration();
|
||||
|
||||
public Logger logger = LoggerFactory.getLogger("Kiafumi");
|
||||
|
||||
public String footer = EmbedUI.BRAND + " 0.9.0-pre";
|
||||
|
||||
public static Kiafumi instance;
|
||||
|
@ -63,7 +59,6 @@ public class Kiafumi {
|
|||
public KiafumiConfig config;
|
||||
|
||||
public KiafumiDB database;
|
||||
|
||||
public ServerManager serverManager;
|
||||
|
||||
/**
|
||||
|
@ -85,8 +80,7 @@ public class Kiafumi {
|
|||
*/
|
||||
public void start() {
|
||||
instance = this;
|
||||
|
||||
logger.info("Starting Kiafumi.");
|
||||
info("Starting Kiafumi.");
|
||||
|
||||
// All commands to be loaded on startup!
|
||||
activeCommands = new CommandRegistrar().getCommandClasses();
|
||||
|
@ -97,7 +91,7 @@ public class Kiafumi {
|
|||
// Save the default config
|
||||
Files.copy(is, CONFIG_FILE.toPath());
|
||||
} catch (Exception ex) {
|
||||
logger.warn("Failed to create the configuration file. Stopping. (" + CONFIG_FILE.getAbsolutePath() + ")");
|
||||
warn("Failed to create the configuration file. Stopping. (" + CONFIG_FILE.getAbsolutePath() + ")");
|
||||
ex.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
@ -108,23 +102,23 @@ public class Kiafumi {
|
|||
yamlConfiguration.load("config.yml");
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
logger.warn("File not found, must've failed to create...");
|
||||
warn("File not found, must've failed to create...");
|
||||
} catch (Exception e) {
|
||||
logger.warn("Ensure all values are inputted properly.");
|
||||
warn("Ensure all values are inputted properly.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Initializes our configuration helper & ensures it loads properly.
|
||||
config = new KiafumiConfig(yamlConfiguration);
|
||||
if(config.load()) {
|
||||
logger.info("Fetched Kiafumi config.");
|
||||
info("Fetched Kiafumi config.");
|
||||
} else {
|
||||
logger.error("Failed to load configuration. Stopping process.");
|
||||
error("Failed to load configuration. Stopping process.");
|
||||
Runtime.getRuntime().exit(0);
|
||||
}
|
||||
|
||||
// Registers the stop() function if the program is stopped.
|
||||
logger.info("Registering shutdown hook.");
|
||||
info("Registering shutdown hook.");
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(this::stop));
|
||||
|
||||
// Initializes database and loads credentials.
|
||||
|
@ -250,34 +244,6 @@ public class Kiafumi {
|
|||
debug("Registered " + i + " commands for guild [" + guild.getId() + "]");
|
||||
}
|
||||
|
||||
/*
|
||||
Static logger references.
|
||||
*/
|
||||
public static void info(String str) {
|
||||
instance.logger.info(str);
|
||||
}
|
||||
public static void error(String str) {
|
||||
instance.logger.error(str);
|
||||
}
|
||||
public static void debug(String str) { instance.logger.debug(str); }
|
||||
|
||||
/**
|
||||
* Used for logging commands with ease to console via a static method.
|
||||
* @param event - the event ran
|
||||
* @param msg - Any message to append with this.
|
||||
*/
|
||||
public static void slashLog(SlashCommandInteractionEvent event, @Nullable String msg) {
|
||||
info("User [" + event.getUser().getName() + ":" + event.getUser().getId() + "] ran command: \"" + event.getName() + "\" " + (msg == null ? "" : msg));
|
||||
}
|
||||
|
||||
public static void slashLog(SlashCommandInteractionEvent event) {
|
||||
info("User [" + event.getUser().getName() + ":" + event.getUser().getId() + "] ran command: \"" + event.getName() + "\".");
|
||||
}
|
||||
|
||||
public static void slashResponse(SlashCommandInteractionEvent event, String msg) {
|
||||
info("User [" + event.getUser().getName() + ":" + event.getUser().getId() + "] was provided response: \"" + msg + "\"");
|
||||
}
|
||||
|
||||
// Gets the active database.
|
||||
public KiafumiDB getDatabase() { return database; }
|
||||
|
||||
|
|
|
@ -5,8 +5,8 @@ import org.simpleyaml.configuration.file.YamlConfiguration;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.error;
|
||||
import static moe.oko.Kiafumi.Kiafumi.info;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.error;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.info;
|
||||
|
||||
/**
|
||||
* KiafumiConfig class
|
||||
|
@ -63,8 +63,6 @@ public class KiafumiConfig {
|
|||
*/
|
||||
public boolean load() {
|
||||
try {
|
||||
var n = System.lineSeparator() + "\033[0m"; // newline
|
||||
|
||||
// Discord loaders
|
||||
var discord = configuration.getConfigurationSection("discord");
|
||||
token = discord.getString("token"); // This used to log the token into stdout ???
|
||||
|
@ -74,14 +72,15 @@ public class KiafumiConfig {
|
|||
clientId = discord.getString("clientId");
|
||||
defaultInvitePermissionLevel = discord.getInt("invitePermissionLevel");
|
||||
// Log discord settings in a neat table.
|
||||
info("Printing loaded discord configuration." +n+
|
||||
"DISCORD CONFIGURATION" +n+
|
||||
"--------------------------------" +n+
|
||||
"Log Channel: \033[0;34m" + logChannel +n+
|
||||
"Owner ID: \033[0;34m" + ownerId +n+
|
||||
"Primary Guild: \033[0;34m" + mainGuild +n+
|
||||
"Invite URL: \033[0;34m" + assembleDefaultInvite() +n+
|
||||
"--------------------------------");
|
||||
info("""
|
||||
Printing loaded discord configuration.
|
||||
DISCORD CONFIGURATION
|
||||
--------------------------------
|
||||
Log Channel: %s
|
||||
Owner ID: %s
|
||||
Primary Guild: %s
|
||||
Invite URL: %s
|
||||
--------------------------------""".formatted(logChannel, ownerId, mainGuild, assembleDefaultInvite()));
|
||||
|
||||
// Kiafumi loaders
|
||||
var main = configuration.getConfigurationSection("main");
|
||||
|
|
|
@ -9,9 +9,9 @@ import java.util.List;
|
|||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.error;
|
||||
import static moe.oko.Kiafumi.Kiafumi.info;
|
||||
import static moe.oko.Kiafumi.Kiafumi.debug;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.error;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.info;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.debug;
|
||||
|
||||
/**
|
||||
* CommandRegistrar Class
|
||||
|
|
|
@ -13,7 +13,7 @@ import java.util.List;
|
|||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.slashLog;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.slashLog;
|
||||
|
||||
/**
|
||||
* Dreidel Dreidel...
|
||||
|
|
|
@ -16,7 +16,7 @@ import java.util.List;
|
|||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.slashLog;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.slashLog;
|
||||
|
||||
/**
|
||||
* Random User Choice Command
|
||||
|
|
|
@ -14,7 +14,7 @@ import java.time.temporal.ChronoUnit;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.slashLog;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.slashLog;
|
||||
|
||||
/**
|
||||
* Fetches the REAL date
|
||||
|
|
|
@ -12,7 +12,7 @@ import java.time.ZonedDateTime;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.slashLog;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.slashLog;
|
||||
|
||||
/**
|
||||
* Helpful Avatar grabber command
|
||||
|
|
|
@ -14,8 +14,8 @@ import java.net.URI;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.error;
|
||||
import static moe.oko.Kiafumi.Kiafumi.slashLog;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.error;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.slashLog;
|
||||
import static moe.oko.Kiafumi.util.ResponseHandlers.STRING_RESPONSE_HANDLER;
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,111 +0,0 @@
|
|||
package moe.oko.Kiafumi.command.moderation;
|
||||
|
||||
import moe.oko.Kiafumi.Kiafumi;
|
||||
import moe.oko.Kiafumi.command.CommandClass;
|
||||
import moe.oko.Kiafumi.model.Server;
|
||||
import moe.oko.Kiafumi.util.CommandInfo;
|
||||
import moe.oko.Kiafumi.util.CommandType;
|
||||
import moe.oko.Kiafumi.util.EmbedUI;
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.slashLog;
|
||||
|
||||
/**
|
||||
* For use on Guilds that tend to get TOS'd with ease. Also just basic server protection
|
||||
* @author Kay
|
||||
* (Requires Permission.ADMINISTRATOR to function properly)
|
||||
*/
|
||||
@Deprecated
|
||||
public class ModCommand extends CommandClass {
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Moderation";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void newCommand(String name, SlashCommandInteractionEvent e) {
|
||||
if(e.getGuild() == null) { return; }
|
||||
var server = Kiafumi.instance.getServerManager().getOrCreateServer(e.getGuild());
|
||||
switch (name) {
|
||||
case "mod":
|
||||
slashLog(e);
|
||||
e.deferReply(true).queue();
|
||||
if(server.isServerProtected()) {
|
||||
// Start check.
|
||||
if(server.getModRole() != null) {
|
||||
if(e.getMember().isOwner()) {
|
||||
e.getHook().sendMessage("*Owners cannot run this command.*").queue();
|
||||
return;
|
||||
}
|
||||
if(hasModRole(e.getMember(), server)) {
|
||||
if(isAdmin(e.getMember())) {
|
||||
// They already have the admin role... So we try removing our mod role in case thats it
|
||||
removeModRole(e.getMember());
|
||||
e.getHook().sendMessage("**Admin Mode Disabled.**").queue();
|
||||
} else {
|
||||
if(applyModRole(e.getMember())) {
|
||||
e.getHook().sendMessage("**Admin Mode Enabled.**").queue();
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
} else {
|
||||
e.getHook().sendMessage("*This server does not have a moderator role set.*").queue();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
e.getHook().sendMessage("*This server does not have protection enabled. This command will not function.*").queue();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isAdmin(Member member) {
|
||||
return member.hasPermission(Permission.ADMINISTRATOR);
|
||||
}
|
||||
|
||||
public boolean hasModRole(Member member, Server server) {
|
||||
return member.getRoles().contains(Kiafumi.JDA.getRoleById(server.getModRole()));
|
||||
}
|
||||
|
||||
public boolean applyModRole(Member member) {
|
||||
if(member.getGuild().getRolesByName("senior citizen", true).get(0) != null) {
|
||||
member.getGuild().addRoleToMember(member, member.getGuild().getRolesByName("senior citizen", true).get(0)).queue();
|
||||
return true;
|
||||
} else {
|
||||
// Create, role doesn't exist
|
||||
member.getGuild().createRole()
|
||||
.setColor(EmbedUI.INFO)
|
||||
.setName("senior citizen")
|
||||
.setPermissions(Permission.ADMINISTRATOR)
|
||||
.setMentionable(false)
|
||||
.setHoisted(true)
|
||||
.complete();
|
||||
// should be created so rerun
|
||||
return applyModRole(member);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeModRole(Member member) {
|
||||
if(member.getGuild().getRolesByName("senior citizen", true).get(0) != null) {
|
||||
member.getGuild().removeRoleFromMember(member, member.getGuild().getRolesByName("senior citizen", true).get(0)).queue();
|
||||
} // Role must've been deleted otherwise, :thinking:
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CommandInfo> getSlashCommandInfo() {
|
||||
List<CommandInfo> cil = new ArrayList<>();
|
||||
cil.add(new CommandInfo("mod", "Toggles administrative permissions you.", CommandType.COMMAND));
|
||||
return cil;
|
||||
}
|
||||
}
|
|
@ -28,7 +28,7 @@ import java.io.File;
|
|||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.slashLog;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.slashLog;
|
||||
|
||||
/**
|
||||
* Music Command
|
||||
|
|
|
@ -14,7 +14,7 @@ import java.time.ZonedDateTime;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.slashLog;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.slashLog;
|
||||
|
||||
/**
|
||||
* Helpful Search Command (Uses DDG API)
|
||||
|
|
|
@ -12,12 +12,12 @@ import java.time.ZonedDateTime;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.slashLog;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.slashLog;
|
||||
|
||||
public class HelpCommand extends CommandClass {
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return true; //perma-enabled
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -30,12 +30,12 @@ public class HelpCommand extends CommandClass {
|
|||
if ("help".equalsIgnoreCase(name)) {
|
||||
slashLog(e);
|
||||
e.deferReply().queue();
|
||||
//We assemble a help string, this is our description for our embed.
|
||||
StringBuilder sb = new StringBuilder();
|
||||
// We assemble a help string, this is our description for our embed.
|
||||
var sb = new StringBuilder();
|
||||
for(CommandClass cc : Kiafumi.instance.activeCommands) {
|
||||
for(CommandInfo ci : cc.getSlashCommandInfo()) {
|
||||
String cname = ci.getName();
|
||||
String cdesc= ci.getDescription();
|
||||
var cname = ci.getName();
|
||||
var cdesc= ci.getDescription();
|
||||
sb.append("**").append(cname).append("** - __").append(cdesc).append("__\n");
|
||||
if(ci.hasOptions()) {
|
||||
for (String opt : ci.getOptions().keySet()) {
|
||||
|
@ -49,7 +49,7 @@ public class HelpCommand extends CommandClass {
|
|||
}
|
||||
if(ci.hasSubCommands()) {
|
||||
for (String subc : ci.getSubCommands().keySet()) {
|
||||
CommandInfo subCommand = ci.getSubCommands().get(subc);
|
||||
var subCommand = ci.getSubCommands().get(subc);
|
||||
sb.append("-> **").append(subc).append("** *").append(subCommand.getDescription()).append("*\n");
|
||||
if (subCommand.hasOptions()) {
|
||||
for (String opt : subCommand.getOptions().keySet()) {
|
||||
|
@ -80,7 +80,7 @@ public class HelpCommand extends CommandClass {
|
|||
@Override
|
||||
public List<CommandInfo> getSlashCommandInfo() {
|
||||
List<CommandInfo> cil = new ArrayList<>();
|
||||
CommandInfo ci = new CommandInfo("help", "Displays all enabled commands this bot has.", CommandType.COMMAND);
|
||||
var ci = new CommandInfo("help", "Displays all enabled commands this bot has.", CommandType.COMMAND);
|
||||
cil.add(ci);
|
||||
return cil;
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ import java.time.format.DateTimeFormatter;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.slashLog;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.slashLog;
|
||||
|
||||
/**
|
||||
* Helpful User Information Command
|
||||
|
|
|
@ -12,7 +12,7 @@ import java.time.ZonedDateTime;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.slashLog;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.slashLog;
|
||||
|
||||
/**
|
||||
* Helpful Invite Command
|
||||
|
|
|
@ -14,7 +14,7 @@ import java.util.List;
|
|||
import java.util.Locale;
|
||||
import java.util.Random;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.slashLog;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.slashLog;
|
||||
|
||||
/**
|
||||
* Helpful Ping Command
|
||||
|
|
|
@ -15,8 +15,8 @@ import java.time.ZonedDateTime;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.slashLog;
|
||||
import static moe.oko.Kiafumi.Kiafumi.slashResponse;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.slashLog;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.slashResponse;
|
||||
|
||||
/**
|
||||
* Permits modification of server settings, critical class to functionality.
|
||||
|
|
|
@ -11,7 +11,7 @@ import org.jetbrains.annotations.NotNull;
|
|||
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.info;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.info;
|
||||
|
||||
/**
|
||||
* Main Listener
|
||||
|
@ -37,12 +37,12 @@ public class MainListener extends ListenerAdapter {
|
|||
*/
|
||||
@Override
|
||||
public void onReady(@NotNull ReadyEvent event) {
|
||||
var n = System.lineSeparator();
|
||||
info("Received READY signal from Discord, bot is now logged in." +n+
|
||||
"--------------------------------" +n+
|
||||
"Active Guilds: " + event.getGuildAvailableCount() +n+
|
||||
"Guilds Unavailable: " + event.getGuildUnavailableCount() +n+
|
||||
"--------------------------------");
|
||||
info("""
|
||||
Received READY signal from Discord, bot is now logged in.
|
||||
--------------------------------
|
||||
Active Guilds: [%s]
|
||||
Guilds Unavailable: [%s]
|
||||
--------------------------------""".formatted(event.getGuildAvailableCount(), event.getGuildUnavailableCount()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -14,9 +14,9 @@ import org.jetbrains.annotations.NotNull;
|
|||
import java.time.OffsetDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.debug;
|
||||
import static moe.oko.Kiafumi.Kiafumi.info;
|
||||
import static moe.oko.Kiafumi.Kiafumi.error;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.debug;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.info;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.error;
|
||||
|
||||
/**
|
||||
* Skynet Listener
|
||||
|
@ -30,34 +30,36 @@ public class SkynetListener extends ListenerAdapter {
|
|||
|
||||
/**
|
||||
* Join/Leave logging
|
||||
* Requires the server to configure welcomeEnabled & welcomeChannel (optionally joinRole).
|
||||
* Requires the server to configure welcomeChannel (optionally joinRole).
|
||||
*/
|
||||
@Override
|
||||
public void onGuildMemberJoin(@NotNull GuildMemberJoinEvent event) {
|
||||
var server = Kiafumi.instance.getServerManager().getOrCreateServer(event.getGuild());
|
||||
var guild = event.getGuild();
|
||||
var server = Kiafumi.instance.getServerManager().getOrCreateServer(guild);
|
||||
var member = event.getMember();
|
||||
info("User [" + event.getUser().getName() + ":" + event.getUser().getId() + "] joined guild [" + guild.getName() + ':' + guild.getId() + "].");
|
||||
if(server.getJoinRole() != null) {
|
||||
try {
|
||||
event.getGuild().addRoleToMember(event.getMember(), event.getGuild().getRoleById(server.getJoinRole())).queue();
|
||||
guild.addRoleToMember(member, guild.getRoleById(server.getJoinRole())).queue();
|
||||
} catch(Exception ex) {
|
||||
error("Failed to apply welcome role to " + event.getMember().getEffectiveName() + ", as the role does not exist.");
|
||||
error("Failed to apply welcome role to " + member.getEffectiveName() + ", as the role does not exist.");
|
||||
}
|
||||
}
|
||||
if(server.isWelcomeEnabled()) {
|
||||
info("User [" + event.getUser().getName() + ":" + event.getUser().getId() + "] joined guild [" + event.getGuild().getName() + ':' + event.getGuild().getId() + "].");
|
||||
if(server.getWelcomeChannel() != null) {
|
||||
// Fetch the welcome channel from settings.
|
||||
TextChannel textChannel;
|
||||
try {
|
||||
textChannel = event.getGuild().getTextChannelById(server.getWelcomeChannel());
|
||||
textChannel = guild.getTextChannelById(server.getWelcomeChannel());
|
||||
} catch (Exception ex) {
|
||||
error("Failed to send join message to guild " + event.getGuild().getId() + " as the welcome channel was not found.");
|
||||
error("Failed to send join message to guild " + guild.getId() + " as the welcome channel was not found.");
|
||||
return;
|
||||
}
|
||||
// Prepare embed.
|
||||
var eb = new EmbedBuilder()
|
||||
.setColor(EmbedUI.SUCCESS)
|
||||
.setAuthor(event.getMember().getEffectiveName() + "#" + event.getMember().getUser().getDiscriminator() + " ("
|
||||
+ event.getMember().getId() + ")", null, event.getUser().getAvatarUrl())
|
||||
.setDescription(event.getMember().getAsMention() + " | **Joined Discord**: " + event.getMember().getTimeCreated().format(dTF))
|
||||
.setAuthor(member.getEffectiveName() + "#" + member.getUser().getDiscriminator() + " ("
|
||||
+ member.getId() + ")", null, event.getUser().getAvatarUrl()) // Url cannot be member.
|
||||
.setDescription(member.getAsMention() + " | **Joined Discord**: " + member.getTimeCreated().format(dTF))
|
||||
.setFooter("User Joined")
|
||||
.setTimestamp(OffsetDateTime.now());
|
||||
textChannel.sendMessageEmbeds(eb.build()).queue();
|
||||
|
@ -67,23 +69,25 @@ public class SkynetListener extends ListenerAdapter {
|
|||
|
||||
@Override
|
||||
public void onGuildMemberRemove(@NotNull GuildMemberRemoveEvent event) {
|
||||
info("User [" + event.getUser().getName() + ":" + event.getUser().getId() + "] left guild [" + event.getGuild().getName() + ':' + event.getGuild().getId() + "].");
|
||||
var server = Kiafumi.instance.getServerManager().getOrCreateServer(event.getGuild());
|
||||
if(server.isWelcomeEnabled()) {
|
||||
var guild = event.getGuild();
|
||||
info("User [" + event.getUser().getName() + ":" + event.getUser().getId() + "] left guild [" + guild.getName() + ':' + guild.getId() + "].");
|
||||
var server = Kiafumi.instance.getServerManager().getOrCreateServer(guild);
|
||||
if(server.getWelcomeChannel() != null) {
|
||||
// Fetch the welcome channel from settings.
|
||||
TextChannel textChannel;
|
||||
var member = event.getMember();
|
||||
try {
|
||||
textChannel = event.getGuild().getTextChannelById(server.getWelcomeChannel());
|
||||
textChannel = guild.getTextChannelById(server.getWelcomeChannel());
|
||||
} catch (Exception ex) {
|
||||
error("Failed to send leave message to guild " + event.getGuild().getId() + " as the welcome channel was not found.");
|
||||
error("Failed to send leave message to guild " + guild.getId() + " as the welcome channel was not found.");
|
||||
return;
|
||||
}
|
||||
// Prepare embed.
|
||||
var eb = new EmbedBuilder()
|
||||
.setColor(EmbedUI.FAILURE)
|
||||
.setAuthor(event.getMember().getEffectiveName() + "#" + event.getMember().getUser().getDiscriminator() + " ("
|
||||
+ event.getMember().getId() + ")", null, event.getUser().getAvatarUrl())
|
||||
.setDescription(event.getMember().getAsMention() + " | **Joined Server**: " + event.getMember().getTimeJoined().format(dTF))
|
||||
.setAuthor(member.getEffectiveName() + "#" + member.getUser().getDiscriminator() + " ("
|
||||
+ member.getId() + ")", null, event.getUser().getAvatarUrl()) // Url cannot be member.
|
||||
.setDescription(member.getAsMention() + " | **Joined Server**: " + member.getTimeJoined().format(dTF))
|
||||
.setFooter("User Left")
|
||||
.setTimestamp(OffsetDateTime.now());
|
||||
textChannel.sendMessageEmbeds(eb.build()).queue();
|
||||
|
|
|
@ -8,7 +8,7 @@ import java.util.ArrayList;
|
|||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.*;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.*;
|
||||
|
||||
/**
|
||||
* Kiafumi DB Class
|
||||
|
@ -17,17 +17,17 @@ import static moe.oko.Kiafumi.Kiafumi.*;
|
|||
* @apiNote I HATE SQL I HATE SQL AAAAAAAAAAAAAA
|
||||
*/
|
||||
public class KiafumiDB {
|
||||
//Our actual MySQL Connection, this is created on class construction.
|
||||
// Our actual MySQL Connection, this is created on class construction.
|
||||
private Connection connection;
|
||||
|
||||
//The prepared statement strings
|
||||
// The prepared statement strings
|
||||
private final String CREATE_SERVERINFO_TABLE = "CREATE TABLE IF NOT EXISTS `serverInfo`" +
|
||||
"(`id` LONGTEXT NOT NULL,`welcomeEnabled` TINYINT NOT NULL,`welcomeChannel` LONGTEXT NULL, joinRole LONGTEXT NULL, `serverProtected` TINYINT NOT NULL, `modRole` LONGTEXT NULL);";
|
||||
private final String INSERT_SERVERINFO_DEFAULT = "INSERT INTO serverInfo(id,welcomeEnabled,welcomeChannel, joinRole, serverProtected, modRole) values (?,?,?,?,?, ?)";
|
||||
"(`id` LONGTEXT NOT NULL, `welcomeChannel` LONGTEXT NULL, `modChannel` LONGTEXT NULL, joinRole LONGTEXT NULL);";
|
||||
private final String INSERT_SERVERINFO_DEFAULT = "INSERT INTO serverInfo(id, welcomeChannel, modChannel, joinRole) values (?,?,?,?)";
|
||||
private final String SERVERINFO_EXISTS = "select * from serverInfo where id = ?";
|
||||
private final String SERVER_INFO_LOAD = "select * from serverInfo";
|
||||
//1 = welcomeEnabled, 2 = welcomeChannel, 3 = joinRole, 4 = serverProtected, 5 = Guild_ID (non-changeable)
|
||||
private final String SERVER_INFO_MODIFY = "UPDATE `serverInfo` SET welcomeEnabled=?, welcomeChannel=?, joinRole=?,serverProtected=?, modRole=? WHERE id = ?";
|
||||
// 1 = welcomeChannel, 2 = modChannel , 3 = joinRole, 4 = Guild_ID (immutable)
|
||||
private final String SERVER_INFO_MODIFY = "UPDATE `serverInfo` SET welcomeChannel=?, modChannel=?, joinRole=? WHERE id = ?";
|
||||
|
||||
/**
|
||||
* KiafumiDB Config Constructor
|
||||
|
@ -41,7 +41,7 @@ public class KiafumiDB {
|
|||
try {
|
||||
connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + db, username, password);
|
||||
} catch (Exception ex) {
|
||||
error("Failed to initialize the MySQL instance. Contact a developer.");
|
||||
error("Failed to initialize the MySQL instance.");
|
||||
ex.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
@ -81,11 +81,9 @@ public class KiafumiDB {
|
|||
//Proceed with making defaults.
|
||||
PreparedStatement ps = connection.prepareStatement(INSERT_SERVERINFO_DEFAULT);
|
||||
ps.setString(1, guild.getId());
|
||||
ps.setInt(2, 0); //default is false.
|
||||
ps.setNull(2, Types.LONGVARCHAR);
|
||||
ps.setNull(3, Types.LONGVARCHAR);
|
||||
ps.setNull(4, Types.LONGVARCHAR);
|
||||
ps.setInt(5, 0); //Falseeeee
|
||||
ps.setNull(6, Types.LONGVARCHAR);
|
||||
ps.execute();
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
|
@ -107,24 +105,10 @@ public class KiafumiDB {
|
|||
while(rs.next()) {
|
||||
info("Starting new load for server: " + rs.getString(1));
|
||||
String id = rs.getString(1);
|
||||
boolean welcomeEnabled = false;
|
||||
switch(rs.getInt(2)) {
|
||||
case 0:
|
||||
welcomeEnabled = false;
|
||||
case 1:
|
||||
welcomeEnabled = true;
|
||||
}
|
||||
String welcomeChannel = rs.getString(3);
|
||||
String welcomeChannel = rs.getString(2);
|
||||
String modChannel = rs.getString(3);
|
||||
String joinRole = rs.getString(4);
|
||||
boolean protectionEnabled = false;
|
||||
switch(rs.getInt(5)) {
|
||||
case 0:
|
||||
protectionEnabled = false;
|
||||
case 1:
|
||||
protectionEnabled = true;
|
||||
}
|
||||
String modRole = rs.getString(6);
|
||||
Server server = new Server(id, welcomeEnabled, welcomeChannel, joinRole, protectionEnabled, modRole);
|
||||
Server server = new Server(id, welcomeChannel, modChannel, joinRole);
|
||||
debug("Loaded " + server + "from database.");
|
||||
servers.add(server);
|
||||
}
|
||||
|
@ -144,19 +128,19 @@ public class KiafumiDB {
|
|||
Collection<Server> servers = Kiafumi.instance.getServerManager().getServers();
|
||||
info("Starting save on " + servers.size() + " servers.");
|
||||
try {
|
||||
//1 = welcomeEnabled, 2 = welcomeChannel, 3 = joinRole, 4 = serverProtected, 5 = modRole 6 = Guild_ID (non-changeable)
|
||||
// 1 = welcomeChannel, 2 = modChannel, 3 = joinRole, 4 = Guild_ID (immutable)
|
||||
PreparedStatement ps = connection.prepareStatement(SERVER_INFO_MODIFY);
|
||||
int i = 0;
|
||||
for (Server server : servers) {
|
||||
if(!server.isModified()) { continue; } //Skip, non modified server.
|
||||
if(!server.isModified()) { continue; } // Skip, unmodified server.
|
||||
info("Starting save on modified " + server);
|
||||
if(server.isWelcomeEnabled()) {
|
||||
ps.setInt(1, 1);
|
||||
} else {
|
||||
ps.setInt(1, 0);
|
||||
}
|
||||
if(server.getWelcomeChannel() != null) {
|
||||
ps.setString(2, server.getWelcomeChannel());
|
||||
ps.setString(1, server.getWelcomeChannel());
|
||||
} else {
|
||||
ps.setNull(1, Types.LONGVARCHAR);
|
||||
}
|
||||
if(server.getModChannel() != null) {
|
||||
ps.setString(2, server.getModChannel());
|
||||
} else {
|
||||
ps.setNull(2, Types.LONGVARCHAR);
|
||||
}
|
||||
|
@ -165,17 +149,7 @@ public class KiafumiDB {
|
|||
} else {
|
||||
ps.setNull(3, Types.LONGVARCHAR);
|
||||
}
|
||||
if(server.isServerProtected()) {
|
||||
ps.setInt(4, 1);
|
||||
} else {
|
||||
ps.setInt(4, 0);
|
||||
}
|
||||
if(server.getModRole() != null) {
|
||||
ps.setString(5, server.getModRole());
|
||||
} else {
|
||||
ps.setNull(5, Types.LONGVARCHAR);
|
||||
}
|
||||
ps.setString(6, server.getId());
|
||||
ps.setString(4, server.getId());
|
||||
ps.addBatch();
|
||||
i++;
|
||||
}
|
||||
|
|
|
@ -11,18 +11,14 @@ import javax.annotation.Nullable;
|
|||
* @implNote This class is where all server info is stored per-server, so be liberal with additions.
|
||||
*/
|
||||
public class Server {
|
||||
//Guild ID
|
||||
// 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.
|
||||
// The channel for welcome logs to be posted to.
|
||||
private String welcomeChannel;
|
||||
//The role to be assigned on join, if null ignored.
|
||||
// The role to be assigned on join, if null ignored.
|
||||
private String joinRole;
|
||||
//TODO Whether the server has protections enabled (do protections)
|
||||
private boolean serverProtected;
|
||||
//Moderation role, used for /mod
|
||||
private String modRole;
|
||||
private String modChannel;
|
||||
//If the server has been modified in memory, for saving persistently.
|
||||
private boolean modified;
|
||||
|
||||
|
@ -32,28 +28,22 @@ public class Server {
|
|||
*/
|
||||
public Server(String id) {
|
||||
this.id = id;
|
||||
this.welcomeEnabled = false;
|
||||
this.welcomeChannel = null;
|
||||
this.modChannel = null;
|
||||
this.joinRole = null;
|
||||
this.serverProtected = false;
|
||||
this.modRole = null;
|
||||
this.modified = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Database Constructor
|
||||
* @param id - id of the server
|
||||
* @param welcomeEnabled - is welcome enabled for the server
|
||||
* @param welcomeChannel - channel for welcome messages, if enabled
|
||||
* @param serverProtected - is the server under protection rules. (/archive, /stop, etc.)
|
||||
*/
|
||||
public Server(String id, boolean welcomeEnabled, @Nullable String welcomeChannel, @Nullable String joinRole, boolean serverProtected, String modRole) {
|
||||
public Server(String id, @Nullable String welcomeChannel, @Nullable String joinRole, String modChannel) {
|
||||
this.id = id;
|
||||
this.welcomeEnabled = welcomeEnabled;
|
||||
this.welcomeChannel = welcomeChannel;
|
||||
this.modChannel = modChannel;
|
||||
this.joinRole = joinRole;
|
||||
this.serverProtected = serverProtected;
|
||||
this.modRole = modRole;
|
||||
this.modified = false;
|
||||
}
|
||||
|
||||
|
@ -61,28 +51,17 @@ public class Server {
|
|||
|
||||
public String getJoinRole() { return joinRole; }
|
||||
|
||||
public String getModRole() { return modRole; }
|
||||
|
||||
public boolean isWelcomeEnabled() {
|
||||
return welcomeEnabled;
|
||||
}
|
||||
public String getModChannel() { return modChannel; }
|
||||
|
||||
public String getWelcomeChannel() {
|
||||
return welcomeChannel;
|
||||
}
|
||||
|
||||
public boolean isServerProtected() { return serverProtected; }
|
||||
|
||||
public void setJoinRole(String joinRole) {
|
||||
this.modified = true;
|
||||
this.joinRole = joinRole;
|
||||
}
|
||||
|
||||
public void setWelcomeEnabled(boolean welcomeEnabled) {
|
||||
this.modified = true;
|
||||
this.welcomeEnabled = welcomeEnabled;
|
||||
}
|
||||
|
||||
public void setWelcomeChannel(String welcomeChannel) {
|
||||
this.modified = true;
|
||||
this.welcomeChannel = welcomeChannel;
|
||||
|
@ -101,11 +80,10 @@ public class Server {
|
|||
* @return - Options in a string that can be printed to discord.
|
||||
*/
|
||||
public String getOpts() {
|
||||
return "welcomeEnabled - whether or not join/leave logs are enabled" +
|
||||
"\nwelcomeChannel - the channel to send welcome messages to" +
|
||||
"\njoinRole - the role to apply to new members who join this guild" +
|
||||
"\nserverProtected - Determines whether or not server protections are enabled." +
|
||||
"\nmodRole - The moderator role for the /mod command (only works if protection enabled)";
|
||||
return """
|
||||
welcomeChannel - the channel to send welcome messages to
|
||||
modChannel - the channel to send moderation logs to
|
||||
joinRole - the role to apply to new members who join this guild""";
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -114,28 +92,12 @@ public class Server {
|
|||
* @return - the value (if applicable) to return
|
||||
*/
|
||||
public String getOptionByString(String string) {
|
||||
switch(string.toLowerCase()) {
|
||||
case "welcomeenabled":
|
||||
if(!welcomeEnabled) {
|
||||
return "False";
|
||||
} else {
|
||||
return "True";
|
||||
}
|
||||
case "joinrole":
|
||||
return joinRole;
|
||||
case "welcomechannel":
|
||||
return welcomeChannel;
|
||||
case "serverprotected":
|
||||
if(!serverProtected) {
|
||||
return "False";
|
||||
} else {
|
||||
return "True";
|
||||
}
|
||||
case "modrole":
|
||||
return modRole;
|
||||
default:
|
||||
return "INVALID";
|
||||
}
|
||||
return switch (string.toLowerCase()) {
|
||||
case "joinrole" -> joinRole;
|
||||
case "welcomechannel" -> welcomeChannel;
|
||||
case "modchannel" -> modChannel;
|
||||
default -> "INVALID";
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -145,21 +107,15 @@ public class Server {
|
|||
*/
|
||||
public String resetOptionByString(String name) {
|
||||
switch(name.toLowerCase()) {
|
||||
case "welcomeenabled":
|
||||
welcomeEnabled = false;
|
||||
return "Reset Join/Leave logs to default.";
|
||||
case "joinrole":
|
||||
joinRole = null;
|
||||
return "Auto-role on join is now set to disabled (Default).";
|
||||
case "welcomechannel":
|
||||
welcomeChannel = null;
|
||||
return "Welcome channel is now unset.";
|
||||
case "serverprotected":
|
||||
serverProtected = true;
|
||||
return "Server protection set to default.";
|
||||
case "modrole":
|
||||
modRole = null;
|
||||
return "Mod role is now unset.";
|
||||
case "modchannel":
|
||||
modChannel = null;
|
||||
return "Mod channel is now unset.";
|
||||
default:
|
||||
return "INVALID SETTING";
|
||||
}
|
||||
|
@ -172,18 +128,8 @@ public class Server {
|
|||
* @return - whether the name and value were valid and the option was set.
|
||||
*/
|
||||
public String setOptionByString(String name, String value) {
|
||||
modified = true; //If this is being used just tell it it's been modified.
|
||||
modified = true; // If this is being used set it to modified.
|
||||
switch (name.toLowerCase()) {
|
||||
case "welcomeenabled":
|
||||
if(value.equalsIgnoreCase("false")) {
|
||||
welcomeEnabled = false;
|
||||
return "Join/Leave logs are now disabled.";
|
||||
} else if(value.equalsIgnoreCase("true")) {
|
||||
welcomeEnabled = true;
|
||||
return "Join/Leave logs are now enabled. Make sure you set the `welcomeChannel` setting.";
|
||||
} else {
|
||||
return "Bad Value, use either true or false";
|
||||
}
|
||||
case "joinrole":
|
||||
try {
|
||||
if (Kiafumi.JDA.getRoleById(value) == null) {
|
||||
|
@ -206,23 +152,13 @@ public class Server {
|
|||
} catch (Exception ex) {
|
||||
return "Bad Value";
|
||||
}
|
||||
case "serverprotected":
|
||||
if(value.equalsIgnoreCase("false")) {
|
||||
serverProtected = false;
|
||||
return "Server protection is now disabled.";
|
||||
} else if(value.equalsIgnoreCase("true")) {
|
||||
serverProtected = true;
|
||||
return "Server protection is now enabled.";
|
||||
} else {
|
||||
return "Bad Value, use either true or false";
|
||||
}
|
||||
case "modrole":
|
||||
case "modchannel":
|
||||
try {
|
||||
if(Kiafumi.JDA.getRoleById(value) == null) {
|
||||
return "That role ID is invalid.";
|
||||
} else {
|
||||
modRole = value;
|
||||
return "Successfully set modRole ID to " + modRole;
|
||||
modChannel = value;
|
||||
return "Successfully set modChannel ID to " + modChannel;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
return "Bad Value";
|
||||
|
@ -234,7 +170,7 @@ public class Server {
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Server{id=" + this.id + ",welcomeEnabled=" + welcomeEnabled + ",welcomeChannel="+welcomeChannel+
|
||||
",joinrole=" + joinRole +",serverProtected="+serverProtected+ ",modRole=" + modRole +"}";
|
||||
return "Server [id=" + this.id + ",welcomeChannel="+welcomeChannel+
|
||||
",joinrole=" + joinRole + ",modChannel=" + modChannel +"]";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ import java.util.Collection;
|
|||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import static moe.oko.Kiafumi.Kiafumi.*;
|
||||
import static moe.oko.Kiafumi.util.LoggingManager.*;
|
||||
|
||||
/**
|
||||
* ServerManager Class
|
||||
|
|
48
src/main/java/moe/oko/Kiafumi/util/LoggingManager.java
Normal file
48
src/main/java/moe/oko/Kiafumi/util/LoggingManager.java
Normal file
|
@ -0,0 +1,48 @@
|
|||
package moe.oko.Kiafumi.util;
|
||||
|
||||
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Logging Class
|
||||
* Provides static logging & extensible template messages.
|
||||
* @author oko
|
||||
*/
|
||||
public class LoggingManager {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger("Kiafumi");
|
||||
|
||||
// Static logging reference
|
||||
public static void debug(String str) { logger.debug(str); }
|
||||
public static void info(String str) { logger.info(str); }
|
||||
public static void warn(String str) { logger.warn(str); }
|
||||
public static void error(String str) { logger.error(str); }
|
||||
|
||||
/**
|
||||
* Used for logging commands with ease to console via a static method.
|
||||
* @param event - the event ran
|
||||
* @param msg - Any message to append with this.
|
||||
*/
|
||||
public static void slashLog(SlashCommandInteractionEvent event, @Nullable String msg) {
|
||||
var user = event.getUser();
|
||||
info("""
|
||||
User [%s:%s] ran command: "%s" %s"""
|
||||
.formatted(user.getName(), user.getId(), event.getName(), msg == null ? "" : msg));
|
||||
}
|
||||
|
||||
public static void slashLog(SlashCommandInteractionEvent event) {
|
||||
var user = event.getUser();
|
||||
info("""
|
||||
User [%s:%s] ran command: "%s"."""
|
||||
.formatted(user.getName(), user.getId(), event.getName()));
|
||||
}
|
||||
|
||||
public static void slashResponse(SlashCommandInteractionEvent event, String msg) {
|
||||
var user = event.getUser();
|
||||
info("""
|
||||
User [%s:%s] was provided response: "%s\"""".formatted(user.getName(), user.getId(), msg));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue