Skip to content

Commit

Permalink
Add wiki command (#317)
Browse files Browse the repository at this point in the history
* Add search command

Signed-off-by: Joshua Castle <[email protected]>

* Use unicode codepoints and rename PageUtils > PageHelper

Signed-off-by: Joshua Castle <[email protected]>

* Fallback to include up to first 9 lines if no match is found

Signed-off-by: Joshua Castle <[email protected]>

* Address reviews; fix Algolia v4

Signed-off-by: Joshua Castle <[email protected]>

* Ensure no profane wiki searches and other commands

Signed-off-by: Joshua Castle <[email protected]>

* License header

Signed-off-by: Joshua Castle <[email protected]>

* Update src/main/java/org/geysermc/discordbot/listeners/SwearHandler.java

Co-authored-by: rtm516 <[email protected]>

---------

Signed-off-by: Joshua Castle <[email protected]>
Co-authored-by: rtm516 <[email protected]>
  • Loading branch information
Kas-tle and rtm516 authored Sep 25, 2024
1 parent 98569a4 commit 011d1b3
Show file tree
Hide file tree
Showing 18 changed files with 1,571 additions and 24 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ cmake-build-*/
# IntelliJ
out/

# VSCode
.vscode/

# mpeltonen/sbt-idea plugin
.idea_modules/

Expand Down Expand Up @@ -237,3 +240,4 @@ gradle-app.setting
# End of https://www.toptal.com/developers/gitignore/api/git,java,gradle,eclipse,netbeans,jetbrains+all,visualstudiocode.

bot.properties
bot
4 changes: 4 additions & 0 deletions bot.properties.example
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ github-token: github_oauth_token
sentry-dsn: https://[email protected]/xxx
sentry-env: production
ocr-path: /usr/share/tesseract-ocr/4.00/tessdata
algolia-application-id: 0DTHI9QFCH
algolia-search-api-key: 3cc0567f76d2ed3ffdb4cc94f0ac9815
algolia-index-name: geysermc
algolia-site-search-url: https://geysermc.org/search?q=
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ dependencies {
// Image processing and OCR
implementation 'net.sourceforge.tess4j:tess4j:5.12.0'
implementation 'org.imgscalr:imgscalr-lib:4.2'

// Agolia Search (For Wiki)
implementation 'com.algolia:algoliasearch:4.3.2'
}

jar {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/geysermc/discordbot/GeyserBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package org.geysermc.discordbot;

import com.algolia.api.SearchClient;
import com.jagrosh.jdautilities.command.Command;
import com.jagrosh.jdautilities.command.CommandClientBuilder;
import com.jagrosh.jdautilities.command.ContextMenu;
Expand Down Expand Up @@ -65,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 All @@ -87,6 +89,7 @@ public class GeyserBot {
private static JDA jda;
private static GitHub github;
private static Server httpServer;
private static SearchClient algolia;

static {
// Gathers all commands from "commands" package.
Expand Down Expand Up @@ -114,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 Expand Up @@ -161,6 +169,9 @@ public static void main(String[] args) throws IOException {
// Connect to github
github = new GitHubBuilder().withOAuthToken(PropertiesManager.getGithubToken()).build();

// Connect to Algolia
algolia = new SearchClient(PropertiesManager.getAlgoliaApplicationId(), PropertiesManager.getAlgoliaSearchApiKey());

// Initialize the waiter
EventWaiter waiter = new EventWaiter();

Expand Down Expand Up @@ -298,6 +309,10 @@ public static GitHub getGithub() {
return github;
}

public static SearchClient getAlgolia() {
return algolia;
}

public static ScheduledExecutorService getGeneralThreadPool() {
return generalThreadPool;
}
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,73 @@
/*
* Copyright (c) 2020-2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/GeyserDiscordBot
*/

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
Loading

0 comments on commit 011d1b3

Please sign in to comment.