Skip to content

Commit 2aa1ca0

Browse files
authored
add target channel option to channel_config command (#723)
1 parent 0ca44b2 commit 2aa1ca0

File tree

9 files changed

+395
-22
lines changed

9 files changed

+395
-22
lines changed

README.adoc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
= Button Dice Roller
22
:toc: macro
33

4-
https://www.gnu.org/licenses/agpl-3.0[image:https://img.shields.io/badge/License-AGPL_v3-blue.svg[License: AGPL v3]]
4+
https://www.gnu.org/licenses/agpl-3.0[image:https://img.shields.io/badge/License-AGPL_v3-blue.svg[License: AGPL v3]]
55
https://codecov.io/gh/twonirwana/DiscordDiceBot[image:https://codecov.io/gh/twonirwana/DiscordDiceBot/branch/main/graph/badge.svg?token=OLH7L312D7[codecov]]
66
image:https://img.shields.io/github/actions/workflow/status/twonirwana/DiscordDiceBot/codeCov.yml?branch=main[GitHub Workflow Status]
77
image:https://img.shields.io/discord/898657305883725834[link="https://discord.gg/e43BsqKpFr"]
@@ -598,9 +598,14 @@ The output can be configured with the `channel_config` command.
598598
[#_channel_config]
599599
=== Channel Config
600600

601-
This command is used create a channel-specific configuration.
601+
This command is used to create a channel-specific configuration.
602602
If a channel config exists in a channel where a thread is created, then the configuration, at that point in time, will be copied into the new thread.
603603

604+
All channel config commands have a optional `channel` option.
605+
If the `channel` option is kept empty then the command will be applied to the current channel.
606+
If a channel is specified in the `channel` option then the command will be applied in the specified channel.
607+
This is especially useful if the `channel` option is a forum because then all posts all new posts in the forum get a copy of the channel configuration/alias.
608+
604609
It is possible to configure alias and the output for the direct rolls:
605610

606611
==== Direct Roll Config

bot/src/main/java/de/janno/discord/bot/command/channelConfig/ChannelConfigCommand.java

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ public class ChannelConfigCommand implements SlashCommand {
3232
public final static String REPLACE_NAME_VALUE_DELIMITER = ":";
3333
public final static String REGEX_NAME_VALUE_DELIMITER = "::";
3434
public final static String NAME_VALUE_DELIMITER = ";";
35+
public static final String CHANNEL_OPTION_NAME = "channel";
36+
public static final CommandDefinitionOption CHANNEL_COMMAND_OPTION = CommandDefinitionOption.builder()
37+
.name(CHANNEL_OPTION_NAME)
38+
.nameLocales(I18n.allNoneEnglishMessagesNames("base.option.channel.name"))
39+
.description(I18n.getMessage("base.option.channel.description", Locale.ENGLISH))
40+
.descriptionLocales(I18n.allNoneEnglishMessagesDescriptions("base.option.channel.description"))
41+
.type(CommandDefinitionOption.Type.CHANNEL)
42+
.build();
3543
static final String ALWAYS_SUM_RESULTS_OPTION_NAME = "always_sum_result";
3644
private static final String SAVE_DIRECT_ROLL_CONFIG_OPTION_NAME = "save_direct_roll_config";
3745
private static final String DELETE_DIRECT_ROLL_CONFIG_OPTION_NAME = "delete_direct_roll_config";
@@ -81,6 +89,7 @@ public class ChannelConfigCommand implements SlashCommand {
8189
.required(true)
8290
.build())
8391
.option(SCOPE_OPTION)
92+
.option(CHANNEL_COMMAND_OPTION)
8493
.build();
8594
private static final CommandDefinitionOption DELETE_ALIAS_OPTION = CommandDefinitionOption.builder()
8695
.type(CommandDefinitionOption.Type.SUB_COMMAND)
@@ -98,6 +107,7 @@ public class ChannelConfigCommand implements SlashCommand {
98107
.required(true)
99108
.autoComplete(true)
100109
.build())
110+
.option(CHANNEL_COMMAND_OPTION)
101111
.build();
102112
private static final CommandDefinitionOption DELETE_ALL_ALIAS_OPTION = CommandDefinitionOption.builder()
103113
.type(CommandDefinitionOption.Type.SUB_COMMAND)
@@ -106,6 +116,7 @@ public class ChannelConfigCommand implements SlashCommand {
106116
.description(I18n.getMessage("channel_config.option.delete.all.description", Locale.ENGLISH))
107117
.descriptionLocales(I18n.allNoneEnglishMessagesDescriptions("channel_config.option.delete.all.description"))
108118
.option(SCOPE_OPTION)
119+
.option(CHANNEL_COMMAND_OPTION)
109120
.build();
110121
private static final CommandDefinitionOption LIST_ALIAS_OPTION = CommandDefinitionOption.builder()
111122
.type(CommandDefinitionOption.Type.SUB_COMMAND)
@@ -114,6 +125,7 @@ public class ChannelConfigCommand implements SlashCommand {
114125
.description(I18n.getMessage("channel_config.option.list.description", Locale.ENGLISH))
115126
.descriptionLocales(I18n.allNoneEnglishMessagesDescriptions("channel_config.option.list.description"))
116127
.option(SCOPE_OPTION)
128+
.option(CHANNEL_COMMAND_OPTION)
117129
.build();
118130
private static final CommandDefinitionOption ALIAS_TYPE_OPTION = CommandDefinitionOption.builder()
119131
.type(CommandDefinitionOption.Type.STRING)
@@ -157,6 +169,7 @@ public class ChannelConfigCommand implements SlashCommand {
157169
.build())
158170
.option(SCOPE_OPTION)
159171
.option(ALIAS_TYPE_OPTION)
172+
.option(CHANNEL_COMMAND_OPTION)
160173
.build();
161174
private final PersistenceManager persistenceManager;
162175

@@ -214,13 +227,15 @@ public static List<Alias> parseStringToMultiAliasList(String aliasesString) {
214227
.type(CommandDefinitionOption.Type.BOOLEAN)
215228
.required(false)
216229
.build())
230+
.option(CHANNEL_COMMAND_OPTION)
217231
.build())
218232
.option(CommandDefinitionOption.builder()
219233
.name(DELETE_DIRECT_ROLL_CONFIG_OPTION_NAME)
220234
.nameLocales(I18n.allNoneEnglishMessagesNames("channel_config.option.delete_direct_roll_config.name"))
221235
.description(I18n.getMessage("channel_config.option.delete_direct_roll_config.description", Locale.ENGLISH))
222236
.descriptionLocales(I18n.allNoneEnglishMessagesDescriptions("channel_config.option.delete_direct_roll_config.description"))
223237
.type(CommandDefinitionOption.Type.SUB_COMMAND_GROUP)
238+
.option(CHANNEL_COMMAND_OPTION)
224239
.build())
225240
.option(CommandDefinitionOption.builder()
226241
.name(ALIAS_OPTION_NAME)
@@ -269,6 +284,10 @@ private String serializeConfig(DirectRollConfig channelConfig) {
269284

270285
@Override
271286
public @NonNull Mono<Void> handleSlashCommandEvent(@NonNull SlashEventAdaptor event, @NonNull Supplier<UUID> uuidSupplier, @NonNull Locale userLocal) {
287+
long channelId = event.getOption(CHANNEL_OPTION_NAME)
288+
.map(CommandInteractionOption::getChannelIdValue)
289+
.orElse(event.getChannelId());
290+
272291
if (event.getOption(SAVE_DIRECT_ROLL_CONFIG_OPTION_NAME).isPresent()) {
273292
CommandInteractionOption saveAction = event.getOption(SAVE_DIRECT_ROLL_CONFIG_OPTION_NAME).get();
274293
boolean alwaysSumResults = saveAction.getBooleanSubOptionWithName(ALWAYS_SUM_RESULTS_OPTION_NAME).orElse(true);
@@ -281,11 +300,12 @@ private String serializeConfig(DirectRollConfig channelConfig) {
281300
final String name = BaseCommandOptions.getNameFromStartCommandOption(saveAction).orElse(null);
282301
DirectRollConfig config = new DirectRollConfig(null, alwaysSumResults, answerFormatType, answerInteractionType, null, new DiceStyleAndColor(diceImageStyle, defaultDiceColor), userOrConfigLocale, name);
283302
BotMetrics.incrementSlashStartMetricCounter(getCommandId() + "_rollConfigSave");
303+
284304
return Mono.defer(() -> {
285-
persistenceManager.deleteChannelConfig(event.getChannelId(), DIRECT_ROLL_CONFIG_TYPE_ID);
305+
persistenceManager.deleteChannelConfig(channelId, DIRECT_ROLL_CONFIG_TYPE_ID);
286306
persistenceManager.saveChannelConfig(new ChannelConfigDTO(uuidSupplier.get(),
287307
event.getGuildId(),
288-
event.getChannelId(),
308+
channelId,
289309
null,
290310
DirectRollCommand.ROLL_COMMAND_ID,
291311
DIRECT_ROLL_CONFIG_TYPE_ID,
@@ -307,16 +327,16 @@ private String serializeConfig(DirectRollConfig channelConfig) {
307327
event.getRequester().toLogString(),
308328
event.getCommandString().replace("`", "")
309329
);
310-
persistenceManager.deleteChannelConfig(event.getChannelId(), DIRECT_ROLL_CONFIG_TYPE_ID);
330+
persistenceManager.deleteChannelConfig(channelId, DIRECT_ROLL_CONFIG_TYPE_ID);
311331
return event.reply(I18n.getMessage("channel_config.deleted.reply", userLocal, event.getCommandString()), false);
312332
});
313333
}
314334
if (event.getOption(ALIAS_OPTION_NAME).isPresent()) {
315335
CommandInteractionOption aliasOption = event.getOption(ALIAS_OPTION_NAME).get();
316336
if (SCOPE_OPTION_CHOICE_USER_CHANNEL_NAME.equals(aliasOption.getStringSubOptionWithName(SCOPE_OPTION_NAME).orElse(null))) {
317-
return handelChannelEvent(event, event.getUserId(), uuidSupplier, userLocal);
337+
return handelChannelEvent(event, event.getUserId(), uuidSupplier, userLocal, channelId);
318338
} else if (SCOPE_OPTION_CHOICE_CHANNEL_NAME.equals(aliasOption.getStringSubOptionWithName(SCOPE_OPTION_NAME).orElse(null))) {
319-
return handelChannelEvent(event, null, uuidSupplier, userLocal);
339+
return handelChannelEvent(event, null, uuidSupplier, userLocal, channelId);
320340
}
321341
log.error("missing scope in slash event: {}", event.getOptions());
322342
}
@@ -339,7 +359,11 @@ public void saveAliasesConfig(@NonNull List<Alias> aliases, long channelId, Long
339359

340360
}
341361

342-
private Mono<Void> handelChannelEvent(@NonNull SlashEventAdaptor event, Long userId, @NonNull Supplier<UUID> uuidSupplier, @NonNull Locale userLocale) {
362+
private Mono<Void> handelChannelEvent(@NonNull SlashEventAdaptor event,
363+
Long userId,
364+
@NonNull Supplier<UUID> uuidSupplier,
365+
@NonNull Locale userLocale,
366+
long channelId) {
343367
if (event.getOption(SAVE_ALIAS_OPTION_NAME).isPresent()) {
344368
String scope = userId == null ? "channelAliasSave" : "user_channelAliasSave";
345369
BotMetrics.incrementSlashStartMetricCounter(getCommandId() + "_" + scope);
@@ -351,7 +375,7 @@ private Mono<Void> handelChannelEvent(@NonNull SlashEventAdaptor event, Long use
351375
.orElse(Alias.Type.Replace);
352376

353377
Alias alias = new Alias(name, value, type);
354-
saveAliasesConfig(List.of(alias), event.getChannelId(), event.getGuildId(), userId, uuidSupplier, null);
378+
saveAliasesConfig(List.of(alias), channelId, event.getGuildId(), userId, uuidSupplier, null);
355379
log.info("{}: save {} alias: {}",
356380
event.getRequester().toLogString(),
357381
userId == null ? "channel" : "user channel",
@@ -377,7 +401,7 @@ private Mono<Void> handelChannelEvent(@NonNull SlashEventAdaptor event, Long use
377401
}
378402
List<Alias> aliases = parseStringToMultiAliasList(aliasesString);
379403
final String name = BaseCommandOptions.getNameFromStartCommandOption(commandInteractionOption).orElse(null);
380-
saveAliasesConfig(aliases, event.getChannelId(), event.getGuildId(), userId, uuidSupplier, name);
404+
saveAliasesConfig(aliases, channelId, event.getGuildId(), userId, uuidSupplier, name);
381405

382406
log.info("{}: save {} aliases: {}",
383407
event.getRequester().toLogString(),
@@ -390,15 +414,15 @@ private Mono<Void> handelChannelEvent(@NonNull SlashEventAdaptor event, Long use
390414
String name = commandInteractionOption.getStringSubOptionWithName(ALIAS_NAME_OPTION_NAME).orElseThrow();
391415

392416
BotMetrics.incrementSlashStartMetricCounter(getCommandId() + "_aliasDelete");
393-
final List<Alias> existingAlias = loadAlias(event.getChannelId(), userId);
417+
final List<Alias> existingAlias = loadAlias(channelId, userId);
394418
if (existingAlias.stream().noneMatch(alias -> alias.getName().equals(name))) {
395419
return event.reply(I18n.getMessage("channel_config.deletedAlias.notFound", userLocale, name), true);
396420
}
397421
List<Alias> newAliasList = existingAlias.stream()
398422
.filter(alias -> !alias.getName().equals(name))
399423
.toList();
400-
deleteAlias(event.getChannelId(), userId);
401-
saveAliasConfig(event.getChannelId(), event.getGuildId(), userId, new AliasConfig(newAliasList, null), uuidSupplier);
424+
deleteAlias(channelId, userId);
425+
saveAliasConfig(channelId, event.getGuildId(), userId, new AliasConfig(newAliasList, null), uuidSupplier);
402426
log.info("{}: delete {} alias: {}",
403427
event.getRequester().toLogString(),
404428
userId == null ? "channel" : "user channel",
@@ -407,14 +431,14 @@ private Mono<Void> handelChannelEvent(@NonNull SlashEventAdaptor event, Long use
407431
return event.reply(I18n.getMessage("channel_config.deletedAlias.reply", userLocale, event.getCommandString()), userId != null);
408432
} else if (event.getOption(DELETE_ALL_ALIAS_OPTION_NAME).isPresent()) {
409433
BotMetrics.incrementSlashStartMetricCounter(getCommandId() + "_aliasDeleteAll");
410-
deleteAlias(event.getChannelId(), userId);
434+
deleteAlias(channelId, userId);
411435
log.info("{}: delete {} all alias",
412436
event.getRequester().toLogString(),
413437
userId == null ? "channel" : "user channel"
414438
);
415439
return event.reply(I18n.getMessage("channel_config.deletedAlias.reply", userLocale, event.getCommandString()), userId != null);
416440
} else if (event.getOption(LIST_ALIAS_OPTION_NAME).isPresent()) {
417-
final List<Alias> existingAlias = loadAlias(event.getChannelId(), userId);
441+
final List<Alias> existingAlias = loadAlias(channelId, userId);
418442

419443
BotMetrics.incrementSlashStartMetricCounter(getCommandId() + "_aliasList");
420444

bot/src/main/resources/botMessages.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ base.option.help=help
115115
base.option.dice_color.name=dice_image_color
116116
base.option.answer_format.name=answer_format
117117
base.option.target_channel.name=target_channel
118+
base.option.channel.name=channel
118119
base.option.name.name=name
119120
base.option.locale.name=language
120121
base.option.dice_image_style.name=dice_image_style
@@ -186,6 +187,7 @@ base.option.answer_format.compact.description=compact
186187
base.option.answer_format.minimal.description=minimal
187188
base.option.answer_interaction.description=If the answer provides interaction options like rerolls
188189
base.option.target_channel.description=Another channel where the answer will be given
190+
base.option.channel.description=Specifies the channel where the command will be applied. Default is the current channel.
189191
base.option.name.description=A name to reuse the command with starter or quickstart command
190192
base.option.locale.description=The language of the bot messages
191193
base.option.dice_image_style.description=If and in what style the dice throw should be shown as image

bot/src/main/resources/botMessages_de.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ base.option.answer_format.only_dice.description=Nur Würfel
111111
base.option.answer_format.compact.description=Kompakt
112112
base.option.answer_format.minimal.description=Minimal
113113
base.option.target_channel.description=Ein anderer Kanal in der die Nachricht gesendet wird
114+
base.option.channel.description=Gibt den Kanal an, auf den der Befehl angewendet wird. Standard ist der aktuelle Kanal.
114115
base.option.locale.description=Die Sprach des Bots
115116
base.option.dice_image_style.description=Ob und in welchen Stil Würfelbilder gezeigt werden
116117
base.option.dice_image_style.none=none

bot/src/main/resources/botMessages_fr.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ base.option.answer_format.only_dice.description=seulement_dice
111111
base.option.answer_format.compact.description=compacte
112112
base.option.answer_format.minimal.description=minimal
113113
base.option.target_channel.description=Autre canal où la réponse sera donnée
114+
base.option.channel.description=Spécifie le canal sur lequel la commande sera appliquée. Par défaut, le canal actuel est utilisé.
114115
base.option.locale.description=La langue des messages du robot
115116
base.option.dice_image_style.description=Si et dans quel style le lancer de dés doit être montré comme une image
116117
base.option.dice_image_style.none=none

bot/src/main/resources/botMessages_pt_BR.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ base.option.answer_format.only_dice.description=apenas_dados
113113
base.option.answer_format.compact.description=compacto
114114
base.option.answer_format.minimal.description=minimo
115115
base.option.target_channel.description=Outro canal onde o resultado será mostrado
116+
base.option.channel.description=Especifica o canal onde o comando será aplicado. O padrão é o canal atual.
116117
base.option.locale.description=O idioma da mensagem do bot
117118
base.option.dice_image_style.description=Se os dados devem ser mostrados como imagens e em qual estilo
118119
base.option.dice_image_style.none=nenhum

0 commit comments

Comments
 (0)