Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions application/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,15 @@ dependencies {

implementation 'org.jooq:jooq:3.15.3'

implementation 'org.scilab.forge:jlatexmath:1.0.7'
implementation 'org.scilab.forge:jlatexmath-font-greek:1.0.7'
implementation 'org.scilab.forge:jlatexmath-font-cyrillic:1.0.7'

implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-csv:2.13.0'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.0'

testImplementation 'org.mockito:mockito-core:3.12.4'
testRuntimeOnly 'org.mockito:mockito-core:3.12.4'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}
Expand Down
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 @@ -3,6 +3,12 @@
import org.jetbrains.annotations.NotNull;
import org.togetherjava.tjbot.commands.basic.DatabaseCommand;
import org.togetherjava.tjbot.commands.basic.PingCommand;
import org.togetherjava.tjbot.commands.free.FreeCommand;
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 Down Expand Up @@ -30,9 +36,12 @@ public enum 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));
return List.of(new PingCommand(), new DatabaseCommand(database), new TeXCommand(),
new TagCommand(tagSystem), new TagManageCommand(tagSystem),
new TagsCommand(tagSystem), new FreeCommand());
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.togetherjava.tjbot.commands;

import net.dv8tion.jda.api.entities.Emoji;
import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.events.interaction.ButtonClickEvent;
import net.dv8tion.jda.api.events.interaction.SelectionMenuEvent;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.components.ButtonStyle;
import net.dv8tion.jda.api.interactions.components.ComponentInteraction;
import org.jetbrains.annotations.NotNull;
import org.togetherjava.tjbot.commands.system.ComponentIds;

import java.util.List;

Expand Down Expand Up @@ -88,9 +90,30 @@ public interface SlashCommand {
@NotNull
CommandData getData();

/**
* Triggered by the command system after system startup is complete. This can be used for
* initialisation actions that cannot occur during construction.
* <p>
* This method may be called multi-threaded. There is no guarantee as to the order that commands
* will get called and there is no guarantee which thread they will be called on or even that
* they will be called by the same thread.
* <p>
* There is also no guarantee that slashCommands will be registered on guilds before this is
* called. Do not use this method to interact with slashCommands.
* <p>
* Details are available in the given event and the event also enables implementations to
* respond to it.
* <p>
* This method will be called in a multi-threaded context and the event may not be hold valid
* forever.
*
* @param event the event that triggered this
*/
void onReady(@NotNull ReadyEvent event);

/**
* 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 All @@ -99,6 +122,10 @@ public interface SlashCommand {
* Details are available in the given event and the event also enables implementations to
* respond to it.
* <p>
* The helper {@link org.togetherjava.tjbot.commands.system.ComponentIds} can be used to
* generate valid IDs. Alternatively, if {@link SlashCommandAdapter} has been extended, it also
* offers a handy {@link SlashCommandAdapter#generateComponentId(String...)} method to ease the
* flow.
* <p>
* Buttons or menus have to be created with component ID (see
* {@link ComponentInteraction#getComponentId()},
Expand All @@ -121,10 +148,9 @@ public interface SlashCommand {
* {@link #onButtonClick(ButtonClickEvent, List)},
* {@link #onSelectionMenu(SelectionMenuEvent, List)}).
* <p>
* The helper {@link org.togetherjava.tjbot.commands.system.ComponentIds} can be used to
* generate valid IDs. Alternatively, if {@link SlashCommandAdapter} has been extended, it also
* offers a handy {@link SlashCommandAdapter#generateComponentId(String...)} method to ease the
* flow.
* The helper {@link ComponentIds} can be used to generate valid IDs. Alternatively, if
* {@link SlashCommandAdapter} has been extended, it also offers a handy
* {@link SlashCommandAdapter#generateComponentId(String...)} method to ease the flow.
* <p>
* <p>
* This method will be called in a multi-threaded context and the event may not be hold valid
Expand All @@ -136,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 @@ -156,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
@@ -1,6 +1,7 @@
package org.togetherjava.tjbot.commands;

import com.fasterxml.jackson.core.JsonProcessingException;
import net.dv8tion.jda.api.events.ReadyEvent;
import net.dv8tion.jda.api.events.interaction.ButtonClickEvent;
import net.dv8tion.jda.api.events.interaction.SelectionMenuEvent;
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
Expand All @@ -22,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 All @@ -34,13 +35,13 @@
* <p>
* Some example commands are available in {@link org.togetherjava.tjbot.commands.basic}. A minimal
* setup would consist of a class like
*
*
* <pre>
* {
* &#64;code
* class PingCommand extends SlashCommandAdapter {
* PingCommand() {
* super("ping", "responds with !pong", SlashCommandVisibility.GUILD);
* public class PingCommand extends SlashCommandAdapter {
* public PingCommand() {
* super("ping", "Responds with 'Pong!'", SlashCommandVisibility.GUILD);
* }
*
* &#64;Override
Expand All @@ -50,7 +51,7 @@
* }
* }
* </pre>
*
*
* and registration of an instance of that class in {@link Commands}.
*/
public abstract class SlashCommandAdapter implements SlashCommand {
Expand Down Expand Up @@ -97,6 +98,12 @@ protected SlashCommandAdapter(@NotNull String name, @NotNull String description,
return data;
}

@SuppressWarnings("NoopMethodInAbstractClass")
@Override
public void onReady(@NotNull ReadyEvent event) {
// Adapter does not react by default, subclasses may change this behavior
}

@SuppressWarnings("NoopMethodInAbstractClass")
@Override
public void onButtonClick(@NotNull ButtonClickEvent event, @NotNull List<String> args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package org.togetherjava.tjbot.commands.free;

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.TextChannel;
import org.jetbrains.annotations.NotNull;

import java.util.Objects;

/**
* Class that tracks the current free/busy status of a channel that requires monitoring.
*/
public final class ChannelStatus {
public static final boolean FREE = false;
public static final boolean BUSY = true;

private static final String FREE_STATUS = ":green_circle:";
private static final String BUSY_STATUS = ":red_circle:";

private final long channelID;
private volatile boolean isBusy;
private String name;

/**
* Creates an instance of a Channel Status.
* <p>
* This does not validate the id as that requires the JDA. Any ChannelStatus that gets created
* with an invalid id *should* be ignored and won't be invoked. (Since the Channel statuses are
* selected by retrieval of channels id's via the guild id, before retrieving the relevant
* ChannelStatuses).
*
* @param id the long id of the {@link net.dv8tion.jda.api.entities.TextChannel} to monitor.
*/
protected ChannelStatus(long id) {
channelID = id;
isBusy = true;
name = Long.toString(id);
}

/**
* Retrieves whether the channel is currently busy/free.
* <p>
* This value is volatile but is not thread safe in any other way. While statuses change
* frequently, each individual status instance *should* only be modified from a single source,
* since it represents only a single channel and modification will only be triggered by activity
* in that one channel.
*
* @return the current stored status related to the channel id.
*/
public boolean isBusy() {
return isBusy;
}

/**
* Retrieves the id for the {@link net.dv8tion.jda.api.entities.TextChannel} that this instance
* represents. There is no guarantee that the id is valid according to the {@link JDA}.
*
* @return the {@link net.dv8tion.jda.api.entities.TextChannel} id.
*/
public long getChannelID() {
return channelID;
}

/**
* Retrieves the locally stored name of the {@link net.dv8tion.jda.api.entities.TextChannel}
* this represents. This value is initialised to the channel id and as such is never null. The
* name should first be set by retrieving the name the {@link JDA} currently uses, before
* calling this.
* <p>
* The recommended value to use is {@link TextChannel#getAsMention()}.
*
* @return The currently stored name of the channel, originally the long id as string.
*/
public @NotNull String getName() {
return name;
}

/**
* Method used to keep the channel name up to date with the {@link JDA}. This method is not
* called automatically. Manually update before using the value.
* <p>
* The recommended value to use is {@link TextChannel#getAsMention()}
* <p>
* This method is called in multithreaded context, however the value is not expected to change
* regularly and will not break anything if its invalid so no has not been made thread safe.
*
* @param name the value to set the channel name to.
*/
public void setName(String name) {
this.name = Objects.requireNonNull(name);
}

protected void busy(boolean isBusy) {
this.isBusy = isBusy;
}

// todo should I overload equals with equals(long) so that a Set may be used instead of a Map
/**
* The identity of this object of solely based on the id value. Compares the long id's and
* determines if they are equal.
*
* @param o the other object to test against
* @return whether the objects have the same id or not.
*/
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
ChannelStatus that = (ChannelStatus) o;
return channelID == that.channelID;
}

/**
* A String representation of the instance, gives the name and the current status.
*
* @return a String representation of the instance.
*/
@Override
public String toString() {
return "ChannelStatus{ %s is %s }".formatted(name, isBusy ? "busy" : "not busy");
}

/**
* A {@link #toString()} method spetially formatted for Discord ({@link JDA}. Uses emojis by
* string representation.
*
* @return a String representation of ChannelStatus, formatted for Discord
*/
public String toDiscord() {
return "%s %s".formatted(isBusy ? BUSY_STATUS : FREE_STATUS, name);
}

/**
* The hash that represents the instance. It is based only on the id value.
*
* @return the instance's hash.
*/
@Override
public int hashCode() {
return Objects.hash(channelID);
}
}
Loading