Skip to content

Commit

Permalink
Make module parameters typed
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Jun 6, 2024
1 parent 41bb2d0 commit 42c1d78
Show file tree
Hide file tree
Showing 18 changed files with 129 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/main/java/net/neoforged/camelot/BotMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import net.neoforged.camelot.log.JoinsLogging;
import net.neoforged.camelot.log.MessageLogging;
import net.neoforged.camelot.log.ModerationActionRecorder;
import net.neoforged.camelot.module.CamelotModule;
import net.neoforged.camelot.module.api.CamelotModule;
import net.neoforged.camelot.module.StatsModule;
import net.neoforged.camelot.util.AuthUtil;
import net.neoforged.camelot.util.Utils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import net.neoforged.camelot.config.module.ModuleConfiguration;
import net.neoforged.camelot.config.module.Tricks;
import net.neoforged.camelot.config.module.WebServer;
import net.neoforged.camelot.module.CamelotModule;
import net.neoforged.camelot.module.api.CamelotModule;

import java.nio.file.Files;
import java.nio.file.Path;
Expand Down
18 changes: 7 additions & 11 deletions src/main/java/net/neoforged/camelot/module/BanAppealModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.auto.service.AutoService;
import com.google.common.primitives.Ints;
import io.javalin.Javalin;
import io.javalin.http.Context;
import io.javalin.http.Cookie;
import io.javalin.http.HttpStatus;
Expand Down Expand Up @@ -40,6 +39,7 @@
import net.neoforged.camelot.db.transactionals.BanAppealsDAO;
import net.neoforged.camelot.db.transactionals.ModLogsDAO;
import net.neoforged.camelot.log.ModerationActionRecorder;
import net.neoforged.camelot.module.api.CamelotModule;
import net.neoforged.camelot.server.WebServer;
import net.neoforged.camelot.util.DateUtils;
import net.neoforged.camelot.util.MailService;
Expand Down Expand Up @@ -89,6 +89,12 @@
public class BanAppealModule extends CamelotModule.Base<BanAppeals> {
public BanAppealModule() {
super(BanAppeals.class);
accept(WebServerModule.SERVER, javalin -> {
javalin.get("/ban-appeals/discord", this::verifyOauth);
javalin.get("/ban-appeals/<serverId>", this::onAccess);
javalin.post("/ban-appeals/followup/<serverId>", this::onSubmitFollowup);
javalin.post("/ban-appeals/<serverId>", this::onSubmitAppeal);
});
}

private OAuthClient client;
Expand All @@ -115,16 +121,6 @@ public void setup(JDA jda) {
mail = MailService.from(config().getMail());
}

@Override
public void acceptFrom(String moduleId, Object object) {
if (moduleId.equals("webserver") && object instanceof Javalin javalin) {
javalin.get("/ban-appeals/discord", this::verifyOauth);
javalin.get("/ban-appeals/<serverId>", this::onAccess);
javalin.post("/ban-appeals/followup/<serverId>", this::onSubmitFollowup);
javalin.post("/ban-appeals/<serverId>", this::onSubmitAppeal);
}
}

@Override
public void registerListeners(JDABuilder builder) {
builder.addEventListeners((EventListener) (GenericEvent gevent) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.dv8tion.jda.api.JDABuilder;
import net.neoforged.camelot.config.module.Counters;
import net.neoforged.camelot.listener.CountersListener;
import net.neoforged.camelot.module.api.CamelotModule;

/**
* The module for counters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.neoforged.camelot.commands.utility.CustomPingsCommand;
import net.neoforged.camelot.config.module.CustomPings;
import net.neoforged.camelot.listener.CustomPingListener;
import net.neoforged.camelot.module.api.CamelotModule;

@AutoService(CamelotModule.class)
public class CustomPingsModule extends CamelotModule.Base<CustomPings> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
import net.neoforged.camelot.config.module.FilePreview;
import net.neoforged.camelot.module.api.CamelotModule;
import net.neoforged.camelot.util.Utils;

import java.net.URI;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.neoforged.camelot.commands.information.InfoChannelCommand;
import net.neoforged.camelot.commands.information.RuleCommand;
import net.neoforged.camelot.config.module.InfoChannels;
import net.neoforged.camelot.module.api.CamelotModule;

import java.util.concurrent.TimeUnit;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.dv8tion.jda.api.JDA;
import net.neoforged.camelot.config.module.MessageReferencing;
import net.neoforged.camelot.listener.ReferencingListener;
import net.neoforged.camelot.module.api.CamelotModule;

/**
* Module for message referencing using {@code .} replies.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.auto.service.AutoService;
import com.jagrosh.jdautilities.command.CommandClientBuilder;
import io.javalin.Javalin;
import io.javalin.http.BadRequestResponse;
import io.javalin.http.Context;
import io.javalin.http.Cookie;
Expand All @@ -23,6 +22,7 @@
import net.neoforged.camelot.db.transactionals.McVerificationDAO;
import net.neoforged.camelot.listener.ReferencingListener;
import net.neoforged.camelot.log.ModerationActionRecorder;
import net.neoforged.camelot.module.api.CamelotModule;
import net.neoforged.camelot.server.WebServer;
import net.neoforged.camelot.util.Utils;
import net.neoforged.camelot.util.oauth.OAuthClient;
Expand All @@ -48,18 +48,14 @@
import static j2html.TagCreator.br;
import static j2html.TagCreator.button;
import static j2html.TagCreator.div;
import static j2html.TagCreator.footer;
import static j2html.TagCreator.h1;
import static j2html.TagCreator.h2;
import static j2html.TagCreator.h3;
import static j2html.TagCreator.h5;
import static j2html.TagCreator.hr;
import static j2html.TagCreator.i;
import static j2html.TagCreator.p;
import static j2html.TagCreator.pre;
import static j2html.TagCreator.script;
import static j2html.TagCreator.span;
import static j2html.TagCreator.sub;
import static j2html.TagCreator.text;
import static j2html.TagCreator.title;

Expand All @@ -69,21 +65,7 @@ public class MinecraftVerificationModule extends CamelotModule.Base<MinecraftVer

public MinecraftVerificationModule() {
super(MinecraftVerification.class);
}

@Override
public String id() {
return "mc-verification";
}

@Override
public Set<String> getDependencies() {
return Set.of("webserver");
}

@Override
public void acceptFrom(String moduleId, Object object) {
if (moduleId.equals("webserver") && object instanceof Javalin javalin) {
accept(WebServerModule.SERVER, javalin -> {
javalin.get("/minecraft/<serverId>/verify", this::onVerifyRoot);
javalin.post("/minecraft/<serverId>/verify", this::onVerifyPost);
javalin.get("/minecraft/verify/discord", ctx -> verifyOauth(ctx, "discord_token", discord));
Expand All @@ -94,7 +76,17 @@ public void acceptFrom(String moduleId, Object object) {
ctx.removeCookie("discord_token", path);
ctx.removeCookie("xbox_token", path);
});
}
});
}

@Override
public String id() {
return "mc-verification";
}

@Override
public Set<String> getDependencies() {
return Set.of("webserver");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.neoforged.camelot.commands.moderation.UnmuteCommand;
import net.neoforged.camelot.commands.moderation.WarnCommand;
import net.neoforged.camelot.config.module.Moderation;
import net.neoforged.camelot.module.api.CamelotModule;

/**
* The module that provides moderation commands.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.neoforged.camelot.config.module.Quotes;
import net.neoforged.camelot.db.schemas.Quote;
import net.neoforged.camelot.db.transactionals.QuotesDAO;
import net.neoforged.camelot.module.api.CamelotModule;
import org.jetbrains.annotations.Nullable;

import javax.imageio.ImageIO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import net.dv8tion.jda.api.interactions.components.ItemComponent;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import net.dv8tion.jda.api.requests.RestAction;
import net.dv8tion.jda.api.utils.Result;
import net.dv8tion.jda.api.utils.TimeFormat;
import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder;
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
Expand All @@ -29,11 +28,11 @@
import net.neoforged.camelot.db.transactionals.RemindersDAO;
import net.neoforged.camelot.listener.DismissListener;
import net.neoforged.camelot.listener.ReferencingListener;
import net.neoforged.camelot.module.api.CamelotModule;
import net.neoforged.camelot.util.DateUtils;
import net.neoforged.camelot.util.Utils;

import java.awt.*;
import java.time.Duration;
import java.time.Instant;
import java.util.Collection;
import java.util.EnumSet;
Expand All @@ -43,7 +42,6 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.Stream;

@AutoService(CamelotModule.class)
public class RemindersModule extends CamelotModule.Base<Reminders> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.neoforged.camelot.Database;
import net.neoforged.camelot.config.module.Statistics;
import net.neoforged.camelot.db.transactionals.StatsDAO;
import net.neoforged.camelot.module.api.CamelotModule;
import org.jdbi.v3.core.extension.ExtensionConsumer;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.neoforged.camelot.commands.utility.ThreadPingsCommand;
import net.neoforged.camelot.config.module.ThreadPings;
import net.neoforged.camelot.listener.ThreadPingsListener;
import net.neoforged.camelot.module.api.CamelotModule;

/**
* Module for thread pings, for automatically mentioning a role in public threads created under a channel and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.neoforged.camelot.db.transactionals.SlashTricksDAO;
import net.neoforged.camelot.db.transactionals.TricksDAO;
import net.neoforged.camelot.listener.TrickListener;
import net.neoforged.camelot.module.api.CamelotModule;
import net.neoforged.camelot.script.SlashTrickManager;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import io.javalin.http.staticfiles.Location;
import net.dv8tion.jda.api.JDA;
import net.neoforged.camelot.BotMain;
import net.neoforged.camelot.module.api.CamelotModule;
import net.neoforged.camelot.module.api.ParameterType;
import net.neoforged.camelot.server.WebServer;

import java.io.IOException;
Expand All @@ -14,6 +16,8 @@

@AutoService(CamelotModule.class)
public class WebServerModule extends CamelotModule.Base<net.neoforged.camelot.config.module.WebServer> {
public static final ParameterType<Javalin> SERVER = ParameterType.get("server", Javalin.class);

private WebServer webServer;

public WebServerModule() {
Expand Down Expand Up @@ -50,7 +54,7 @@ public void setup(JDA jda) {
});
}), config().getPort());

BotMain.forEachModule(module -> module.acceptFrom(id(), webServer.javalin));
BotMain.forEachModule(module -> module.acceptParameter(SERVER, webServer.javalin));

this.webServer.run();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package net.neoforged.camelot.module;
package net.neoforged.camelot.module.api;

import com.jagrosh.jdautilities.command.CommandClientBuilder;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.neoforged.camelot.config.CamelotConfig;
import net.neoforged.camelot.config.module.GHAuth;
import net.neoforged.camelot.config.module.ModuleConfiguration;
import net.neoforged.camelot.util.AuthUtil;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;

import java.io.IOException;
import java.nio.file.Files;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

/**
* A camelot module is a part of the bot that can be disabled and is loaded via a {@link java.util.ServiceLoader}.
Expand Down Expand Up @@ -53,10 +48,10 @@ default void setup(JDA jda) {
/**
* Accept an object from another module.
*
* @param moduleId the ID of the module sending the object
* @param object the sent object
* @param type the type of the object
* @param object the sent object
*/
default void acceptFrom(String moduleId, Object object) {
default <T> void acceptParameter(ParameterType<T> type, T object) {

}

Expand Down Expand Up @@ -86,16 +81,22 @@ default boolean shouldLoad() {

/**
* Base class for {@link CamelotModule camelot modules}.
*
* @param <C> the configuration type
*/
abstract class Base<C extends ModuleConfiguration> implements CamelotModule<C> {
private final Class<C> configType;
private final Map<ParameterType<?>, Consumer<?>> parameters = new IdentityHashMap<>();
private C config;

protected Base(Class<C> configType) {
this.configType = configType;
}

protected <T> void accept(ParameterType<T> type, Consumer<T> acceptor) {
parameters.put(type, acceptor);
}

@Override
public C config() {
if (config == null) {
Expand All @@ -104,6 +105,13 @@ public C config() {
return config;
}

@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public <T> void acceptParameter(ParameterType<T> type, T object) {
Consumer accept = parameters.get(type);
if (accept != null) accept.accept(object);
}

@Override
public Class<C> configType() {
return configType;
Expand Down
Loading

0 comments on commit 42c1d78

Please sign in to comment.