Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public static void main(final String[] args) {
* @param token the Discord Bot token to connect with
* @param databasePath the path to the database to use
*/
@SuppressWarnings("WeakerAccess")
public static void runBot(String token, Path databasePath) {
logger.info("Starting bot...");
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
* main logic to take over.
*/
public enum BootstrapLauncher {

;

/**
* Starts the main application.
*
* @param args arguments are forwarded, see {@link Application#main(String[])}
*/
public static void main(String[] args) {
setSystemProperties();

Expand All @@ -29,6 +33,7 @@ private static void setSystemProperties() {
// NOTE This will likely be fixed with Java 18 or newer, remove afterwards (see
// https://bugs.openjdk.java.net/browse/JDK-8274349 and
// https://github.com/openjdk/jdk/pull/5784)
// noinspection UseOfSystemOutOrSystemErr
System.out.println("Available Cores \"" + cores + "\", setting Parallelism Flag");
// noinspection AccessOfSystemProperties
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import org.togetherjava.tjbot.commands.basic.DatabaseCommand;
import org.togetherjava.tjbot.commands.basic.PingCommand;
import org.togetherjava.tjbot.commands.mathcommands.TeXCommand;
import org.togetherjava.tjbot.commands.tags.TagCommand;
import org.togetherjava.tjbot.commands.tags.TagManageCommand;
import org.togetherjava.tjbot.commands.tags.TagSystem;
import org.togetherjava.tjbot.commands.tags.TagsCommand;
import org.togetherjava.tjbot.db.Database;

import java.util.Collection;
Expand All @@ -27,14 +31,16 @@ public enum Commands {
* generally should be avoided.
*
* @param database the database of the application, which commands can use to persist data
*
* @return a collection of all slash commands
*/
public static @NotNull Collection<SlashCommand> createSlashCommands(
@NotNull Database database) {
TagSystem tagSystem = new TagSystem(database);
// NOTE The command system can add special system relevant commands also by itself,
// hence this list may not necessarily represent the full list of all commands actually
// available.
return List.of(new PingCommand(), new DatabaseCommand(database), new TeXCommand());
return List.of(new PingCommand(), new DatabaseCommand(database), new TeXCommand(),
new TagCommand(tagSystem), new TagManageCommand(tagSystem),
new TagsCommand(tagSystem));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public interface SlashCommand {

/**
* Triggered by the command system when a slash command corresponding to this implementation
* (based on {@link #getData()} has been triggered.
* (based on {@link #getData()}) has been triggered.
* <p>
* This method may be called multi-threaded. In particular, there are no guarantees that it will
* be executed on the same thread repeatedly or on the same thread that other event methods have
Expand Down Expand Up @@ -162,7 +162,7 @@ public interface SlashCommand {

/**
* Triggered by the command system when a button corresponding to this implementation (based on
* {@link #getData()} has been clicked.
* {@link #getData()}) has been clicked.
* <p>
* This method may be called multi-threaded. In particular, there are no guarantees that it will
* be executed on the same thread repeatedly or on the same thread that other event methods have
Expand All @@ -182,7 +182,7 @@ public interface SlashCommand {

/**
* Triggered by the command system when a selection menu corresponding to this implementation
* (based on {@link #getData()} has been clicked.
* (based on {@link #getData()}) has been clicked.
* <p>
* This method may be called multi-threaded. In particular, there are no guarantees that it will
* be executed on the same thread repeatedly or on the same thread that other event methods have
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* <p>
* <p>
* The adapter manages all command related data itself, which can be provided during construction
* (see {@link #SlashCommandAdapter(String, String, SlashCommandVisibility)}. In order to add
* (see {@link #SlashCommandAdapter(String, String, SlashCommandVisibility)}). In order to add
* options, subcommands or similar command configurations, use {@link #getData()} and mutate the
* returned data object (see {@link CommandData} for details on how to work with this class).
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ public void onButtonClick(@NotNull ButtonClickEvent event, @NotNull List<String>

ButtonStyle buttonStyle = Objects.requireNonNull(event.getButton()).getStyle();
switch (buttonStyle) {
case DANGER -> {
event.reply("Okay, will not reload.").queue();
}
case DANGER -> event.reply("Okay, will not reload.").queue();
case SUCCESS -> {
logger.info("Reloading commands, triggered by user '{}' in guild '{}'", userId,
event.getGuild());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.togetherjava.tjbot.commands.tags;

import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import org.jetbrains.annotations.NotNull;
import org.togetherjava.tjbot.commands.SlashCommandAdapter;
import org.togetherjava.tjbot.commands.SlashCommandVisibility;
import org.togetherjava.tjbot.commands.utils.MessageUtils;

import java.util.Objects;

/**
* Implements the {@code /tag} command which lets the bot respond content of a tag that has been
* added previously.
* <p>
* Tags can be added by using {@link TagManageCommand} and a list of all tags is available using
* {@link TagsCommand}.
*/
public final class TagCommand extends SlashCommandAdapter {
private final TagSystem tagSystem;

private static final String ID_OPTION = "id";

/**
* Creates a new instance, using the given tag system as base.
*
* @param tagSystem the system providing the actual tag data
*/
public TagCommand(TagSystem tagSystem) {
super("tag", "Display a tags content", SlashCommandVisibility.GUILD);

this.tagSystem = tagSystem;

// TODO Thing about adding an ephemeral selection menu with pagination support
// if the user calls this without id or similar
getData().addOption(OptionType.STRING, ID_OPTION, "the id of the tag to display", true);
}

@Override
public void onSlashCommand(@NotNull SlashCommandEvent event) {
String id = Objects.requireNonNull(event.getOption(ID_OPTION)).getAsString();
if (tagSystem.isUnknownTagAndHandle(id, event)) {
return;
}

event
.replyEmbeds(MessageUtils.generateEmbed(null, tagSystem.getTag(id).orElseThrow(),
event.getUser(), TagSystem.AMBIENT_COLOR))
.queue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.togetherjava.tjbot.commands.tags;

/**
* The style of a tag content.
*/
public enum TagContentStyle {
/**
* Content that will be interpreted by Discord, for example a message containing {@code **foo**}
* will be displayed in <b>bold</b>.
*/
INTERPRETED,
/**
* Content that will be displayed raw, not interpreted by Discord. For example a message
* containing {@code **foo**} will be displayed as {@code **foo**} literally, by escaping the
* special characters.
*/
RAW
}
Loading