Skip to content

Commit 35ba891

Browse files
committed
Merge branch 'develop' into feature/cool-messages
2 parents e90dc35 + 5ac1e07 commit 35ba891

File tree

15 files changed

+145
-167
lines changed

15 files changed

+145
-167
lines changed

.github/CODEOWNERS

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* @Together-Java/moderators @Together-Java/maintainers
1+
* @Together-Java/maintainers

application/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ dependencies {
7777

7878
implementation 'org.kohsuke:github-api:1.319'
7979

80-
testImplementation 'org.mockito:mockito-core:5.3.1'
80+
testImplementation 'org.mockito:mockito-core:5.10.0'
8181
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
8282
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.10.0'
8383
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

application/config.json.template

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
"special": [
111111
]
112112
},
113+
"memberCountCategoryPattern": "Info",
113114
"selectRolesChannelPattern": "select-your-roles",
114115
"coolMessagesConfig": {
115116
"minimumReactions": 1,

application/src/main/java/org/togetherjava/tjbot/config/Config.java

+13
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public final class Config {
4444
private final HelperPruneConfig helperPruneConfig;
4545
private final FeatureBlacklistConfig featureBlacklistConfig;
4646
private final String selectRolesChannelPattern;
47+
private final String memberCountCategoryPattern;
4748
private final CoolMessagesBoardConfig coolMessagesConfig;
4849

4950
@SuppressWarnings("ConstructorWithTooManyParameters")
@@ -87,6 +88,8 @@ private Config(@JsonProperty(value = "token", required = true) String token,
8788
@JsonProperty(value = "openaiApiKey", required = true) String openaiApiKey,
8889
@JsonProperty(value = "sourceCodeBaseUrl", required = true) String sourceCodeBaseUrl,
8990
@JsonProperty(value = "jshell", required = true) JShellConfig jshell,
91+
@JsonProperty(value = "memberCountCategoryPattern",
92+
required = true) String memberCountCategoryPattern,
9093
@JsonProperty(value = "helperPruneConfig",
9194
required = true) HelperPruneConfig helperPruneConfig,
9295
@JsonProperty(value = "featureBlacklist",
@@ -99,6 +102,7 @@ private Config(@JsonProperty(value = "token", required = true) String token,
99102
this.githubApiKey = Objects.requireNonNull(githubApiKey);
100103
this.databasePath = Objects.requireNonNull(databasePath);
101104
this.projectWebsite = Objects.requireNonNull(projectWebsite);
105+
this.memberCountCategoryPattern = Objects.requireNonNull(memberCountCategoryPattern);
102106
this.discordGuildInvite = Objects.requireNonNull(discordGuildInvite);
103107
this.modAuditLogChannelPattern = Objects.requireNonNull(modAuditLogChannelPattern);
104108
this.modMailChannelPattern = Objects.requireNonNull(modMailChannelPattern);
@@ -410,6 +414,15 @@ public String getSelectRolesChannelPattern() {
410414
return selectRolesChannelPattern;
411415
}
412416

417+
/**
418+
* Gets the pattern matching the category that is used to display the total member count.
419+
*
420+
* @return the categories name types
421+
*/
422+
public String getMemberCountCategoryPattern() {
423+
return memberCountCategoryPattern;
424+
}
425+
413426
/**
414427
* The configuration of the cool messages config.
415428
*

application/src/main/java/org/togetherjava/tjbot/features/Features.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.togetherjava.tjbot.config.FeatureBlacklist;
99
import org.togetherjava.tjbot.config.FeatureBlacklistConfig;
1010
import org.togetherjava.tjbot.db.Database;
11+
import org.togetherjava.tjbot.features.basic.MemberCountDisplayRoutine;
1112
import org.togetherjava.tjbot.features.basic.CoolMessagesBoardManager;
1213
import org.togetherjava.tjbot.features.basic.PingCommand;
1314
import org.togetherjava.tjbot.features.basic.RoleSelectCommand;
@@ -110,6 +111,7 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
110111
.add(new AutoPruneHelperRoutine(config, helpSystemHelper, modAuditLogWriter, database));
111112
features.add(new HelpThreadAutoArchiver(helpSystemHelper));
112113
features.add(new LeftoverBookmarksCleanupRoutine(bookmarksSystem));
114+
features.add(new MemberCountDisplayRoutine(config));
113115

114116
// Message receivers
115117
features.add(new TopHelpersMessageListener(database, config));
@@ -164,7 +166,7 @@ public static Collection<Feature> createFeatures(JDA jda, Database database, Con
164166
features.add(new HelpThreadCommand(config, helpSystemHelper));
165167
features.add(new ReportCommand(config));
166168
features.add(new BookmarksCommand(bookmarksSystem));
167-
features.add(new ChatGptCommand(chatGptService));
169+
features.add(new ChatGptCommand(chatGptService, helpSystemHelper));
168170
features.add(new JShellCommand(jshellEval));
169171

170172
FeatureBlacklist<Class<?>> blacklist = blacklistConfig.normal();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.togetherjava.tjbot.features.basic;
2+
3+
import net.dv8tion.jda.api.JDA;
4+
import net.dv8tion.jda.api.entities.channel.concrete.Category;
5+
6+
import org.togetherjava.tjbot.config.Config;
7+
import org.togetherjava.tjbot.features.Routine;
8+
9+
import java.util.concurrent.TimeUnit;
10+
import java.util.function.Predicate;
11+
import java.util.regex.Pattern;
12+
13+
/**
14+
* Shows the guild member count on selected category, which updates everyday.
15+
*/
16+
public class MemberCountDisplayRoutine implements Routine {
17+
private final Predicate<String> memberCountCategoryPredicate;
18+
19+
/**
20+
* Creates an instance on member count display routine.
21+
*
22+
* @param config the config to use
23+
*/
24+
public MemberCountDisplayRoutine(Config config) {
25+
memberCountCategoryPredicate =
26+
Pattern.compile(config.getMemberCountCategoryPattern() + "( - \\d+ Members)?")
27+
.asMatchPredicate();
28+
}
29+
30+
private void updateCategoryName(Category category) {
31+
int totalMemberCount = category.getGuild().getMemberCount();
32+
String baseName = category.getName().split("-")[0].trim();
33+
34+
category.getManager()
35+
.setName("%s - %d Members".formatted(baseName, totalMemberCount))
36+
.queue();
37+
}
38+
39+
@Override
40+
public Schedule createSchedule() {
41+
return new Schedule(ScheduleMode.FIXED_RATE, 0, 24, TimeUnit.HOURS);
42+
}
43+
44+
@Override
45+
public void runRoutine(JDA jda) {
46+
jda.getGuilds()
47+
.forEach(guild -> guild.getCategories()
48+
.stream()
49+
.filter(category -> memberCountCategoryPredicate.test(category.getName()))
50+
.findAny()
51+
.ifPresent(this::updateCategoryName));
52+
}
53+
}

application/src/main/java/org/togetherjava/tjbot/features/chatgpt/AIResponseParser.java

-82
This file was deleted.

application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptCommand.java

+18-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.github.benmanes.caffeine.cache.Cache;
44
import com.github.benmanes.caffeine.cache.Caffeine;
5+
import net.dv8tion.jda.api.entities.MessageEmbed;
6+
import net.dv8tion.jda.api.entities.SelfUser;
57
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
68
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
79
import net.dv8tion.jda.api.interactions.components.Modal;
@@ -10,6 +12,7 @@
1012

1113
import org.togetherjava.tjbot.features.CommandVisibility;
1214
import org.togetherjava.tjbot.features.SlashCommandAdapter;
15+
import org.togetherjava.tjbot.features.help.HelpSystemHelper;
1316

1417
import java.time.Duration;
1518
import java.time.Instant;
@@ -28,6 +31,7 @@ public final class ChatGptCommand extends SlashCommandAdapter {
2831
private static final int MIN_MESSAGE_INPUT_LENGTH = 4;
2932
private static final Duration COMMAND_COOLDOWN = Duration.of(10, ChronoUnit.SECONDS);
3033
private final ChatGptService chatGptService;
34+
private final HelpSystemHelper helper;
3135

3236
private final Cache<String, Instant> userIdToAskedAtCache =
3337
Caffeine.newBuilder().maximumSize(1_000).expireAfterWrite(COMMAND_COOLDOWN).build();
@@ -36,11 +40,13 @@ public final class ChatGptCommand extends SlashCommandAdapter {
3640
* Creates an instance of the chatgpt command.
3741
*
3842
* @param chatGptService ChatGptService - Needed to make calls to ChatGPT API
43+
* @param helper HelpSystemHelper - Needed to generate response embed for prompt
3944
*/
40-
public ChatGptCommand(ChatGptService chatGptService) {
45+
public ChatGptCommand(ChatGptService chatGptService, HelpSystemHelper helper) {
4146
super(COMMAND_NAME, "Ask the ChatGPT AI a question!", CommandVisibility.GUILD);
4247

4348
this.chatGptService = chatGptService;
49+
this.helper = helper;
4450
}
4551

4652
@Override
@@ -75,20 +81,23 @@ public void onModalSubmitted(ModalInteractionEvent event, List<String> args) {
7581
event.deferReply().queue();
7682

7783
String context = "";
78-
Optional<String[]> optional =
79-
chatGptService.ask(event.getValue(QUESTION_INPUT).getAsString(), context);
84+
String question = event.getValue(QUESTION_INPUT).getAsString();
85+
86+
Optional<String> optional = chatGptService.ask(question, context);
8087
if (optional.isPresent()) {
8188
userIdToAskedAtCache.put(event.getMember().getId(), Instant.now());
8289
}
8390

84-
String[] errorResponse = {"""
91+
String errorResponse = """
8592
An error has occurred while trying to communicate with ChatGPT.
8693
Please try again later.
87-
"""};
94+
""";
8895

89-
String[] response = optional.orElse(errorResponse);
90-
for (String message : response) {
91-
event.getHook().sendMessage(message).queue();
92-
}
96+
String response = optional.orElse(errorResponse);
97+
SelfUser selfUser = event.getJDA().getSelfUser();
98+
99+
MessageEmbed responseEmbed = helper.generateGptResponseEmbed(response, selfUser, question);
100+
101+
event.getHook().sendMessageEmbeds(responseEmbed).queue();
93102
}
94103
}

application/src/main/java/org/togetherjava/tjbot/features/chatgpt/ChatGptService.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ public ChatGptService(Config config) {
8787
* @param question The question being asked of ChatGPT. Max is {@value MAX_TOKENS} tokens.
8888
* @param context The category of asked question, to set the context(eg. Java, Database, Other
8989
* etc).
90-
* @return partitioned response from ChatGPT as a String array.
90+
* @return response from ChatGPT as a String.
9191
* @see <a href="https://platform.openai.com/docs/guides/chat/managing-tokens">ChatGPT
9292
* Tokens</a>.
9393
*/
94-
public Optional<String[]> ask(String question, String context) {
94+
public Optional<String> ask(String question, String context) {
9595
if (isDisabled) {
9696
return Optional.empty();
9797
}
@@ -121,7 +121,7 @@ public Optional<String[]> ask(String question, String context) {
121121
return Optional.empty();
122122
}
123123

124-
return Optional.of(AIResponseParser.parse(response));
124+
return Optional.of(response);
125125
} catch (OpenAiHttpException openAiHttpException) {
126126
logger.warn(
127127
"There was an error using the OpenAI API: {} Code: {} Type: {} Status Code: {}",

application/src/main/java/org/togetherjava/tjbot/features/filesharing/FileSharingMessageListener.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private void processAttachments(MessageReceivedEvent event,
134134
.build()
135135
.createGist()
136136
.public_(false)
137-
.description("Uploaded by " + event.getAuthor().getAsTag());
137+
.description("Uploaded by " + event.getAuthor().getName());
138138

139139
List<CompletableFuture<Void>> tasks = new ArrayList<>();
140140

0 commit comments

Comments
 (0)