Skip to content

Commit

Permalink
Merge branch 'develop' into oofs_and_lmaos
Browse files Browse the repository at this point in the history
  • Loading branch information
java-coding-prodigy authored Sep 29, 2023
2 parents 8fac872 + 6b9f572 commit d9a55c1
Show file tree
Hide file tree
Showing 17 changed files with 192 additions and 79 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/code-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -83,7 +83,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
#- name: Autobuild
# uses: github/codeql-action/autobuild@v1
# uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -95,4 +95,4 @@ jobs:
./gradlew build
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@v2
6 changes: 3 additions & 3 deletions application/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
buildscript {
dependencies {
classpath 'org.xerial:sqlite-jdbc:3.42.0.0'
classpath 'org.xerial:sqlite-jdbc:3.43.0.0'
}
}

plugins {
id 'application'
id 'com.google.cloud.tools.jib' version '3.3.0'
id 'com.google.cloud.tools.jib' version '3.4.0'
id 'com.github.johnrengelman.shadow' version '8.1.0'
id 'database-settings'
}
Expand Down Expand Up @@ -74,7 +74,7 @@ dependencies {

implementation 'com.github.ben-manes.caffeine:caffeine:3.1.1'

implementation 'org.kohsuke:github-api:1.315'
implementation 'org.kohsuke:github-api:1.316'

testImplementation 'org.mockito:mockito-core:5.3.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
Expand Down
19 changes: 16 additions & 3 deletions application/config.json.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
"databasePath": "local-database.db",
"projectWebsite": "https://github.com/Together-Java/TJ-Bot",
"discordGuildInvite": "https://discord.com/invite/XXFUXzK",
"modAuditLogChannelPattern": "mod_audit_log",
"modAuditLogChannelPattern": "mod-audit-log",
"modMailChannelPattern": "modmail",
"mutedRolePattern": "Muted",
"heavyModerationRolePattern": "Moderator",
"softModerationRolePattern": "Moderator|Community Ambassador",
"tagManageRolePattern": "Moderator|Community Ambassador|Top Helpers .+",
"excludeCodeAutoDetectionRolePattern": "Top Helpers .+|Moderator|Community Ambassador|Expert",
"suggestions": {
"channelPattern": "tj_suggestions",
"channelPattern": "tj-suggestions",
"upVoteEmoteName": "peepo_yes",
"downVoteEmoteName": "peepo_no"
},
Expand Down Expand Up @@ -89,11 +89,24 @@
"logInfoChannelWebhook": "<put_your_webhook_here>",
"logErrorChannelWebhook": "<put_your_webhook_here>",
"openaiApiKey": "<check pins in #tjbot_discussion for the key>",
"sourceCodeBaseUrl": "<https://github.com/<your_account_here>/<your_repo_here>/blob/master/application/src/main/java/>"
"sourceCodeBaseUrl": "https://github.com/Together-Java/TJ-Bot/blob/master/application/src/main/java/",
"jshell": {
"baseUrl": "<put_jshell_rest_api_url_here>",
"rateLimitWindowSeconds": 10,
"rateLimitRequestsInWindow": 3
},
"helperPruneConfig": {
"roleFullLimit": 100,
"roleFullThreshold": 95,
"pruneMemberAmount": 7,
"inactivateAfterDays": 90,
"recentlyJoinedDays": 4
},
"featureBlacklist": {
"normal": [
],
"special": [
]
}
"starboard": {
"emojiNames" : ["star"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public final class Config {
private final String sourceCodeBaseUrl;
private final JShellConfig jshell;
private final StarboardConfig starboard;
private final HelperPruneConfig helperPruneConfig;
private final FeatureBlacklistConfig featureBlacklistConfig;


@SuppressWarnings("ConstructorWithTooManyParameters")
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
Expand Down Expand Up @@ -78,7 +81,11 @@ private Config(@JsonProperty(value = "token", required = true) String token,
@JsonProperty(value = "openaiApiKey", required = true) String openaiApiKey,
@JsonProperty(value = "sourceCodeBaseUrl", required = true) String sourceCodeBaseUrl,
@JsonProperty(value = "jshell", required = true) JShellConfig jshell,
@JsonProperty(value = "starboard", required = true) StarboardConfig starboard) {
@JsonProperty(value = "starboard", required = true) StarboardConfig starboard,
@JsonProperty(value = "helperPruneConfig",
required = true) HelperPruneConfig helperPruneConfig,
@JsonProperty(value = "featureBlacklist",
required = true) FeatureBlacklistConfig featureBlacklistConfig) {
this.token = Objects.requireNonNull(token);
this.gistApiKey = Objects.requireNonNull(gistApiKey);
this.databasePath = Objects.requireNonNull(databasePath);
Expand All @@ -105,6 +112,8 @@ private Config(@JsonProperty(value = "token", required = true) String token,
this.sourceCodeBaseUrl = Objects.requireNonNull(sourceCodeBaseUrl);
this.jshell = Objects.requireNonNull(jshell);
this.starboard = Objects.requireNonNull(starboard);
this.helperPruneConfig = Objects.requireNonNull(helperPruneConfig);
this.featureBlacklistConfig = Objects.requireNonNull(featureBlacklistConfig);
}

/**
Expand Down Expand Up @@ -347,12 +356,30 @@ public JShellConfig getJshell() {
}

/**
* Gets the config for the Starboard Contains the List of emoji names recognized by the
* starboard as well as the name of the channel with the starboard.
*
* @return the config of the Starboard
*/
public StarboardConfig getStarboard() {
return starboard;

* Gets the config for automatic pruning of helper roles.
*
* @return the configuration
*/
public HelperPruneConfig getHelperPruneConfig() {
return helperPruneConfig;
}

/**
* The configuration of blacklisted features.
*
* @return configuration of blacklisted features
*/
public FeatureBlacklistConfig getFeatureBlacklistConfig() {
return featureBlacklistConfig;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.togetherjava.tjbot.config;

import com.fasterxml.jackson.annotation.JsonCreator;

import java.util.Set;

/**
* Blacklist of features, use {@link FeatureBlacklist#isEnabled(T)} to test if a feature is enabled.
* If a feature is blacklisted, it won't be enabled by the bot, and so will be ignored.
*
* @param <T> the type of the feature identifier
*/
public class FeatureBlacklist<T> {
private final Set<T> featureIdentifierBlacklist;

/**
* Creates a feature blacklist
*
* @param featureIdentifierBlacklist a set of identifiers which are blacklisted
*/
@JsonCreator
public FeatureBlacklist(Set<T> featureIdentifierBlacklist) {
this.featureIdentifierBlacklist = Set.copyOf(featureIdentifierBlacklist);
}

/**
* Returns if a feature is enabled or not.
*
* @param featureId the identifier of the feature
* @return true if a feature is enabled, false otherwise
*/
public boolean isEnabled(T featureId) {
return !featureIdentifierBlacklist.contains(featureId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.togetherjava.tjbot.config;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Objects;

/**
* Configuration of the feature blacklist, any feature present here will be disabled.
*
* @param normal the normal features, which are present in
* {@link org.togetherjava.tjbot.features.Features}
* @param special the special features, which require special code
*/
public record FeatureBlacklistConfig(
@JsonProperty(value = "normal", required = true) FeatureBlacklist<Class<?>> normal,
@JsonProperty(value = "special", required = true) FeatureBlacklist<String> special) {

/**
* Creates a FeatureBlacklistConfig.
*
* @param normal the list of normal features, must be not null
* @param special the list of special features, must be not null
*/
public FeatureBlacklistConfig {
Objects.requireNonNull(normal);
Objects.requireNonNull(special);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.togetherjava.tjbot.config;


/**
* Config for automatic pruning of helper roles, see
* {@link org.togetherjava.tjbot.features.help.AutoPruneHelperRoutine}.
*
* @param roleFullLimit if a helper role contains that many users, it is considered full and pruning
* must occur
* @param roleFullThreshold if a helper role contains that many users, pruning will start to occur
* to prevent reaching the limit
* @param pruneMemberAmount amount of users to remove from helper roles during a prune
* @param inactivateAfterDays after how many days of inactivity a user is eligible for pruning
* @param recentlyJoinedDays if a user is with the server for just this amount of days, they are
* protected from pruning
*/
public record HelperPruneConfig(int roleFullLimit, int roleFullThreshold, int pruneMemberAmount,
int inactivateAfterDays, int recentlyJoinedDays) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import net.dv8tion.jda.api.JDA;

import org.togetherjava.tjbot.config.Config;
import org.togetherjava.tjbot.config.FeatureBlacklist;
import org.togetherjava.tjbot.config.FeatureBlacklistConfig;
import org.togetherjava.tjbot.db.Database;
import org.togetherjava.tjbot.features.basic.PingCommand;
import org.togetherjava.tjbot.features.basic.RoleSelectCommand;
Expand All @@ -19,32 +21,13 @@
import org.togetherjava.tjbot.features.code.CodeMessageHandler;
import org.togetherjava.tjbot.features.code.CodeMessageManualDetection;
import org.togetherjava.tjbot.features.filesharing.FileSharingMessageListener;
import org.togetherjava.tjbot.features.help.AutoPruneHelperRoutine;
import org.togetherjava.tjbot.features.help.GuildLeaveCloseThreadListener;
import org.togetherjava.tjbot.features.help.HelpSystemHelper;
import org.togetherjava.tjbot.features.help.HelpThreadActivityUpdater;
import org.togetherjava.tjbot.features.help.HelpThreadAutoArchiver;
import org.togetherjava.tjbot.features.help.HelpThreadCommand;
import org.togetherjava.tjbot.features.help.HelpThreadCreatedListener;
import org.togetherjava.tjbot.features.help.HelpThreadMetadataPurger;
import org.togetherjava.tjbot.features.help.*;
import org.togetherjava.tjbot.features.jshell.JShellCommand;
import org.togetherjava.tjbot.features.jshell.JShellEval;
import org.togetherjava.tjbot.features.mathcommands.TeXCommand;
import org.togetherjava.tjbot.features.mathcommands.wolframalpha.WolframAlphaCommand;
import org.togetherjava.tjbot.features.mediaonly.MediaOnlyChannelListener;
import org.togetherjava.tjbot.features.moderation.BanCommand;
import org.togetherjava.tjbot.features.moderation.KickCommand;
import org.togetherjava.tjbot.features.moderation.ModerationActionsStore;
import org.togetherjava.tjbot.features.moderation.MuteCommand;
import org.togetherjava.tjbot.features.moderation.NoteCommand;
import org.togetherjava.tjbot.features.moderation.QuarantineCommand;
import org.togetherjava.tjbot.features.moderation.RejoinModerationRoleListener;
import org.togetherjava.tjbot.features.moderation.ReportCommand;
import org.togetherjava.tjbot.features.moderation.UnbanCommand;
import org.togetherjava.tjbot.features.moderation.UnmuteCommand;
import org.togetherjava.tjbot.features.moderation.UnquarantineCommand;
import org.togetherjava.tjbot.features.moderation.WarnCommand;
import org.togetherjava.tjbot.features.moderation.WhoIsCommand;
import org.togetherjava.tjbot.features.moderation.*;
import org.togetherjava.tjbot.features.moderation.attachment.BlacklistedAttachmentListener;
import org.togetherjava.tjbot.features.moderation.audit.AuditCommand;
import org.togetherjava.tjbot.features.moderation.audit.ModAuditLogRoutine;
Expand Down Expand Up @@ -94,14 +77,16 @@ private Features() {
* @return a collection of all features
*/
public static Collection<Feature> createFeatures(JDA jda, Database database, Config config) {
FeatureBlacklistConfig blacklistConfig = config.getFeatureBlacklistConfig();
JShellEval jshellEval = new JShellEval(config.getJshell());

TagSystem tagSystem = new TagSystem(database);
BookmarksSystem bookmarksSystem = new BookmarksSystem(config, database);
ModerationActionsStore actionsStore = new ModerationActionsStore(database);
ModAuditLogWriter modAuditLogWriter = new ModAuditLogWriter(config);
ScamHistoryStore scamHistoryStore = new ScamHistoryStore(database);
CodeMessageHandler codeMessageHandler = new CodeMessageHandler(jshellEval);
CodeMessageHandler codeMessageHandler =
new CodeMessageHandler(blacklistConfig.special(), jshellEval);
ChatGptService chatGptService = new ChatGptService(config);
HelpSystemHelper helpSystemHelper = new HelpSystemHelper(config, database, chatGptService);

Expand Down Expand Up @@ -174,6 +159,8 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
features.add(new BookmarksCommand(bookmarksSystem));
features.add(new ChatGptCommand(chatGptService));
features.add(new JShellCommand(jshellEval));
return features;

FeatureBlacklist<Class<?>> blacklist = blacklistConfig.normal();
return features.stream().filter(f -> blacklist.isEnabled(f.getClass())).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ public class ChatGptService {
private static final Duration TIMEOUT = Duration.ofSeconds(90);
private static final int MAX_TOKENS = 3_000;
private static final String AI_MODEL = "gpt-3.5-turbo";

private boolean isDisabled = false;
private final OpenAiService openAiService;
private OpenAiService openAiService;

/**
* Creates instance of ChatGPTService
Expand All @@ -33,8 +34,10 @@ public class ChatGptService {
*/
public ChatGptService(Config config) {
String apiKey = config.getOpenaiApiKey();
if (apiKey.isBlank()) {
boolean keyIsDefaultDescription = apiKey.startsWith("<") && apiKey.endsWith(">");
if (apiKey.isBlank() || keyIsDefaultDescription) {
isDisabled = true;
return;
}

openAiService = new OpenAiService(apiKey, TIMEOUT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.togetherjava.tjbot.config.FeatureBlacklist;
import org.togetherjava.tjbot.features.MessageReceiverAdapter;
import org.togetherjava.tjbot.features.UserInteractionType;
import org.togetherjava.tjbot.features.UserInteractor;
Expand All @@ -30,6 +31,7 @@
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Handles code in registered messages and offers code actions to the user, such as formatting their
Expand Down Expand Up @@ -63,14 +65,18 @@ public final class CodeMessageHandler extends MessageReceiverAdapter implements

/**
* Creates a new instance.
*
*
* @param blacklist the feature blacklist, used to test if certain code actions should be
* disabled
* @param jshellEval used to execute java code and build visual result
*/
public CodeMessageHandler(JShellEval jshellEval) {
public CodeMessageHandler(FeatureBlacklist<String> blacklist, JShellEval jshellEval) {
componentIdInteractor = new ComponentIdInteractor(getInteractionType(), getName());

List<CodeAction> codeActions =
List.of(new FormatCodeCommand(), new EvalCodeCommand(jshellEval));
Stream.of(new FormatCodeCommand(), new EvalCodeCommand(jshellEval))
.filter(a -> blacklist.isEnabled(a.getClass().getSimpleName()))
.toList();

labelToCodeAction = codeActions.stream()
.collect(Collectors.toMap(CodeAction::getLabel, Function.identity(), (x, y) -> y,
Expand Down
Loading

0 comments on commit d9a55c1

Please sign in to comment.