Skip to content

Commit 6bea11c

Browse files
committed
Integration of features
- Renamed Commands to Features - Renamed CommandSystem to BotCore - Integrated new MessageReceiver and EventReceiver capabilities - Migrated RejoinMuteListener to new EventReceiver
1 parent 7678d9d commit 6bea11c

File tree

9 files changed

+165
-118
lines changed

9 files changed

+165
-118
lines changed

application/src/main/java/org/togetherjava/tjbot/Application.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import net.dv8tion.jda.api.requests.GatewayIntent;
66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
8-
import org.togetherjava.tjbot.commands.Commands;
9-
import org.togetherjava.tjbot.commands.system.CommandSystem;
8+
import org.togetherjava.tjbot.commands.Features;
9+
import org.togetherjava.tjbot.commands.system.BotCore;
1010
import org.togetherjava.tjbot.config.Config;
1111
import org.togetherjava.tjbot.db.Database;
1212

@@ -22,7 +22,7 @@
2222
* New commands can be created by implementing
2323
* {@link net.dv8tion.jda.api.events.interaction.SlashCommandEvent} or extending
2424
* {@link org.togetherjava.tjbot.commands.SlashCommandAdapter}. They can then be registered in
25-
* {@link Commands}.
25+
* {@link Features}.
2626
*/
2727
public enum Application {
2828
;
@@ -79,7 +79,7 @@ public static void runBot(String token, Path databasePath) {
7979
JDA jda = JDABuilder.createDefault(token)
8080
.enableIntents(GatewayIntent.GUILD_MEMBERS)
8181
.build();
82-
jda.addEventListener(new CommandSystem(jda, database));
82+
jda.addEventListener(new BotCore(jda, database));
8383
jda.awaitReady();
8484
logger.info("Bot is ready");
8585

application/src/main/java/org/togetherjava/tjbot/commands/Commands.java

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package org.togetherjava.tjbot.commands;
2+
3+
import net.dv8tion.jda.api.JDA;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.togetherjava.tjbot.commands.basic.PingCommand;
6+
import org.togetherjava.tjbot.commands.basic.VcActivityCommand;
7+
import org.togetherjava.tjbot.commands.free.FreeCommand;
8+
import org.togetherjava.tjbot.commands.mathcommands.TeXCommand;
9+
import org.togetherjava.tjbot.commands.moderation.*;
10+
import org.togetherjava.tjbot.commands.moderation.temp.TemporaryModerationRoutine;
11+
import org.togetherjava.tjbot.commands.system.BotCore;
12+
import org.togetherjava.tjbot.commands.tags.TagCommand;
13+
import org.togetherjava.tjbot.commands.tags.TagManageCommand;
14+
import org.togetherjava.tjbot.commands.tags.TagSystem;
15+
import org.togetherjava.tjbot.commands.tags.TagsCommand;
16+
import org.togetherjava.tjbot.db.Database;
17+
import org.togetherjava.tjbot.routines.ModAuditLogRoutine;
18+
19+
import java.util.ArrayList;
20+
import java.util.Collection;
21+
22+
/**
23+
* Utility class that offers all features that should be registered by the system, such as commands.
24+
* New features have to be added here, where {@link BotCore} will then pick it up from and register
25+
* it with the system.
26+
* <p>
27+
* To add a new slash command, extend the commands returned by
28+
* {@link #createFeatures(JDA, Database)}.
29+
*/
30+
public enum Features {
31+
;
32+
33+
/**
34+
* Creates all features that should be registered with this application.
35+
* <p>
36+
* Calling this method multiple times will result in multiple features being created, which
37+
* generally should be avoided.
38+
*
39+
* @param jda the JDA instance commands will be registered at
40+
* @param database the database of the application, which features can use to persist data
41+
* @return a collection of all features
42+
*/
43+
public static @NotNull Collection<Feature> createFeatures(@NotNull JDA jda,
44+
@NotNull Database database) {
45+
TagSystem tagSystem = new TagSystem(database);
46+
ModerationActionsStore actionsStore = new ModerationActionsStore(database);
47+
48+
// NOTE The system can add special system relevant commands also by itself,
49+
// hence this list may not necessarily represent the full list of all commands actually
50+
// available.
51+
Collection<Feature> features = new ArrayList<>();
52+
53+
// Routines
54+
// TODO This should be moved into some proper command system instead (see GH issue #235
55+
// which adds support for routines)
56+
new ModAuditLogRoutine(jda, database).start();
57+
new TemporaryModerationRoutine(jda, actionsStore).start();
58+
59+
// Message receivers
60+
61+
// Event receivers
62+
features.add(new RejoinMuteListener(actionsStore));
63+
64+
// Slash commands
65+
features.add(new PingCommand());
66+
features.add(new TeXCommand());
67+
features.add(new TagCommand(tagSystem));
68+
features.add(new TagManageCommand(tagSystem));
69+
features.add(new TagsCommand(tagSystem));
70+
features.add(new VcActivityCommand());
71+
features.add(new WarnCommand(actionsStore));
72+
features.add(new KickCommand(actionsStore));
73+
features.add(new BanCommand(actionsStore));
74+
features.add(new UnbanCommand(actionsStore));
75+
features.add(new FreeCommand());
76+
features.add(new AuditCommand(actionsStore));
77+
features.add(new MuteCommand(actionsStore));
78+
features.add(new UnmuteCommand(actionsStore));
79+
80+
return features;
81+
}
82+
}

application/src/main/java/org/togetherjava/tjbot/commands/SlashCommand.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* <p>
2222
* All slash commands have to implement this interface. For convenience, there is a
2323
* {@link SlashCommandAdapter} available that implemented most methods already. A new command can
24-
* then be registered by adding it to {@link Commands}.
24+
* then be registered by adding it to {@link Features}.
2525
* <p>
2626
* <p>
2727
* Slash commands can either be visible globally in Discord or just to specific guilds. They can
@@ -36,7 +36,7 @@
3636
* <p>
3737
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}.
3838
*/
39-
public interface SlashCommand {
39+
public interface SlashCommand extends Feature {
4040

4141
/**
4242
* Gets the name of the command.
@@ -84,16 +84,16 @@ public interface SlashCommand {
8484
* <p>
8585
* <p>
8686
* This method may be called multiple times, implementations must not create new data each time
87-
* but instead configure it once beforehand. The command system will automatically call this
88-
* method to register the command to Discord.
87+
* but instead configure it once beforehand. The core system will automatically call this method
88+
* to register the command to Discord.
8989
*
9090
* @return the command data of this command
9191
*/
9292
@NotNull
9393
CommandData getData();
9494

9595
/**
96-
* Triggered by the command system after system startup is complete. This can be used for
96+
* Triggered by the core system after system startup is complete. This can be used for
9797
* initialisation actions that cannot occur during construction.
9898
* <p>
9999
* This method may be called multi-threaded. There is no guarantee as to the order that commands
@@ -114,8 +114,8 @@ public interface SlashCommand {
114114
void onReady(@NotNull ReadyEvent event);
115115

116116
/**
117-
* Triggered by the command system when a slash command corresponding to this implementation
118-
* (based on {@link #getData()}) has been triggered.
117+
* Triggered by the core system when a slash command corresponding to this implementation (based
118+
* on {@link #getData()}) has been triggered.
119119
* <p>
120120
* This method may be called multi-threaded. In particular, there are no guarantees that it will
121121
* be executed on the same thread repeatedly or on the same thread that other event methods have
@@ -127,7 +127,7 @@ public interface SlashCommand {
127127
* Buttons or menus have to be created with a component ID (see
128128
* {@link ComponentInteraction#getComponentId()},
129129
* {@link net.dv8tion.jda.api.interactions.components.Button#of(ButtonStyle, String, Emoji)}) in
130-
* a very specific format, otherwise the command system will fail to identify the command that
130+
* a very specific format, otherwise the core system will fail to identify the command that
131131
* corresponded to the button or menu click event and is unable to route it back.
132132
* <p>
133133
* The component ID has to be a UUID-string (see {@link java.util.UUID}), which is associated to
@@ -154,7 +154,7 @@ public interface SlashCommand {
154154
void onSlashCommand(@NotNull SlashCommandEvent event);
155155

156156
/**
157-
* Triggered by the command system when a button corresponding to this implementation (based on
157+
* Triggered by the core system when a button corresponding to this implementation (based on
158158
* {@link #getData()}) has been clicked.
159159
* <p>
160160
* This method may be called multi-threaded. In particular, there are no guarantees that it will
@@ -174,7 +174,7 @@ public interface SlashCommand {
174174
void onButtonClick(@NotNull ButtonClickEvent event, @NotNull List<String> args);
175175

176176
/**
177-
* Triggered by the command system when a selection menu corresponding to this implementation
177+
* Triggered by the core system when a selection menu corresponding to this implementation
178178
* (based on {@link #getData()}) has been clicked.
179179
* <p>
180180
* This method may be called multi-threaded. In particular, there are no guarantees that it will
@@ -194,10 +194,10 @@ public interface SlashCommand {
194194
void onSelectionMenu(@NotNull SelectionMenuEvent event, @NotNull List<String> args);
195195

196196
/**
197-
* Triggered by the command system during its setup phase. It will provide the command a
198-
* component id generator through this method, which can be used to generate component ids, as
199-
* used for button or selection menus. See {@link #onSlashCommand(SlashCommandEvent)} for
200-
* details on how to use this.
197+
* Triggered by the core system during its setup phase. It will provide the command a component
198+
* id generator through this method, which can be used to generate component ids, as used for
199+
* button or selection menus. See {@link #onSlashCommand(SlashCommandEvent)} for details on how
200+
* to use this.
201201
*
202202
* @param generator the provided component id generator
203203
*/

application/src/main/java/org/togetherjava/tjbot/commands/SlashCommandAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* Adapter implementation of a {@link SlashCommand}. The minimal setup only requires implementation
1919
* of {@link #onSlashCommand(SlashCommandEvent)}. A new command can then be registered by adding it
20-
* to {@link Commands}.
20+
* to {@link Features}.
2121
* <p>
2222
* Further, {@link #onButtonClick(ButtonClickEvent, List)} and
2323
* {@link #onSelectionMenu(SelectionMenuEvent, List)} can be overridden if desired. The default
@@ -54,7 +54,7 @@
5454
* }
5555
* </pre>
5656
* <p>
57-
* and registration of an instance of that class in {@link Commands}.
57+
* and registration of an instance of that class in {@link Features}.
5858
*/
5959
public abstract class SlashCommandAdapter implements SlashCommand {
6060
private final String name;

application/src/main/java/org/togetherjava/tjbot/commands/moderation/RejoinMuteListener.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
import net.dv8tion.jda.api.entities.Guild;
44
import net.dv8tion.jda.api.entities.IPermissionHolder;
55
import net.dv8tion.jda.api.entities.Member;
6+
import net.dv8tion.jda.api.events.GenericEvent;
67
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
7-
import net.dv8tion.jda.api.hooks.ListenerAdapter;
88
import org.jetbrains.annotations.NotNull;
99
import org.slf4j.Logger;
1010
import org.slf4j.LoggerFactory;
11+
import org.togetherjava.tjbot.commands.EventReceiver;
1112

12-
import javax.annotation.Nonnull;
1313
import java.time.Instant;
14-
import java.util.*;
14+
import java.util.Objects;
15+
import java.util.Optional;
1516

1617
/**
1718
* Reapplies existing mutes to users who have left and rejoined a guild.
@@ -21,7 +22,7 @@
2122
* join events and reapplies the mute role in case the user is supposed to be muted still (according
2223
* to the {@link ModerationActionsStore}).
2324
*/
24-
public final class RejoinMuteListener extends ListenerAdapter {
25+
public final class RejoinMuteListener implements EventReceiver {
2526
private static final Logger logger = LoggerFactory.getLogger(RejoinMuteListener.class);
2627

2728
private final ModerationActionsStore actionsStore;
@@ -52,7 +53,13 @@ private static boolean isActionEffective(@NotNull ActionRecord action) {
5253
}
5354

5455
@Override
55-
public void onGuildMemberJoin(@Nonnull GuildMemberJoinEvent event) {
56+
public void onEvent(@NotNull GenericEvent event) {
57+
if (event instanceof GuildMemberJoinEvent joinEvent) {
58+
onGuildMemberJoin(joinEvent);
59+
}
60+
}
61+
62+
private void onGuildMemberJoin(@NotNull GuildMemberJoinEvent event) {
5663
Member member = event.getMember();
5764
if (!shouldMemberBeMuted(member)) {
5865
return;

application/src/main/java/org/togetherjava/tjbot/commands/package-info.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* This package contains the command system and most commands of the bot. Commands can also be
33
* created in different modules, if desired.
44
* <p>
5-
* Commands are registered in {@link org.togetherjava.tjbot.commands.Commands} and then picked up by
6-
* the {@link org.togetherjava.tjbot.commands.system.CommandSystem}.
5+
* Commands are registered in {@link org.togetherjava.tjbot.commands.Features} and then picked up by
6+
* the {@link org.togetherjava.tjbot.commands.system.BotCore}.
77
* <p>
88
* Custom slash commands can be created by implementing
99
* {@link org.togetherjava.tjbot.commands.SlashCommand} or using the adapter

0 commit comments

Comments
 (0)