Skip to content

Commit

Permalink
Ensure no profane wiki searches and other commands
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Castle <[email protected]>
  • Loading branch information
Kas-tle committed Sep 19, 2024
1 parent 4622b5a commit 22652b6
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 27 deletions.
6 changes: 6 additions & 0 deletions src/main/java/org/geysermc/discordbot/GeyserBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
Expand Down Expand Up @@ -116,6 +117,11 @@ public class GeyserBot {
if (theClass.getName().contains("SubCommand")) {
continue;
}
// Don't load abstract classes
if (Modifier.isAbstract(theClass.getModifiers())) {
continue;
}

slashCommands.add(theClass.getDeclaredConstructor().newInstance());
LoggerFactory.getLogger(theClass).debug("Loaded SlashCommand Successfully!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@

package org.geysermc.discordbot.commands;

import com.jagrosh.jdautilities.command.SlashCommand;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.geysermc.discordbot.commands.filter.FilteredSlashCommand;
import org.geysermc.discordbot.util.BotColors;
import org.json.JSONObject;
import pw.chew.chewbotcca.util.RestClient;
Expand All @@ -39,7 +39,7 @@
import java.util.Collections;
import java.util.UUID;

public class FloodgateUuidCommand extends SlashCommand {
public class FloodgateUuidCommand extends FilteredSlashCommand {

public FloodgateUuidCommand() {
this.name = "uuid";
Expand All @@ -54,7 +54,7 @@ public FloodgateUuidCommand() {
}

@Override
protected void execute(SlashCommandEvent event) {
protected void executeFiltered(SlashCommandEvent event) {
// get bedrock username, replace char in case they include Floodgate prefix.
String username = event.optString("bedrock-username", "").replace(".", "");
EmbedBuilder builder = new EmbedBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@

package org.geysermc.discordbot.commands;

import com.jagrosh.jdautilities.command.SlashCommand;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.geysermc.discordbot.commands.filter.FilteredSlashCommand;
import org.geysermc.discordbot.util.BotColors;
import org.geysermc.discordbot.util.BotHelpers;
import org.geysermc.discordbot.util.MessageHelper;
Expand All @@ -40,7 +40,7 @@
import java.io.IOException;
import java.util.Arrays;

public class GithubCommand extends SlashCommand {
public class GithubCommand extends FilteredSlashCommand {

public GithubCommand() {
this.name = "github";
Expand All @@ -54,7 +54,7 @@ public GithubCommand() {
}

@Override
protected void execute(SlashCommandEvent event) {
protected void executeFiltered(SlashCommandEvent event) {
String repository = event.optString("repo", "");
String owner = event.optString("owner", "");
event.deferReply(false).queue(interactionHook -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
package org.geysermc.discordbot.commands;

import com.jagrosh.jdautilities.command.CommandEvent;
import com.jagrosh.jdautilities.command.SlashCommand;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.geysermc.discordbot.GeyserBot;
import org.geysermc.discordbot.commands.filter.FilteredSlashCommand;
import org.geysermc.discordbot.util.BotColors;
import org.geysermc.discordbot.util.MessageHelper;
import org.kohsuke.github.GHFileNotFoundException;
Expand All @@ -50,7 +50,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IssueCommand extends SlashCommand {
public class IssueCommand extends FilteredSlashCommand {

private static final Pattern REPO_PATTERN = Pattern.compile("(^| )([\\w.\\-]+/)?([\\w.\\-]+)( |$)", Pattern.CASE_INSENSITIVE);
private static final Pattern ISSUE_PATTERN = Pattern.compile("(^| )#?([0-9]+)( |$)", Pattern.CASE_INSENSITIVE);
Expand All @@ -69,7 +69,7 @@ public IssueCommand() {
}

@Override
protected void execute(SlashCommandEvent event) {
protected void executeFiltered(SlashCommandEvent event) {
// Issue
int issue = (int) event.optLong("number", 0);
// Repo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import br.com.azalim.mcserverping.MCPingResponse;
import br.com.azalim.mcserverping.MCPingUtil;
import com.jagrosh.jdautilities.command.CommandEvent;
import com.jagrosh.jdautilities.command.SlashCommand;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import com.nukkitx.protocol.bedrock.BedrockClient;
import com.nukkitx.protocol.bedrock.BedrockPong;
Expand All @@ -39,6 +38,7 @@
import net.dv8tion.jda.api.interactions.InteractionHook;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.geysermc.discordbot.commands.filter.FilteredSlashCommand;
import org.geysermc.discordbot.util.BotColors;
import org.geysermc.discordbot.util.BotHelpers;
import org.geysermc.discordbot.util.MessageHelper;
Expand All @@ -52,7 +52,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class PingCommand extends SlashCommand {
public class PingCommand extends FilteredSlashCommand {
private static final int TIMEOUT = 1250; // in ms, has to stay below 1500 (1.5s for each platform, total of 3s)

public PingCommand() {
Expand All @@ -71,7 +71,7 @@ public PingCommand() {
}

@Override
protected void execute(SlashCommandEvent event) {
protected void executeFiltered(SlashCommandEvent event) {
// Defer to wait for us to load a response and allows for files to be uploaded
InteractionHook interactionHook = event.deferReply().complete();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@

package org.geysermc.discordbot.commands;

import com.jagrosh.jdautilities.command.SlashCommand;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.geysermc.discordbot.commands.filter.FilteredSlashCommand;
import org.geysermc.discordbot.tags.SlashTag;
import org.geysermc.discordbot.tags.TagsManager;
import org.geysermc.discordbot.util.BotColors;
Expand All @@ -41,7 +41,7 @@
import java.util.Collections;
import java.util.List;

public class TagCommand extends SlashCommand {
public class TagCommand extends FilteredSlashCommand {

public TagCommand() {
this.name = "tag";
Expand All @@ -56,7 +56,7 @@ public TagCommand() {
}

@Override
protected void execute(SlashCommandEvent event) {
protected void executeFiltered(SlashCommandEvent event) {
String tagName = event.getOption("name").getAsString();
SlashTag tag = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@

import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandEvent;
import com.jagrosh.jdautilities.command.SlashCommand;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.geysermc.discordbot.commands.filter.FilteredSlashCommand;
import org.geysermc.discordbot.tags.TagsManager;
import org.geysermc.discordbot.util.BotColors;
import org.geysermc.discordbot.util.PropertiesManager;
Expand All @@ -42,7 +42,7 @@
import java.util.Collections;
import java.util.List;

public class TagsCommand extends SlashCommand {
public class TagsCommand extends FilteredSlashCommand {
public TagsCommand() {
this.name = "tags";
this.arguments = "[search]";
Expand All @@ -55,7 +55,7 @@ public TagsCommand() {
}

@Override
protected void execute(SlashCommandEvent event) {
protected void executeFiltered(SlashCommandEvent event) {
String search = event.optString("search", "");

event.replyEmbeds(handle(search)).queue();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.geysermc.discordbot.commands.filter;

import com.jagrosh.jdautilities.command.SlashCommand;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import org.geysermc.discordbot.listeners.SwearHandler;
import org.geysermc.discordbot.storage.ServerSettings;
import org.geysermc.discordbot.util.BotColors;

import java.util.regex.Pattern;

public abstract class FilteredSlashCommand extends SlashCommand {
@Override
protected final void execute(SlashCommandEvent event) {
Pattern filterPattern = null;
for (OptionMapping option : event.getOptions()) {
if (option.getType() != OptionType.STRING) continue;
if ((filterPattern = SwearHandler.checkString(option.getAsString())) != null) break;
}

if (filterPattern != null) {
event.reply(event.getUser().getAsMention() +
" your command cannot be processed because it contains profanity! Please read our rules for more information.")
.setEphemeral(true).queue();

// Log the event
if (event.getGuild() != null) {
String channel = event.getChannel() == null ? "Unknown" : event.getChannel().getAsMention();

ServerSettings.getLogChannel(event.getGuild()).sendMessageEmbeds(new EmbedBuilder()
.setTitle("Profanity blocked command")
.setDescription("**Sender:** " + event.getUser().getAsMention() + "\n" +
"**Channel:** " + channel + "\n" +
"**Regex:** `" + filterPattern + "`\n" +
"**Command:** " + event.getCommandString())
.setColor(BotColors.FAILURE.getColor())
.build()).queue();
}
return;
}

executeFiltered(event);
}

protected abstract void executeFiltered(SlashCommandEvent event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@
package org.geysermc.discordbot.commands.search;

import com.jagrosh.jdautilities.command.CommandEvent;
import com.jagrosh.jdautilities.command.SlashCommand;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.geysermc.discordbot.commands.filter.FilteredSlashCommand;
import org.geysermc.discordbot.util.BotColors;
import org.geysermc.discordbot.util.DicesCoefficient;
import org.geysermc.discordbot.util.MessageHelper;
Expand All @@ -44,9 +44,8 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class ProviderCommand extends SlashCommand {
public class ProviderCommand extends FilteredSlashCommand {
private List<Provider> cache = null;
private long cacheTime = 0;

Expand All @@ -63,7 +62,7 @@ public ProviderCommand() {
}

@Override
protected void execute(SlashCommandEvent event) {
protected void executeFiltered(SlashCommandEvent event) {
event.replyEmbeds(handle(event.optString("provider", ""))).queue();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
import com.algolia.model.search.FacetFilters;
import com.algolia.model.search.SearchParamsObject;
import com.jagrosh.jdautilities.command.CommandEvent;
import com.jagrosh.jdautilities.command.SlashCommand;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.OptionData;
import org.geysermc.discordbot.GeyserBot;
import org.geysermc.discordbot.commands.filter.FilteredSlashCommand;
import org.geysermc.discordbot.util.BotColors;
import org.geysermc.discordbot.util.DocSearchResult;
import org.geysermc.discordbot.util.MessageHelper;
Expand All @@ -55,7 +55,7 @@
/**
* A command to search the Geyser wiki for a query.
*/
public class WikiCommand extends SlashCommand {
public class WikiCommand extends FilteredSlashCommand {
/**
* The attributes to retrieve from the Algolia search.
*/
Expand Down Expand Up @@ -96,7 +96,7 @@ public WikiCommand() {
* @param event The SlashCommandEvent.
*/
@Override
protected void execute(SlashCommandEvent event) {
protected void executeFiltered(SlashCommandEvent event) {
String query = event.optString("query", "");

if (query.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.events.guild.member.update.GuildMemberUpdateNicknameEvent;
import net.dv8tion.jda.api.events.interaction.command.GenericCommandInteractionEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import org.geysermc.discordbot.GeyserBot;
import org.geysermc.discordbot.storage.ServerSettings;
import org.geysermc.discordbot.util.BotColors;
import org.geysermc.discordbot.util.BotHelpers;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -99,7 +103,7 @@ public static void loadFilters() {
}

@Nullable
private Pattern checkString(String input) {
public static Pattern checkString(String input) {
// TODO: Maybe only clean start and end? Then run through the same as normalInput?
input = input.toLowerCase();
String cleanInput = CLEAN_PATTERN.matcher(input).replaceAll("");
Expand Down Expand Up @@ -200,4 +204,34 @@ public void onGuildMemberUpdateNickname(@NotNull GuildMemberUpdateNicknameEvent
event.getMember().modifyNickname(getRandomNick()).queue();
}
}

// @Override
// public void onGenericCommandInteraction(@Nonnull GenericCommandInteractionEvent event) {
// Pattern filterPattern = null;
// for (OptionMapping option: event.getOptions()) {
// if (option.getType() != OptionType.STRING) continue;
// if ((filterPattern = checkString(option.getAsString())) == null) continue;
// break;
// }

// if (filterPattern == null) return;

// event.reply(event.getUser().getAsMention() +
// " your command cannot be processed because it contains profanity! Please read our rules for more information.")
// .setEphemeral(true).queue();

// if (event.getGuild() == null) return;

// @SuppressWarnings("null")
// String channel = event.getChannel() == null ? "Unknown" : event.getChannel().getAsMention();

// ServerSettings.getLogChannel(event.getGuild()).sendMessageEmbeds(new EmbedBuilder()
// .setTitle("Profanity removed")
// .setDescription("**Sender:** " + event.getUser().getAsMention() + "\n" +
// "**Channel:** " + channel + "\n" +
// "**Regex:** `" + filterPattern + "`\n" +
// "**Command:** " + event.getCommandString())
// .setColor(BotColors.FAILURE.getColor())
// .build()).queue();
// }
}
Loading

0 comments on commit 22652b6

Please sign in to comment.