I gotta start committing more consistently
This commit is contained in:
56
src/main/java/net/nevet5gi/buzzbot/Bot.java
Normal file
56
src/main/java/net/nevet5gi/buzzbot/Bot.java
Normal file
@@ -0,0 +1,56 @@
|
||||
package net.nevet5gi.buzzbot;
|
||||
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.JDABuilder;
|
||||
import net.dv8tion.jda.api.entities.Activity;
|
||||
import net.dv8tion.jda.api.utils.cache.CacheFlag;
|
||||
import net.nevet5gi.buzzbot.commands.utils.CommandManager;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.security.auth.login.LoginException;
|
||||
import java.util.EnumSet;
|
||||
|
||||
public class Bot {
|
||||
public static JDA jda;
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Bot.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
Config.loadConfig();
|
||||
|
||||
try {
|
||||
jda = JDABuilder.createDefault(Config.getConfig().getString("bot.token"))
|
||||
.disableCache(EnumSet.of(
|
||||
CacheFlag.CLIENT_STATUS,
|
||||
CacheFlag.ACTIVITY,
|
||||
CacheFlag.EMOTE
|
||||
))
|
||||
.enableCache(CacheFlag.VOICE_STATE)
|
||||
.addEventListeners(new Listener())
|
||||
.build();
|
||||
CommandManager.registerSlashCommands();
|
||||
getActivity();
|
||||
} catch (LoginException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Listener.startProfanityFilter();
|
||||
}
|
||||
|
||||
public static void getActivity() {
|
||||
if (Config.getConfig().getString("bot.activity").equalsIgnoreCase("playing")) {
|
||||
jda.getPresence().setActivity(Activity.playing(Config.getConfig().getString("bot.action")));
|
||||
} else if (Config.getConfig().getString("bot.activity").equalsIgnoreCase("watching")) {
|
||||
jda.getPresence().setActivity(Activity.watching(Config.getConfig().getString("bot.action")));
|
||||
} else if (Config.getConfig().getString("bot.activity").equalsIgnoreCase("competing")) {
|
||||
jda.getPresence().setActivity(Activity.competing(Config.getConfig().getString("bot.action")));
|
||||
} else if (Config.getConfig().getString("bot.activity").equalsIgnoreCase("listening")) {
|
||||
jda.getPresence().setActivity(Activity.listening(Config.getConfig().getString("bot.action")));
|
||||
} else if (Config.getConfig().getString("bot.activity").equalsIgnoreCase("debug")) {
|
||||
jda.getPresence().setActivity(Activity.listening("my prefix " + Config.getConfig().getString("bot.prefix")));
|
||||
} else {
|
||||
jda.getPresence().setActivity(Activity.playing("with myself!"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
53
src/main/java/net/nevet5gi/buzzbot/Config.java
Normal file
53
src/main/java/net/nevet5gi/buzzbot/Config.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package net.nevet5gi.buzzbot;
|
||||
|
||||
import org.simpleyaml.configuration.file.YamlFile;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class Config {
|
||||
private static final YamlFile YML_FILE = new YamlFile("./config.yml");
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Config.class);
|
||||
|
||||
public static void loadConfig() {
|
||||
YML_FILE.setConfigurationFile("./config.yml");
|
||||
try {
|
||||
if (!YML_FILE.exists()) {
|
||||
LOGGER.info("Config file not found, creating new one...");
|
||||
YML_FILE.createNewFile(true);
|
||||
LOGGER.info("Config file created!");
|
||||
} else {
|
||||
LOGGER.info("Loading Config file...");
|
||||
YML_FILE.loadWithComments();
|
||||
LOGGER.info("Config file loaded!");
|
||||
}
|
||||
} catch (final Exception e) {
|
||||
LOGGER.error("Error while loading config file!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
YML_FILE.addDefault("bot.token", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
|
||||
YML_FILE.addDefault("bot.prefix", "!");
|
||||
YML_FILE.addDefault("bot.activity", "playing");
|
||||
YML_FILE.addDefault("bot.action", "with myself");
|
||||
YML_FILE.addDefault("database.url", "localhost");
|
||||
YML_FILE.addDefault("database.database", "buzzbot");
|
||||
YML_FILE.addDefault("database.user", "barry");
|
||||
YML_FILE.addDefault("database.password", "benson");
|
||||
YML_FILE.addDefault("filter.path.mild", "./mild.txt");
|
||||
YML_FILE.addDefault("filter.path.moderate", "./moderate.txt");
|
||||
YML_FILE.addDefault("filter.path.strong", "./strong.txt");
|
||||
|
||||
try {
|
||||
YML_FILE.save();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static YamlFile getConfig() {
|
||||
return YML_FILE;
|
||||
}
|
||||
}
|
||||
5
src/main/java/net/nevet5gi/buzzbot/Group.java
Normal file
5
src/main/java/net/nevet5gi/buzzbot/Group.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package net.nevet5gi.buzzbot;
|
||||
|
||||
public class Group {
|
||||
|
||||
}
|
||||
83
src/main/java/net/nevet5gi/buzzbot/Listener.java
Normal file
83
src/main/java/net/nevet5gi/buzzbot/Listener.java
Normal file
@@ -0,0 +1,83 @@
|
||||
package net.nevet5gi.buzzbot;
|
||||
|
||||
import net.dv8tion.jda.api.entities.User;
|
||||
import net.dv8tion.jda.api.events.ReadyEvent;
|
||||
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
import net.nevet5gi.buzzbot.commands.utils.CommandManager;
|
||||
import net.nevet5gi.buzzbot.database.SqlDB;
|
||||
import net.nevet5gi.buzzbot.functions.ProfanityFilter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public class Listener extends ListenerAdapter {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(Listener.class);
|
||||
private final CommandManager manager = new CommandManager();
|
||||
private static final ProfanityFilter profanityFilter = new ProfanityFilter();
|
||||
|
||||
public static void startProfanityFilter() {
|
||||
profanityFilter.buildDictionaries();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReady(@Nonnull ReadyEvent event) {
|
||||
LOGGER.info("BuzzBot is ready: " + event.getJDA().getSelfUser().getAsTag());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGuildMessageReceived(@Nonnull GuildMessageReceivedEvent event) {
|
||||
User user = event.getAuthor();
|
||||
String raw = event.getMessage().getContentRaw();
|
||||
|
||||
if (user.isBot() || event.isWebhookMessage()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (profanityFilter.containsProfanity(raw, "strong")) {
|
||||
SqlDB sql = new SqlDB();
|
||||
int profanityLevel = sql.getGuildData(event.getGuild().getIdLong()).getProfanityLevel();
|
||||
|
||||
switch (profanityLevel) {
|
||||
case 1:
|
||||
if (profanityFilter.containsProfanity(raw, "mild")) profanityFilter.handle(event);
|
||||
case 2:
|
||||
if (profanityFilter.containsProfanity(raw, "moderate")) profanityFilter.handle(event);
|
||||
case 3:
|
||||
profanityFilter.handle(event);
|
||||
}
|
||||
}
|
||||
|
||||
if (raw.startsWith(Config.getConfig().getString("bot.prefix"))) {
|
||||
manager.handle(event);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGuildMessageUpdate(@Nonnull GuildMessageUpdateEvent event) {
|
||||
String raw = event.getMessage().getContentRaw();
|
||||
|
||||
if (profanityFilter.containsProfanity(raw, "strong")) {
|
||||
SqlDB sql = new SqlDB();
|
||||
int profanityLevel = sql.getGuildData(event.getGuild().getIdLong()).getProfanityLevel();
|
||||
|
||||
switch (profanityLevel) {
|
||||
case 1:
|
||||
if (profanityFilter.containsProfanity(raw, "mild")) profanityFilter.handleEdit(event);
|
||||
case 2:
|
||||
if (profanityFilter.containsProfanity(raw, "moderate")) profanityFilter.handleEdit(event);
|
||||
case 3:
|
||||
profanityFilter.handleEdit(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSlashCommand(@NotNull SlashCommandEvent event) {
|
||||
manager.handle(event);
|
||||
}
|
||||
}
|
||||
64
src/main/java/net/nevet5gi/buzzbot/Test.java
Normal file
64
src/main/java/net/nevet5gi/buzzbot/Test.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package net.nevet5gi.buzzbot;
|
||||
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.dv8tion.jda.api.JDABuilder;
|
||||
import net.dv8tion.jda.api.utils.cache.CacheFlag;
|
||||
import net.nevet5gi.buzzbot.database.SqlDB;
|
||||
import net.nevet5gi.buzzbot.objects.BanData;
|
||||
import net.nevet5gi.buzzbot.objects.GuildData;
|
||||
|
||||
import javax.security.auth.login.LoginException;
|
||||
import java.sql.Date;
|
||||
import java.sql.Time;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalTime;
|
||||
import java.util.EnumSet;
|
||||
|
||||
public class Test {
|
||||
private static JDA jda;
|
||||
public static void main(String[] args) {
|
||||
Config.loadConfig();
|
||||
|
||||
SqlDB sql = new SqlDB();
|
||||
GuildData guild = new GuildData();
|
||||
|
||||
guild.setName("DevHQ");
|
||||
guild.setId(824071914673668138L);
|
||||
guild.setGroup("developer");
|
||||
guild.setProfanityLevel(3);
|
||||
|
||||
sql.addGuild(guild);
|
||||
|
||||
//sqlTest();
|
||||
}
|
||||
|
||||
private static void initJda() {
|
||||
try {
|
||||
jda = JDABuilder.createDefault(Config.getConfig().getString("bot.token"))
|
||||
.disableCache(EnumSet.of(
|
||||
CacheFlag.CLIENT_STATUS,
|
||||
CacheFlag.ACTIVITY,
|
||||
CacheFlag.EMOTE
|
||||
))
|
||||
.enableCache(CacheFlag.VOICE_STATE)
|
||||
.addEventListeners(new Listener())
|
||||
.build();
|
||||
} catch (LoginException e) {
|
||||
e.printStackTrace();
|
||||
jda = null;
|
||||
}
|
||||
}
|
||||
|
||||
private static void sqlTest() {
|
||||
SqlDB db = new SqlDB();
|
||||
BanData ban = new BanData(972924565361695745L, "nevetS", 3866, Date.valueOf(LocalDate.now()), Time.valueOf(LocalTime.now()), true, 0, "Reason", "test3", 865368792980914186L, "DevHQ", 824071914673668138L);
|
||||
db.insertBan(ban, "masterbanlist");
|
||||
|
||||
SqlDB db2 = new SqlDB();
|
||||
BanData qban = db2.queryBan(972924565361695745L, "masterbanlist");
|
||||
|
||||
System.out.println(qban.getUserName());
|
||||
System.out.println(qban.getDate());
|
||||
System.out.println(qban.getBanReason());
|
||||
}
|
||||
}
|
||||
92
src/main/java/net/nevet5gi/buzzbot/commands/BanCmd.java
Normal file
92
src/main/java/net/nevet5gi/buzzbot/commands/BanCmd.java
Normal file
@@ -0,0 +1,92 @@
|
||||
package net.nevet5gi.buzzbot.commands;
|
||||
|
||||
import net.nevet5gi.buzzbot.Bot;
|
||||
import net.nevet5gi.buzzbot.commands.utils.CommandContext;
|
||||
import net.nevet5gi.buzzbot.commands.utils.ICommand;
|
||||
import net.nevet5gi.buzzbot.objects.BanData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class BanCmd implements ICommand {
|
||||
@Override
|
||||
public void handle(CommandContext ctx) {
|
||||
//TODO Add checks for commands so they dont break the bot
|
||||
//TODO Get username and discriminator of person banned and mod
|
||||
//TODO DM Person who was banned with info about ban
|
||||
//TODO Make bans able to be modified by original banner only
|
||||
|
||||
List<String> args = ctx.getArgs();
|
||||
|
||||
if (args.size() < 3) {
|
||||
ctx.getChannel().sendMessage("Not enough arguments, please try again").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
BanData ban = new BanData();
|
||||
|
||||
if (args.get(0).contains("<@")) {
|
||||
ban.setUserId(Long.parseLong(args.get(0).replace("<","").replace("@","").replace("&","").replace(">","")));
|
||||
} else {
|
||||
ban.setUserId(Long.parseLong(args.get(0)));
|
||||
}
|
||||
|
||||
if (args.get(1).contains("perm")) {
|
||||
ban.setBanLength(0);
|
||||
ban.setBanType(true);
|
||||
} else if (args.get(1).contains("h")) {
|
||||
ban.setBanLength(Integer.parseInt(args.get(1).replace("h","")));
|
||||
ban.setBanType(false);
|
||||
} else if (args.get(1).contains("d")) {
|
||||
ban.setBanLength(Integer.parseInt(args.get(1).replace("d","")) * 24);
|
||||
ban.setBanType(false);
|
||||
} else if (args.get(1).contains("w")) {
|
||||
ban.setBanLength(Integer.parseInt(args.get(1).replace("w","")) * 24 * 7);
|
||||
ban.setBanType(false);
|
||||
} else if (args.get(1).contains("m")) {
|
||||
ban.setBanLength(Integer.parseInt(args.get(1).replace("m", "")) * 24 * 30);
|
||||
ban.setBanType(false);
|
||||
} else if (args.get(1).contains("y")) {
|
||||
ban.setBanLength(Integer.parseInt(args.get(1).replace("y","")) * 24 * 365);
|
||||
ban.setBanType(false);
|
||||
} else {
|
||||
ctx.getChannel().sendMessage("Invalid time argument, please try again").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (args.size() >= 2) {
|
||||
for (int i = 2; i < args.size(); i++) {
|
||||
sb.append(args.get(i));
|
||||
sb.append(" ");
|
||||
}
|
||||
}
|
||||
ban.setBanReason(sb.toString().trim());
|
||||
|
||||
if (ban.getBanLength() == 0) {
|
||||
ctx.getChannel().sendMessage("<@" + ctx.getMessage().getAuthor().getId() + "> permanently banned <@" + ban.getUserId() + ">. Reason: " + ban.getBanReason()).queue();
|
||||
} else {
|
||||
ctx.getChannel().sendMessage("<@" + ctx.getMessage().getAuthor().getId() + "> banned <@" + ban.getUserId() + "> for " + ban.getBanLength() + " hours. Reason: " + ban.getBanReason()).queue();
|
||||
}
|
||||
|
||||
Bot.jda.retrieveUserById(ban.getUserId()).queue(user -> { ban.setUserName(user.getName()); });
|
||||
Bot.jda.retrieveUserById(ban.getUserId()).queue(user -> { ban.setUserDiscriminator(Integer.parseInt(user.getDiscriminator())); });
|
||||
ban.setModId(ctx.getMessage().getAuthor().getIdLong());
|
||||
ban.setModName(ctx.getMessage().getAuthor().getName());
|
||||
ban.setServerId(ctx.getGuild().getIdLong());
|
||||
ban.setServerName(ctx.getGuild().getName());
|
||||
|
||||
|
||||
//ctx.getEvent().getGuild().ban(cmdf, 1 , "").submit();
|
||||
//ctx.getMessage().reply("yes ban");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "ban";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelp() {
|
||||
return "Bans the specified user";
|
||||
}
|
||||
}
|
||||
124
src/main/java/net/nevet5gi/buzzbot/commands/HelpCmd.java
Normal file
124
src/main/java/net/nevet5gi/buzzbot/commands/HelpCmd.java
Normal file
@@ -0,0 +1,124 @@
|
||||
package net.nevet5gi.buzzbot.commands;
|
||||
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.dv8tion.jda.api.entities.TextChannel;
|
||||
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
|
||||
import net.dv8tion.jda.api.interactions.commands.OptionType;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
||||
import net.nevet5gi.buzzbot.Config;
|
||||
import net.nevet5gi.buzzbot.commands.utils.*;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class HelpCmd implements ICommand, ISlashCommand {
|
||||
|
||||
private final CommandManager manager;
|
||||
|
||||
public HelpCmd(CommandManager manager) {
|
||||
this.manager = manager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(CommandContext ctx) {
|
||||
List<String> args = ctx.getArgs();
|
||||
TextChannel channel = ctx.getChannel();
|
||||
|
||||
if (args.isEmpty()) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
EmbedBuilder eb = new EmbedBuilder();
|
||||
|
||||
eb.setTitle("List of Commands");
|
||||
|
||||
manager.getCommands().stream().map(ICmdGeneric::getName).forEach(
|
||||
(it) -> sb.append('`').append(Config.getConfig().getString("bot.prefix")).append(it).append("`\n")
|
||||
);
|
||||
|
||||
eb.addField("", sb.toString(), false);
|
||||
channel.sendMessageEmbeds(eb.build()).queue();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
String search = args.get(0);
|
||||
ICommand command = manager.getCommand(search);
|
||||
ISlashCommand slashCommand = manager.getSlashCommand(search);
|
||||
|
||||
if (command == null && slashCommand == null) {
|
||||
channel.sendMessage("Nothing found for " + search).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
channel.sendMessage(command.getHelp()).queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleSlash(CommandContext ctx) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
EmbedBuilder eb = new EmbedBuilder();
|
||||
|
||||
OptionMapping option = ctx.getSlashEvent().getOption("command");
|
||||
String cmd = null;
|
||||
if (option != null) cmd = option.getAsString();
|
||||
|
||||
if (cmd == null) {
|
||||
ICommand command = manager.getCommand(ctx.getSlashEvent().getName());
|
||||
ISlashCommand slashCommand = manager.getSlashCommand(ctx.getSlashEvent().getName());
|
||||
|
||||
if (command == null && slashCommand == null) {
|
||||
ctx.getSlashEvent().getHook().sendMessage("Nothing found for " + ctx.getSlashEvent()).queue();
|
||||
return;
|
||||
}
|
||||
|
||||
eb.setTitle("Commands");
|
||||
|
||||
manager.getCommands().stream().map(ICmdGeneric::getName).forEach(
|
||||
(it) -> sb.append('`').append(Config.getConfig().getString("bot.prefix")).append(it).append("`\n")
|
||||
);
|
||||
|
||||
eb.addField("", sb.toString(), false);
|
||||
|
||||
} else {
|
||||
ICommand scmd = manager.getCommand(cmd);
|
||||
|
||||
if (scmd != null) {
|
||||
eb.setTitle(scmd.getName());
|
||||
eb.addField("", scmd.getHelp(), false);
|
||||
} else {
|
||||
eb.setTitle("Error");
|
||||
eb.addField("", "Unknown command, run `/help` to see a list of available commands", false);
|
||||
}
|
||||
}
|
||||
|
||||
ctx.getSlashEvent().getHook().sendMessageEmbeds(eb.build()).queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "help";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Lists bot commands";
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull CommandData getCommandData() {
|
||||
|
||||
return new CommandData(this.getName(), this.getDescription())
|
||||
.addOption(OptionType.STRING, "command", "Gives info on this command", false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelp() {
|
||||
return "Shows the list of bot commands\n" +
|
||||
"Usage: `" + Config.getConfig().getString("bot.prefix") + "help [command]`";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getAliases() {
|
||||
return List.of("commands", "cmds", "commandlist");
|
||||
}
|
||||
}
|
||||
21
src/main/java/net/nevet5gi/buzzbot/commands/MuteCmd.java
Normal file
21
src/main/java/net/nevet5gi/buzzbot/commands/MuteCmd.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package net.nevet5gi.buzzbot.commands;
|
||||
|
||||
import net.nevet5gi.buzzbot.commands.utils.CommandContext;
|
||||
import net.nevet5gi.buzzbot.commands.utils.ICommand;
|
||||
|
||||
public class MuteCmd implements ICommand {
|
||||
@Override
|
||||
public void handle(CommandContext ctx) {
|
||||
ctx.getMessage().reply("This command has not been implemented yet").queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "mute";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelp() {
|
||||
return "Mutes the specified user";
|
||||
}
|
||||
}
|
||||
64
src/main/java/net/nevet5gi/buzzbot/commands/PandaCmd.java
Normal file
64
src/main/java/net/nevet5gi/buzzbot/commands/PandaCmd.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package net.nevet5gi.buzzbot.commands;
|
||||
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import net.nevet5gi.buzzbot.Config;
|
||||
import net.nevet5gi.buzzbot.commands.utils.CommandContext;
|
||||
import net.nevet5gi.buzzbot.commands.utils.ICommand;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
|
||||
public class PandaCmd implements ICommand {
|
||||
public static String url;
|
||||
@Override
|
||||
public void handle(CommandContext ctx) {
|
||||
try { getHttpConnection(); } catch (IOException | InterruptedException e) { e.printStackTrace(); }
|
||||
EmbedBuilder eb = new EmbedBuilder();
|
||||
ctx.getChannel().sendTyping().queue();
|
||||
eb.setImage(url.replace("\"",""));
|
||||
ctx.getChannel().sendMessage(eb.build()).queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "panda";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelp() {
|
||||
return "Sends a picture of a panda!\n" +
|
||||
"Usage: `" + Config.getConfig().getString("bot.prefix") + "panda`";
|
||||
}
|
||||
|
||||
public static void getHttpConnection() throws IOException, InterruptedException {
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.GET()
|
||||
.header("accept", "application/json")
|
||||
.uri(URI.create("https://some-random-api.ml/img/panda"))
|
||||
.build();
|
||||
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
|
||||
.thenApply(HttpResponse::body)
|
||||
.thenApply(PandaCmd::parse)
|
||||
.join();
|
||||
}
|
||||
|
||||
public static String parse(String response) {
|
||||
String mod = "[ " + response + " ]";
|
||||
JsonArray ja = new Gson().fromJson(mod, JsonArray.class);
|
||||
|
||||
for (int i = 0; i < ja.getAsJsonArray().size(); i++) {
|
||||
JsonElement jo = ja.get(i);
|
||||
url = jo.getAsJsonObject().get("link").toString();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
27
src/main/java/net/nevet5gi/buzzbot/commands/PingCmd.java
Normal file
27
src/main/java/net/nevet5gi/buzzbot/commands/PingCmd.java
Normal file
@@ -0,0 +1,27 @@
|
||||
package net.nevet5gi.buzzbot.commands;
|
||||
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
import net.nevet5gi.buzzbot.Config;
|
||||
import net.nevet5gi.buzzbot.commands.utils.CommandContext;
|
||||
import net.nevet5gi.buzzbot.commands.utils.ICommand;
|
||||
|
||||
public class PingCmd implements ICommand {
|
||||
@Override
|
||||
public void handle(CommandContext ctx) {
|
||||
JDA jda = ctx.getJDA();
|
||||
|
||||
jda.getRestPing().queue((ping) -> ctx.getChannel().sendMessageFormat("Rest API Ping: %sms\nWebSocket Ping: %sms", ping, jda.getGatewayPing()).queue());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelp() {
|
||||
return "Shows the current ping from the bot to the Discord servers" +
|
||||
"Usage: `" + Config.getConfig().getString("bot.prefix") + "ping`";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "ping";
|
||||
}
|
||||
}
|
||||
50
src/main/java/net/nevet5gi/buzzbot/commands/TestCmd.java
Normal file
50
src/main/java/net/nevet5gi/buzzbot/commands/TestCmd.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package net.nevet5gi.buzzbot.commands;
|
||||
|
||||
import net.dv8tion.jda.api.entities.IMentionable;
|
||||
import net.dv8tion.jda.api.interactions.commands.OptionType;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
||||
import net.nevet5gi.buzzbot.commands.utils.CommandContext;
|
||||
import net.nevet5gi.buzzbot.commands.utils.ICommand;
|
||||
import net.nevet5gi.buzzbot.commands.utils.ISlashCommand;
|
||||
|
||||
public class TestCmd implements ICommand, ISlashCommand {
|
||||
@Override
|
||||
public void handle(CommandContext ctx) {
|
||||
ctx.getMessage().reply("test command").queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleSlash(CommandContext ctx) {
|
||||
long number = ctx.getSlashEvent().getOption("number").getAsLong();
|
||||
IMentionable member = ctx.getSlashEvent().getOption("mention").getAsMentionable();
|
||||
boolean testBoolean = ctx.getSlashEvent().getOption("boolean").getAsBoolean();
|
||||
|
||||
String content = "number: " + number + ", mention: " + member.getAsMention() + ", boolean: " + testBoolean;
|
||||
|
||||
ctx.getSlashEvent().getHook().sendMessage(content).queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Checks the piung between bot and API";
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandData getCommandData() {
|
||||
return new CommandData(this.getName(), this.getDescription())
|
||||
.addOption(OptionType.INTEGER, "number", "test number", true)
|
||||
.addOption(OptionType.MENTIONABLE, "mention", "mentioned member or role", true)
|
||||
.addOption(OptionType.BOOLEAN, "boolean", "test boolean", true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "test";
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getHelp() {
|
||||
return "tests api ping";
|
||||
}
|
||||
}
|
||||
21
src/main/java/net/nevet5gi/buzzbot/commands/UnbanCmd.java
Normal file
21
src/main/java/net/nevet5gi/buzzbot/commands/UnbanCmd.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package net.nevet5gi.buzzbot.commands;
|
||||
|
||||
import net.nevet5gi.buzzbot.commands.utils.CommandContext;
|
||||
import net.nevet5gi.buzzbot.commands.utils.ICommand;
|
||||
|
||||
public class UnbanCmd implements ICommand {
|
||||
@Override
|
||||
public void handle(CommandContext ctx) {
|
||||
ctx.getMessage().reply("This command doesn't work yet").queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "unban";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelp() {
|
||||
return "Unbans the specified user";
|
||||
}
|
||||
}
|
||||
21
src/main/java/net/nevet5gi/buzzbot/commands/UnmuteCmd.java
Normal file
21
src/main/java/net/nevet5gi/buzzbot/commands/UnmuteCmd.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package net.nevet5gi.buzzbot.commands;
|
||||
|
||||
import net.nevet5gi.buzzbot.commands.utils.CommandContext;
|
||||
import net.nevet5gi.buzzbot.commands.utils.ICommand;
|
||||
|
||||
public class UnmuteCmd implements ICommand {
|
||||
@Override
|
||||
public void handle(CommandContext ctx) {
|
||||
ctx.getMessage().reply("This command has not been implemented yet").queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "unmute";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelp() {
|
||||
return "Unmutes the specified user";
|
||||
}
|
||||
}
|
||||
22
src/main/java/net/nevet5gi/buzzbot/commands/UnwarnCmd.java
Normal file
22
src/main/java/net/nevet5gi/buzzbot/commands/UnwarnCmd.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package net.nevet5gi.buzzbot.commands;
|
||||
|
||||
import net.nevet5gi.buzzbot.commands.utils.CommandContext;
|
||||
import net.nevet5gi.buzzbot.commands.utils.ICommand;
|
||||
|
||||
public class UnwarnCmd implements ICommand {
|
||||
//TODO Make this command only work for warns that were given from the same server
|
||||
@Override
|
||||
public void handle(CommandContext ctx) {
|
||||
ctx.getMessage().reply("This command has not been implemented yet").queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "unwarn";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelp() {
|
||||
return "Unwarns the specified user";
|
||||
}
|
||||
}
|
||||
38
src/main/java/net/nevet5gi/buzzbot/commands/WarnCmd.java
Normal file
38
src/main/java/net/nevet5gi/buzzbot/commands/WarnCmd.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package net.nevet5gi.buzzbot.commands;
|
||||
|
||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
||||
import net.nevet5gi.buzzbot.commands.utils.CommandContext;
|
||||
import net.nevet5gi.buzzbot.commands.utils.ICommand;
|
||||
import net.nevet5gi.buzzbot.commands.utils.ISlashCommand;
|
||||
|
||||
public class WarnCmd implements ICommand, ISlashCommand {
|
||||
@Override
|
||||
public void handle(CommandContext ctx) {
|
||||
ctx.getMessage().reply("This command has not been implemented yet").queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleSlash(CommandContext ctx) {
|
||||
ctx.getSlashEvent().getHook().sendMessage("This command has not been implemented yet").queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "warn";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Warns specified user";
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandData getCommandData() {
|
||||
return new CommandData(this.getName(), this.getDescription());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHelp() {
|
||||
return "Warns the specified user";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package net.nevet5gi.buzzbot.commands.utils;
|
||||
|
||||
import me.duncte123.botcommons.commands.ICommandContext;
|
||||
import net.dv8tion.jda.api.entities.Guild;
|
||||
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CommandContext implements ICommandContext {
|
||||
private GuildMessageReceivedEvent event;
|
||||
private SlashCommandEvent slashEvent;
|
||||
private final List<String> args;
|
||||
|
||||
public CommandContext(GuildMessageReceivedEvent event, List<String> args) {
|
||||
this.event = event;
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
public CommandContext(SlashCommandEvent slashEvent, List<String> args) {
|
||||
this.slashEvent = slashEvent;
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Guild getGuild() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GuildMessageReceivedEvent getEvent() {
|
||||
return this.event;
|
||||
}
|
||||
|
||||
public SlashCommandEvent getSlashEvent() {
|
||||
return this.slashEvent;
|
||||
}
|
||||
|
||||
public List<String> getArgs() {
|
||||
return this.args;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package net.nevet5gi.buzzbot.commands.utils;
|
||||
|
||||
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
||||
import net.nevet5gi.buzzbot.Bot;
|
||||
import net.nevet5gi.buzzbot.Config;
|
||||
import net.nevet5gi.buzzbot.commands.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class CommandManager {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CommandManager.class);
|
||||
protected final List<ICommand> commands = new ArrayList<>();
|
||||
protected static List<ISlashCommand> slashList = new ArrayList<>();
|
||||
|
||||
public CommandManager() {
|
||||
//Add to this list in alphabetical order
|
||||
addCommand(new BanCmd());
|
||||
addCommand(new HelpCmd(this));
|
||||
addCommand(new MuteCmd());
|
||||
addCommand(new PandaCmd());
|
||||
addCommand(new PingCmd());
|
||||
addCommand(new TestCmd());
|
||||
addCommand(new UnbanCmd());
|
||||
addCommand(new UnmuteCmd());
|
||||
addCommand(new UnwarnCmd());
|
||||
addCommand(new WarnCmd());
|
||||
//addCommand(new CommandClass());
|
||||
}
|
||||
|
||||
private void addCommand(ICmdGeneric cmd) {
|
||||
boolean nameFound = commands.stream().anyMatch((it) -> it.getName().equalsIgnoreCase(cmd.getName()));
|
||||
if (nameFound) {
|
||||
throw new IllegalArgumentException("A command with this name is already present");
|
||||
}
|
||||
|
||||
if (cmd instanceof ICommand) {
|
||||
commands.add((ICommand) cmd);
|
||||
}
|
||||
|
||||
if (cmd instanceof ISlashCommand) {
|
||||
slashList.add(((ISlashCommand) cmd));
|
||||
}
|
||||
}
|
||||
|
||||
public static void registerSlashCommands() {
|
||||
if (slashList != null) {
|
||||
List<CommandData> cmdDataList = new ArrayList<>();
|
||||
for (ISlashCommand slashCmd : slashList) {
|
||||
cmdDataList.add(slashCmd.getCommandData());
|
||||
}
|
||||
Bot.jda.updateCommands().addCommands(cmdDataList).queue();
|
||||
}
|
||||
}
|
||||
|
||||
public List<ICommand> getCommands() {
|
||||
return commands;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public ICommand getCommand(String search) {
|
||||
String searchLower = search.toLowerCase();
|
||||
|
||||
for (ICommand cmd : commands) {
|
||||
if (cmd.getName().equals(searchLower) || cmd.getAliases().contains(searchLower)) {
|
||||
return cmd;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public ISlashCommand getSlashCommand(String search) {
|
||||
String searchLower = search.toLowerCase();
|
||||
|
||||
for (ISlashCommand scmd : slashList) {
|
||||
if (scmd.getName().equals(searchLower) || scmd.getAliases().contains(searchLower)) {
|
||||
return scmd;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void handle(GuildMessageReceivedEvent event) {
|
||||
String[] split = event.getMessage().getContentRaw()
|
||||
.replaceFirst("(?i)" + Pattern.quote(Config.getConfig().getString("bot.prefix")), "")
|
||||
.split("\\s+");
|
||||
|
||||
String invoke = split[0].toLowerCase();
|
||||
ICommand cmd = getCommand(invoke);
|
||||
|
||||
if (cmd != null) {
|
||||
event.getChannel().sendTyping().queue();
|
||||
List<String> args = Arrays.asList(split).subList(1, split.length);
|
||||
|
||||
CommandContext ctx = new CommandContext(event, args);
|
||||
|
||||
cmd.handle(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
public void handle(SlashCommandEvent event) {
|
||||
String invoke = event.getName();
|
||||
ISlashCommand cmd = getSlashCommand(invoke);
|
||||
|
||||
if (cmd != null) {
|
||||
event.deferReply().queue();
|
||||
List<String> args = new ArrayList<>();
|
||||
|
||||
CommandContext ctx = new CommandContext(event, args);
|
||||
|
||||
cmd.handleSlash(ctx);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package net.nevet5gi.buzzbot.commands.utils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface ICmdGeneric {
|
||||
|
||||
String getName();
|
||||
|
||||
String getHelp();
|
||||
|
||||
default List<String> getAliases(){
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package net.nevet5gi.buzzbot.commands.utils;
|
||||
|
||||
public interface ICommand extends ICmdGeneric {
|
||||
|
||||
void handle(CommandContext ctx);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package net.nevet5gi.buzzbot.commands.utils;
|
||||
|
||||
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
|
||||
|
||||
public interface ISlashCommand extends ICmdGeneric {
|
||||
void handleSlash(CommandContext ctx);
|
||||
|
||||
String getDescription();
|
||||
|
||||
CommandData getCommandData();
|
||||
}
|
||||
169
src/main/java/net/nevet5gi/buzzbot/database/SqlDB.java
Normal file
169
src/main/java/net/nevet5gi/buzzbot/database/SqlDB.java
Normal file
@@ -0,0 +1,169 @@
|
||||
package net.nevet5gi.buzzbot.database;
|
||||
|
||||
import net.nevet5gi.buzzbot.Config;
|
||||
import net.nevet5gi.buzzbot.objects.BanData;
|
||||
import net.nevet5gi.buzzbot.objects.GuildData;
|
||||
import net.nevet5gi.buzzbot.objects.MuteData;
|
||||
import net.nevet5gi.buzzbot.objects.WarnData;
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
public class SqlDB {
|
||||
//TODO Make this a two way class for reading and writing from db
|
||||
|
||||
private Connection connect;
|
||||
private Statement statement;
|
||||
private ResultSet resultSet;
|
||||
|
||||
public SqlDB() {
|
||||
try {
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
connect = DriverManager.getConnection("jdbc:mysql://" + Config.getConfig().getString("database.url") + "/" + Config.getConfig().getString("database.database"), Config.getConfig().getString("database.user"), Config.getConfig().getString("database.password"));
|
||||
statement = connect.createStatement();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void insertBan(BanData ban, String table) {
|
||||
try {
|
||||
statement.executeUpdate("INSERT INTO " + table + " VALUES (default, " + ban.getUserId() + ", '" + ban.getUserName() + "', '" + ban.getUserDiscriminator() + "', '" + ban.getDate() + "', '" + ban.getTime() + "', " + ban.getBanType() + ", " + ban.getBanLength() + ", '" + ban.getBanReason() + "', '" + ban.getModName() + "', " + ban.getModId() + ", '" + ban.getServerName() + "', " + ban.getServerId() + ")");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
close();
|
||||
}
|
||||
|
||||
public void insertMute(MuteData mute, String table) {
|
||||
try {
|
||||
statement.executeUpdate("INSERT INTO " + table + " VALUES (default, " + mute.getUserId() + ", '" + mute.getUserName() + "', '" + mute.getUserDiscriminator() + "', '" + mute.getDate() + "', '" + mute.getTime() + "', " + mute.getMuteLength() + ", '" + mute.getMuteReason() + "', '" + mute.getModName() + "', " + mute.getModId() + ", '" + mute.getServerName() + "', " + mute.getServerId() + ")");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void insertWarn(WarnData warn, String table) {
|
||||
try {
|
||||
statement.executeUpdate("INSERT INTO " + table + " VALUES (default, " + warn.getUserId() + ", '" + warn.getUserName() + "', '" + warn.getUserDiscriminator() + "', '" + warn.getDate() + "', '" + warn.getTime() + "', " + warn.getBanType() + ", " + warn.getBanLength() + ", '" + warn.getBanReason() + "', '" + warn.getModName() + "', " + warn.getModId() + ", '" + warn.getServerName() + "', " + warn.getServerId() + ")");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public BanData queryBan(long userId, String table) {
|
||||
BanData ban = new BanData();
|
||||
|
||||
try {
|
||||
//TODO Make this use GuildUtils in order to get the proper group for the server
|
||||
|
||||
resultSet = statement.executeQuery("SELECT * FROM " + table + " WHERE userId=" + userId);
|
||||
|
||||
while (resultSet.next()) {
|
||||
ban.setUserId(resultSet.getLong("userid"));
|
||||
ban.setUserName(resultSet.getString("username"));
|
||||
ban.setUserDiscriminator(resultSet.getInt("userdiscriminator"));
|
||||
ban.setDate(resultSet.getDate("bandate"));
|
||||
ban.setTime(resultSet.getTime("bantime"));
|
||||
ban.setBanType(resultSet.getBoolean("bantype"));
|
||||
ban.setBanLength(resultSet.getInt("banlength"));
|
||||
ban.setBanReason(resultSet.getString("banreason"));
|
||||
ban.setModName(resultSet.getString("modname"));
|
||||
ban.setModId(resultSet.getLong("modid"));
|
||||
ban.setServerName(resultSet.getString("servername"));
|
||||
ban.setServerId(resultSet.getLong("serverid"));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
close();
|
||||
return ban;
|
||||
}
|
||||
|
||||
public MuteData queryMute(long userId, String table) {
|
||||
MuteData mute = new MuteData();
|
||||
|
||||
try {
|
||||
resultSet = statement.executeQuery("SELECT * FROM " + table + "WHERE userId=" + userId);
|
||||
|
||||
while (resultSet.next()) {
|
||||
mute.setUserId(resultSet.getLong("userid"));
|
||||
mute.setUserName(resultSet.getString("username"));
|
||||
mute.setUserDiscriminator(resultSet.getInt("userdiscriminator"));
|
||||
//mute.setDate(resultSet.);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return mute;
|
||||
}
|
||||
|
||||
public WarnData queryWarn(long userId, String table) {
|
||||
WarnData warn = new WarnData();
|
||||
|
||||
try {
|
||||
resultSet = statement.executeQuery("SELECT * FROM " + table + "WHERE userId=" + userId);
|
||||
|
||||
while (resultSet.next()) {
|
||||
warn.setUserId(resultSet.getLong("userid"));
|
||||
warn.setUserName(resultSet.getString("username"));
|
||||
warn.setUserDiscriminator(resultSet.getInt("userdiscriminator"));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return warn;
|
||||
}
|
||||
|
||||
public void addGuild(GuildData guild) {
|
||||
try {
|
||||
statement.executeUpdate("INSERT INTO guild_settings VALUES (\"" + guild.getName() + "\", " + guild.getId() + ", \"" + guild.getGroup() + "\", " + guild.getProfanityLevel() + ")");
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
close();
|
||||
}
|
||||
|
||||
public GuildData getGuildData(long guildId) {
|
||||
GuildData guild = new GuildData();
|
||||
|
||||
try {
|
||||
resultSet = statement.executeQuery("SELECT * FROM guild_settings WHERE guild_id=" + guildId);
|
||||
|
||||
while (resultSet.next()) {
|
||||
guild.setName(resultSet.getString("guild_name"));
|
||||
guild.setId(resultSet.getLong("guild_id"));
|
||||
guild.setGroup(resultSet.getString("guild_group"));
|
||||
guild.setProfanityLevel(resultSet.getInt("profanity_level"));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return guild;
|
||||
}
|
||||
|
||||
private void close() {
|
||||
try {
|
||||
if (resultSet != null) {
|
||||
resultSet.close();
|
||||
}
|
||||
|
||||
if (statement != null) {
|
||||
statement.close();
|
||||
}
|
||||
|
||||
if (connect != null) {
|
||||
connect.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package net.nevet5gi.buzzbot.functions;
|
||||
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageUpdateEvent;
|
||||
import net.nevet5gi.buzzbot.Config;
|
||||
import net.nevet5gi.buzzbot.util.Dictionary;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ProfanityFilter {
|
||||
private final Dictionary mildDictionary;
|
||||
private final Dictionary moderateDictionary;
|
||||
private final Dictionary strongDictionary;
|
||||
|
||||
public ProfanityFilter() {
|
||||
mildDictionary = new Dictionary();
|
||||
moderateDictionary = new Dictionary();
|
||||
strongDictionary = new Dictionary();
|
||||
}
|
||||
|
||||
public void handle(GuildMessageReceivedEvent event) {
|
||||
event.getMessage().delete().queue();
|
||||
event.getChannel().sendMessage("No swearing please :)").queue();
|
||||
}
|
||||
|
||||
public void handleEdit(GuildMessageUpdateEvent event) {
|
||||
event.getMessage().delete().queue();
|
||||
event.getChannel().sendMessage("Nice try ;)").queue();
|
||||
}
|
||||
|
||||
public void buildDictionaries() {
|
||||
String mildPath = Config.getConfig().getString("filter.path.mild");
|
||||
String moderatePath = Config.getConfig().getString("filter.path.moderate");
|
||||
String strongPath = Config.getConfig().getString("filter.path.strong");
|
||||
|
||||
String line;
|
||||
BufferedReader in = null;
|
||||
|
||||
try {
|
||||
in = new BufferedReader(new FileReader(mildPath));
|
||||
while ((line = in.readLine()) != null) {
|
||||
mildDictionary.addToDictionary(line);
|
||||
moderateDictionary.addToDictionary(line);
|
||||
strongDictionary.addToDictionary(line);
|
||||
}
|
||||
|
||||
in = new BufferedReader(new FileReader(moderatePath));
|
||||
while ((line = in.readLine()) != null) {
|
||||
moderateDictionary.addToDictionary(line);
|
||||
strongDictionary.addToDictionary(line);
|
||||
}
|
||||
|
||||
in = new BufferedReader(new FileReader(strongPath));
|
||||
while ((line = in.readLine()) != null) {
|
||||
strongDictionary.addToDictionary(line);
|
||||
}
|
||||
|
||||
} catch (FileNotFoundException e) { // FileReader
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) { // readLine
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean containsProfanity(String input, String strength) {
|
||||
Dictionary dictionary = null;
|
||||
|
||||
switch (strength) {
|
||||
case "mild" -> dictionary = mildDictionary;
|
||||
case "moderate" -> dictionary = moderateDictionary;
|
||||
case "strong" -> dictionary = strongDictionary;
|
||||
}
|
||||
|
||||
if (dictionary == null) {
|
||||
dictionary = new Dictionary();
|
||||
}
|
||||
|
||||
input = convert(input);
|
||||
|
||||
for (String profanity : dictionary.getDictionary()) {
|
||||
String pattern = "\\b" + Pattern.quote(profanity) + "\\b";
|
||||
Pattern p = Pattern.compile(pattern);
|
||||
Matcher m = p.matcher(input);
|
||||
if (m.find()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public String convert(String input) {
|
||||
return input.toLowerCase()
|
||||
.replace("@", "a")
|
||||
.replace("$", "s")
|
||||
.replace("!", "i")
|
||||
.replace("(", "c");
|
||||
}
|
||||
}
|
||||
43
src/main/java/net/nevet5gi/buzzbot/objects/BanData.java
Normal file
43
src/main/java/net/nevet5gi/buzzbot/objects/BanData.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package net.nevet5gi.buzzbot.objects;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.Time;
|
||||
|
||||
public class BanData extends UserData {
|
||||
private boolean banType;
|
||||
private int banLength;
|
||||
private String banReason;
|
||||
|
||||
public BanData() {}
|
||||
|
||||
public BanData(long userId, String userName, int userDiscriminator, Date date, Time time, boolean banType, int banLength, String banReason, String modName, long modId, String serverName, long serverId) {
|
||||
super(userId, userName, userDiscriminator, date, time, modName, modId, serverName, serverId);
|
||||
this.banType = banType;
|
||||
this.banLength = banLength;
|
||||
this.banReason = banReason;
|
||||
}
|
||||
|
||||
public void setBanType(boolean banType) {
|
||||
this.banType = banType;
|
||||
}
|
||||
|
||||
public boolean getBanType() {
|
||||
return banType;
|
||||
}
|
||||
|
||||
public void setBanLength(int banLength) {
|
||||
this.banLength = banLength;
|
||||
}
|
||||
|
||||
public int getBanLength() {
|
||||
return banLength;
|
||||
}
|
||||
|
||||
public void setBanReason(String banReason) {
|
||||
this.banReason = banReason;
|
||||
}
|
||||
|
||||
public String getBanReason() {
|
||||
return banReason;
|
||||
}
|
||||
}
|
||||
40
src/main/java/net/nevet5gi/buzzbot/objects/GuildData.java
Normal file
40
src/main/java/net/nevet5gi/buzzbot/objects/GuildData.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package net.nevet5gi.buzzbot.objects;
|
||||
|
||||
public class GuildData {
|
||||
private String name;
|
||||
private long id;
|
||||
private String group;
|
||||
private int profanityLevel;
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setGroup(String group) {
|
||||
this.group = group;
|
||||
}
|
||||
|
||||
public String getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
public void setProfanityLevel(int level) {
|
||||
this.profanityLevel = level;
|
||||
}
|
||||
|
||||
public int getProfanityLevel() {
|
||||
return profanityLevel;
|
||||
}
|
||||
}
|
||||
33
src/main/java/net/nevet5gi/buzzbot/objects/MuteData.java
Normal file
33
src/main/java/net/nevet5gi/buzzbot/objects/MuteData.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package net.nevet5gi.buzzbot.objects;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.Time;
|
||||
|
||||
public class MuteData extends UserData {
|
||||
private int muteLength;
|
||||
private String muteReason;
|
||||
|
||||
public MuteData() {}
|
||||
|
||||
public MuteData(long userId, String userName, int userDiscriminator, Date date, Time time, int muteLength, String muteReason, String modName, long modId, String serverName, long serverId) {
|
||||
super(userId, userName, userDiscriminator, date, time, modName, modId, serverName, serverId);
|
||||
this.muteLength = muteLength;
|
||||
this.muteReason = muteReason;
|
||||
}
|
||||
|
||||
public void setMuteLength(int muteLength) {
|
||||
this.muteLength = muteLength;
|
||||
}
|
||||
|
||||
public int getMuteLength() {
|
||||
return muteLength;
|
||||
}
|
||||
|
||||
public void setMuteReason(String muteReason) {
|
||||
this.muteReason = muteReason;
|
||||
}
|
||||
|
||||
public String getMuteReason() {
|
||||
return muteReason;
|
||||
}
|
||||
}
|
||||
109
src/main/java/net/nevet5gi/buzzbot/objects/UserData.java
Normal file
109
src/main/java/net/nevet5gi/buzzbot/objects/UserData.java
Normal file
@@ -0,0 +1,109 @@
|
||||
package net.nevet5gi.buzzbot.objects;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.Time;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalTime;
|
||||
|
||||
public class UserData {
|
||||
private String name = "users";
|
||||
private long userId;
|
||||
private String userName;
|
||||
private int userDiscriminator;
|
||||
private Date date = Date.valueOf(LocalDate.now());
|
||||
private Time time = Time.valueOf(LocalTime.now());
|
||||
private String modName;
|
||||
private long modId;
|
||||
private String serverName;
|
||||
private long serverId;
|
||||
|
||||
public UserData() {}
|
||||
|
||||
public UserData(long userId, String userName, int userDiscriminator, Date date, Time time, String modName, long modId, String serverName, long serverId) {
|
||||
this.setUserId(userId);
|
||||
this.setUserName(userName);
|
||||
this.setUserDiscriminator(userDiscriminator);
|
||||
this.setDate(date);
|
||||
this.setTime(time);
|
||||
this.setModName(modName);
|
||||
this.setModId(modId);
|
||||
this.setServerName(serverName);
|
||||
this.setServerId(serverId);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setUserId(long userId) {
|
||||
this.userId = userId;
|
||||
}
|
||||
|
||||
public long getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserDiscriminator(int userDiscriminator) {
|
||||
this.userDiscriminator = userDiscriminator;
|
||||
}
|
||||
|
||||
public int getUserDiscriminator() {
|
||||
return userDiscriminator;
|
||||
}
|
||||
|
||||
public void setDate(Date date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public Date getDate() {
|
||||
return date;
|
||||
}
|
||||
|
||||
public void setTime(Time time) {
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
public Time getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public void setModName(String modName) {
|
||||
this.modName = modName;
|
||||
}
|
||||
|
||||
public String getModName() {
|
||||
return modName;
|
||||
}
|
||||
|
||||
public void setModId(long modId) {
|
||||
this.modId = modId;
|
||||
}
|
||||
|
||||
public long getModId() {
|
||||
return modId;
|
||||
}
|
||||
|
||||
public void setServerName(String serverName) {
|
||||
this.serverName = serverName;
|
||||
}
|
||||
|
||||
public String getServerName() {
|
||||
return serverName;
|
||||
}
|
||||
|
||||
public void setServerId(long serverId) {
|
||||
this.serverId = serverId;
|
||||
}
|
||||
|
||||
public long getServerId() {
|
||||
return serverId;
|
||||
}
|
||||
}
|
||||
43
src/main/java/net/nevet5gi/buzzbot/objects/WarnData.java
Normal file
43
src/main/java/net/nevet5gi/buzzbot/objects/WarnData.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package net.nevet5gi.buzzbot.objects;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.Time;
|
||||
|
||||
public class WarnData extends UserData {
|
||||
private boolean banType;
|
||||
private int banLength;
|
||||
private String banReason;
|
||||
|
||||
public WarnData() {}
|
||||
|
||||
public WarnData(long userId, String userName, int userDiscriminator, Date date, Time time, boolean banType, int banLength, String banReason, String modName, long modId, String serverName, long serverId) {
|
||||
super(userId, userName, userDiscriminator, date, time, modName, modId, serverName, serverId);
|
||||
this.banType = banType;
|
||||
this.banLength = banLength;
|
||||
this.banReason = banReason;
|
||||
}
|
||||
|
||||
public void setBanType(boolean banType) {
|
||||
this.banType = banType;
|
||||
}
|
||||
|
||||
public boolean getBanType() {
|
||||
return banType;
|
||||
}
|
||||
|
||||
public void setBanLength(int banLength) {
|
||||
this.banLength = banLength;
|
||||
}
|
||||
|
||||
public int getBanLength() {
|
||||
return banLength;
|
||||
}
|
||||
|
||||
public void setBanReason(String banReason) {
|
||||
this.banReason = banReason;
|
||||
}
|
||||
|
||||
public String getBanReason() {
|
||||
return banReason;
|
||||
}
|
||||
}
|
||||
15
src/main/java/net/nevet5gi/buzzbot/util/Dictionary.java
Normal file
15
src/main/java/net/nevet5gi/buzzbot/util/Dictionary.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package net.nevet5gi.buzzbot.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Dictionary {
|
||||
private final ArrayList<String> dictionary = new ArrayList<>();
|
||||
|
||||
public void addToDictionary(String word) {
|
||||
dictionary.add(word);
|
||||
}
|
||||
|
||||
public ArrayList<String> getDictionary() {
|
||||
return dictionary;
|
||||
}
|
||||
}
|
||||
39
src/main/java/net/nevet5gi/buzzbot/util/JsonUtils.java
Normal file
39
src/main/java/net/nevet5gi/buzzbot/util/JsonUtils.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package net.nevet5gi.buzzbot.util;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import net.nevet5gi.buzzbot.objects.UserData;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class JsonUtils <T extends UserData> {
|
||||
T object;
|
||||
|
||||
public void createJson(T object) {
|
||||
try {
|
||||
Writer writer = new FileWriter("./" + object.getName() + ".json");
|
||||
|
||||
GsonBuilder builder = new GsonBuilder();
|
||||
builder.setPrettyPrinting();
|
||||
Gson gson = builder.create();
|
||||
String jsonString = gson.toJson(object);
|
||||
writer.write(jsonString);
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public T loadJson(String jsonFileName) {
|
||||
try {
|
||||
BufferedReader reader = new BufferedReader(new FileReader("./" + jsonFileName + ".json"));
|
||||
|
||||
return new Gson().fromJson(reader, (Type) object);
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user