Skip to content

Commit

Permalink
Add config for Dadbot chance (#1076)
Browse files Browse the repository at this point in the history
* Add config for Dadbot chance

* Update tests to show actual reply
  • Loading branch information
Nincodedo authored Dec 11, 2024
1 parent 11a5132 commit 9a08d71
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.nincodedo.nincord.LocaleService;
import dev.nincodedo.nincord.StatAwareListenerAdapter;
import dev.nincodedo.nincord.config.db.Config;
import dev.nincodedo.nincord.config.db.ConfigConstants;
import dev.nincodedo.nincord.config.db.ConfigService;
import dev.nincodedo.nincord.config.db.component.ComponentService;
Expand All @@ -18,6 +19,7 @@

import java.security.SecureRandom;
import java.util.Locale;
import java.util.Objects;
import java.util.Random;
import java.util.ResourceBundle;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -63,15 +65,15 @@ private MessageExecutor parseMessage(MessageReceivedEvent event) {
if (optionalDadJoke.isEmpty() || !event.getChannelType().isGuild()) {
return messageExecutor;
}
if (checkChance()) {
if (checkChance(event.getGuild().getId())) {
hiImDad(event, messageExecutor, optionalDadJoke.get());
}
return messageExecutor;
}

private boolean channelIsOnDenyList(String guildId, String channelId) {
var channelConfigList = configService.getConfigByName(guildId, ConfigConstants.DADBOT_DENY_LIST_CHANNEL);
return channelConfigList.stream().anyMatch(config -> config.getValue().equals(channelId));
return channelConfigList.stream().anyMatch(config -> channelId.equals(config.getValue()));
}

private void hiImDad(MessageReceivedEvent event,
Expand Down Expand Up @@ -104,7 +106,14 @@ private void dadJoke(String dadName, Member member) {
.queueAfter(2, TimeUnit.MINUTES));
}

private boolean checkChance() {
return random.nextInt(100) < 10;
private boolean checkChance(String guildId) {
int chance = configService.getGlobalConfigByName(ConfigConstants.DADBOT_REPLY_CHANCE, guildId)
.stream()
.map(Config::getValue)
.filter(Objects::nonNull)
.map(Integer::parseInt)
.findFirst()
.orElse(10);
return random.nextInt(100) < chance;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.nincodedo.ninbot.components.dad;

import dev.nincodedo.nincord.LocaleService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -13,7 +14,7 @@ class DadbotMessageParserTest {

DadbotMessageParser dadbotMessageParser = new DadbotMessageParser();

public static List<String> dadreplies() {
public static List<String> dadReplies() {
return List.of("I'm happy", "im glad", "i'm tired", "hi im sad", "I'm ready for anything", "I'm a really long"
+ " message with another sentence in it. This should still reply correctly.",
"This is also a really long message with another sentence in it, but the dad part doesn't happen "
Expand All @@ -30,20 +31,21 @@ public static List<String> dadreplies() {
+ "I'm sure in the end it will.");
}

public static List<String> nodadreplies() {
public static List<String> noDadReplies() {
return List.of("I'm", "I'm ", "Just words that don't have that word");
}

@ParameterizedTest
@MethodSource("dadreplies")
@MethodSource("dadReplies")
void dadReply(String message) {
var actual = dadbotMessageParser.dadReply(message, "");
assertThat(actual).isPresent();
log.debug(actual.get());
log.debug(String.format(LocaleService.getResourceBundleOrDefault(null)
.getString("listener.dad.joke"), actual.get()));
}

@ParameterizedTest
@MethodSource("nodadreplies")
@MethodSource("noDadReplies")
void noDadReply(String message) {
var actual = dadbotMessageParser.dadReply(message, "");
assertThat(actual).isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public class ConfigConstants {
public static final String ANNOUNCE_CHANNEL = "announcementChannel";
public static final String DADBOT_DENY_LIST_CHANNEL = "dadbotChannelDenyList";
public static final String DADBOT_REPLY_CHANCE = "dadbotReplyChance";
public static final String STREAMING_ANNOUNCE_CHANNEL = "streamingAnnounceChannel";
public static final String ROLE_DENY_LIST = "roleDenyList";
public static final String STREAMING_ROLE = "streamingRole";
Expand Down

0 comments on commit 9a08d71

Please sign in to comment.