Skip to content

Commit

Permalink
Channel config scope (#432)
Browse files Browse the repository at this point in the history
* Channel Config Scope
  • Loading branch information
twonirwana authored Feb 4, 2024
1 parent 023c839 commit 40b4f36
Show file tree
Hide file tree
Showing 15 changed files with 209 additions and 287 deletions.
18 changes: 11 additions & 7 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -455,20 +455,24 @@ It is possible to configure alias in a channel.
Alias can be used with direct roll and custom dice.
If a custom dice button is created that should use an alias, the alias musst exist before the button is created.

For example `/channel_config user_channel_alias save name: att value: (2d20k1)+10+1d4` creates an attack alias for the user.
For example `/channel_config alias save name: att value: (2d20k1)+10+1d4 scope: current_user_in_this_channel` creates an attack alias for the user.
Each time the user uses the slash command `/r expression: att`, the `att` will automatically replaced with `(2d20k1)+10+1d4`

An alias has a name and a value and will replace each occurrence in the dice expression of its name with its value.
There are two types of alias, channel alias and user channel alias.
A channel alias will be applied to each roll in a channel, for every user.
The user channel alias will only apply to for the user who created the alias in the channel.
The user channel alias will be applied first.
Three are four commands for each type.

There are two scopes of alias:

* `all_users_in_this_channel`: It will be applied to each roll in a channel, for every user.
* `current_user_in_this_channel`: It will only apply to for the user who created the alias in the channel.

The user specific alias will be applied first.

There are four commands for each scope.

* `save` creates or overwrites an alias
* `multi_save` creates or overwrites multiple alias.
Alias are seperated by `;` and the name and the value are seperated by `:`.
For example: `/channel_config channel_alias multi_save aliases: att:2d20;dmg:2d6+4=` saves two alias:
For example: `/channel_config alias multi_save aliases: att:2d20;dmg:2d6+4= scope: current_user_in_this_channel` saves two alias:
** `att` with `2d20`
** `dmg` with `2d6+4=`
* `delete` removes an alias by its name
Expand Down
1 change: 1 addition & 0 deletions bot/src/main/java/de/janno/discord/bot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static void main(final String[] args) throws Exception {
}
}
BotMetrics.init();
Config.onChange(event -> event.modifiedKeys().forEach(k -> log.info("config change: {} -> {}", k, event.configuration().getOptional(k).orElse(""))));

final String url = Config.get("db.url", "jdbc:h2:file:./persistence/dice_config;AUTO_SERVER=TRUE;DB_CLOSE_DELAY=5");
final String user = Config.getNullable("db.user");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import de.janno.discord.connector.api.SlashEventAdaptor;
import de.janno.discord.connector.api.slash.CommandDefinition;
import de.janno.discord.connector.api.slash.CommandDefinitionOption;
import de.janno.discord.connector.api.slash.CommandDefinitionOptionChoice;
import de.janno.discord.connector.api.slash.CommandInteractionOption;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;
import reactor.core.publisher.Mono;

Expand All @@ -38,15 +38,37 @@ public class ChannelConfigCommand implements SlashCommand {
private static final String COMMAND_ID = "channel_config";
private static final String SAVE_DIRECT_ROLL_CONFIG_OPTION_NAME = "save_direct_roll_config";
private static final String DELETE_DIRECT_ROLL_CONFIG_OPTION_NAME = "delete_direct_roll_config";
private static final String CHANNEL_ALIAS_OPTION_NAME = "channel_alias";
private static final String USER_CHANNEL_ALIAS_OPTION_NAME = "user_channel_alias";
private static final String ALIAS_OPTION_NAME = "alias";
private static final String SCOPE_OPTION_NAME = "scope";
private static final String SCOPE_OPTION_CHOICE_USER_CHANNEL_NAME = "current_user_in_this_channel";
private static final String SCOPE_OPTION_CHOICE_CHANNEL_NAME = "all_users_in_this_channel";
private static final String SAVE_ALIAS_OPTION_NAME = "save";
private static final String SAVE_MULTI_ALIAS_OPTION_NAME = "multi_save";
private static final String ALIAS_NAME_OPTION_NAME = "name";
private static final String ALIASES_OPTION_NAME = "aliases";
private static final String ALIAS_VALUE_OPTION_NAME = "value";
private static final String LIST_ALIAS_OPTION_NAME = "list";
private static final String DELETE_ALIAS_OPTION_NAME = "delete";

private static final CommandDefinitionOption SCOPE_OPTION = CommandDefinitionOption.builder()
.type(CommandDefinitionOption.Type.STRING)
.name(SCOPE_OPTION_NAME)
.nameLocales(I18n.allNoneEnglishMessagesNames("channel_config.option.scope.name"))
.description(I18n.getMessage("channel_config.option.scope.description", Locale.ENGLISH))
.descriptionLocales(I18n.allNoneEnglishMessagesDescriptions("channel_config.option.scope.description"))
.required(true)
.choice(CommandDefinitionOptionChoice.builder()
//no locale to have copy and pasteable commands
.name(SCOPE_OPTION_CHOICE_CHANNEL_NAME)
.value(SCOPE_OPTION_CHOICE_CHANNEL_NAME)
.build())
.choice(CommandDefinitionOptionChoice.builder()
//no locale to have copy and pasteable commands
.name(SCOPE_OPTION_CHOICE_USER_CHANNEL_NAME)
.value(SCOPE_OPTION_CHOICE_USER_CHANNEL_NAME)
.build())
.build();

private static final CommandDefinitionOption SAVE_ALIAS_OPTION = CommandDefinitionOption.builder()
.type(CommandDefinitionOption.Type.SUB_COMMAND)
.name(SAVE_ALIAS_OPTION_NAME)
Expand All @@ -69,6 +91,7 @@ public class ChannelConfigCommand implements SlashCommand {
.descriptionLocales(I18n.allNoneEnglishMessagesDescriptions("channel_config.option.value.description"))
.required(true)
.build())
.option(SCOPE_OPTION)
.build();
private static final CommandDefinitionOption MULTI_SAVE_ALIAS_OPTION = CommandDefinitionOption.builder()
.type(CommandDefinitionOption.Type.SUB_COMMAND)
Expand All @@ -84,6 +107,7 @@ public class ChannelConfigCommand implements SlashCommand {
.descriptionLocales(I18n.allNoneEnglishMessagesDescriptions("channel_config.option.aliases.description"))
.required(true)
.build())
.option(SCOPE_OPTION)
.build();
private static final CommandDefinitionOption DELETE_ALIAS_OPTION = CommandDefinitionOption.builder()
.type(CommandDefinitionOption.Type.SUB_COMMAND)
Expand All @@ -99,14 +123,17 @@ public class ChannelConfigCommand implements SlashCommand {
.descriptionLocales(I18n.allNoneEnglishMessagesDescriptions("channel_config.option.delete.aliasName.description"))
.required(true)
.build())
.option(SCOPE_OPTION)
.build();
private static final CommandDefinitionOption LIST_ALIAS_OPTION = CommandDefinitionOption.builder()
.type(CommandDefinitionOption.Type.SUB_COMMAND)
.name(LIST_ALIAS_OPTION_NAME)
.nameLocales(I18n.allNoneEnglishMessagesNames("channel_config.option.list.name"))
.description(I18n.getMessage("channel_config.option.list.description", Locale.ENGLISH))
.descriptionLocales(I18n.allNoneEnglishMessagesDescriptions("channel_config.option.list.description"))
.option(SCOPE_OPTION)
.build();
private final static String NAME_VALUE_DELIMITER = ":";
private final PersistenceManager persistenceManager;

public ChannelConfigCommand(PersistenceManager persistenceManager) {
Expand Down Expand Up @@ -151,21 +178,10 @@ public ChannelConfigCommand(PersistenceManager persistenceManager) {
.type(CommandDefinitionOption.Type.SUB_COMMAND_GROUP)
.build())
.option(CommandDefinitionOption.builder()
.name(CHANNEL_ALIAS_OPTION_NAME)
.nameLocales(I18n.allNoneEnglishMessagesNames("channel_config.option.channel_alias.name"))
.description(I18n.getMessage("channel_config.option.channel_alias.description", Locale.ENGLISH))
.descriptionLocales(I18n.allNoneEnglishMessagesDescriptions("channel_config.option.channel_alias.description"))
.type(CommandDefinitionOption.Type.SUB_COMMAND_GROUP)
.option(SAVE_ALIAS_OPTION)
.option(DELETE_ALIAS_OPTION)
.option(LIST_ALIAS_OPTION)
.option(MULTI_SAVE_ALIAS_OPTION)
.build())
.option(CommandDefinitionOption.builder()
.name(USER_CHANNEL_ALIAS_OPTION_NAME)
.nameLocales(I18n.allNoneEnglishMessagesNames("channel_config.option.user_channel_alias.name"))
.description(I18n.getMessage("channel_config.option.user_channel_alias.description", Locale.ENGLISH))
.descriptionLocales(I18n.allNoneEnglishMessagesDescriptions("channel_config.option.user_channel_alias.description"))
.name(ALIAS_OPTION_NAME)
.nameLocales(I18n.allNoneEnglishMessagesNames("channel_config.option.alias.name"))
.description(I18n.getMessage("channel_config.option.alias.description", Locale.ENGLISH))
.descriptionLocales(I18n.allNoneEnglishMessagesDescriptions("channel_config.option.alias.description"))
.type(CommandDefinitionOption.Type.SUB_COMMAND_GROUP)
.option(SAVE_ALIAS_OPTION)
.option(DELETE_ALIAS_OPTION)
Expand Down Expand Up @@ -225,11 +241,14 @@ private String serializeConfig(DirectRollConfig channelConfig) {
return event.reply(I18n.getMessage("channel_config.deleted.reply", userLocal, event.getCommandString()), false);
});
}
if (event.getOption(CHANNEL_ALIAS_OPTION_NAME).isPresent()) {
return handelChannelEvent(event, null, uuidSupplier, userLocal);
}
if (event.getOption(USER_CHANNEL_ALIAS_OPTION_NAME).isPresent()) {
return handelChannelEvent(event, event.getUserId(), uuidSupplier, userLocal);
if (event.getOption(ALIAS_OPTION_NAME).isPresent()) {
CommandInteractionOption aliasOption = event.getOption(ALIAS_OPTION_NAME).get();
if (SCOPE_OPTION_CHOICE_USER_CHANNEL_NAME.equals(aliasOption.getStringSubOptionWithName(SCOPE_OPTION_NAME).orElse(null))) {
return handelChannelEvent(event, event.getUserId(), uuidSupplier, userLocal);
} else if (SCOPE_OPTION_CHOICE_CHANNEL_NAME.equals(aliasOption.getStringSubOptionWithName(SCOPE_OPTION_NAME).orElse(null))) {
return handelChannelEvent(event, null, uuidSupplier, userLocal);
}
log.error("missing scope in slash event: {}", event.getOptions());
}
log.error("unknown option for slash event: {} ", event.getOptions());
return event.reply(I18n.getMessage("channel_config.unknown.reply", userLocal), false);
Expand Down Expand Up @@ -277,14 +296,14 @@ private Mono<Void> handelChannelEvent(@NonNull SlashEventAdaptor event, @Nullabl
return event.reply(I18n.getMessage("channel_config.noNameValue.reply", userLocale, event.getCommandString()), true);
}
List<String> missingNameValue = nameValuePair.stream()
.filter(s -> StringUtils.countMatches(s, ":") != 1)
.filter(s -> s.split(NAME_VALUE_DELIMITER).length != 2)
.toList();
if (!missingNameValue.isEmpty()) {
return event.reply(I18n.getMessage("channel_config.missingSeparator.reply", userLocale, event.getCommandString(), missingNameValue), true);
return event.reply(I18n.getMessage("channel_config.missingSeparator.reply", userLocale, event.getCommandString(), String.join(";", missingNameValue)), true);
}
List<Alias> aliases = nameValuePair.stream()
.map(s -> {
String[] split = s.split(":");
String[] split = s.split(NAME_VALUE_DELIMITER);
return new Alias(split[0], split[1]);
})
.toList();
Expand Down
10 changes: 5 additions & 5 deletions bot/src/main/resources/botMessages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ channel_config.deleted.reply=`{0}`\nDeleted direct roll channel config
channel_config.unknown.reply=Unknown slash event options
channel_config.savedAlias.reply=`{0}`\nSaved new alias
channel_config.noNameValue.reply=`{0}`\nNo name/value pair
channel_config.missingSeparator.reply=`{0}`\nMissing name value separator `:` in: {1}
channel_config.missingSeparator.reply=`{0}`\nWrong format in `{1}`, it must be `name:value`
channel_config.deletedAlias.reply=`{0}`\nDeleted alias
channel_config.listAlias.reply=`{0}`\nExisting alias:\n{1}
quickstart.unknown.reply=Unknown rpg: `{0}`
Expand Down Expand Up @@ -116,8 +116,8 @@ channel_config.option.delete.name=delete
channel_config.option.save_direct_roll_config.name=save_direct_roll_config
channel_config.option.always_sum_result.name=always_sum_result
channel_config.option.delete_direct_roll_config.name=delete_direct_roll_config
channel_config.option.channel_alias.name=channel_alias
channel_config.option.user_channel_alias.name=channel_alias
channel_config.option.alias.name=alias
channel_config.option.scope.name=scope
quickstart.option.system=system
base.option.answer_format.full.name=full
base.option.answer_format.without_expression.name=without_expression
Expand Down Expand Up @@ -188,8 +188,8 @@ channel_config.option.delete.aliasName.description=The name of the alias to dele
channel_config.option.save_direct_roll_config.description=add or update the channel config
channel_config.option.always_sum_result.description=Always sum the results of the dice expressions
channel_config.option.delete_direct_roll_config.description=remove the current channel config
channel_config.option.channel_alias.description=add, list or remove channel alias
channel_config.option.user_channel_alias.description=add, list or remove user channel alias
channel_config.option.alias.description=add, list or remove alias
channel_config.option.scope.description=scope of the alias, if applies only to one user or more
quickstart.option.description=Start typing to filter and see more options
#autocomplete net.dv8tion.jda.api.interactions.commands.Command.Choice.MAX_NAME_LENGTH
#max 100 characters
Expand Down
2 changes: 1 addition & 1 deletion bot/src/main/resources/botMessages_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ channel_config.deleted.reply=`{0}`\nDirekter Wurf Kanalkonfiguration wurde gelö
channel_config.unknown.reply=Unbekannte Slash Option
channel_config.savedAlias.reply=`{0}`\nNeuer Alias gespeichert
channel_config.noNameValue.reply=`{0}`\nKein Namen/Wert Paar
channel_config.missingSeparator.reply=`{0}`\nFehlender Namen/Wert Separator `:` in: {1}
channel_config.missingSeparator.reply=`{0}`\nFalsches Format in`{1}`, es muss das Format `Name:Wert` haben
channel_config.deletedAlias.reply=`{0}`\nAlias gelöscht
channel_config.listAlias.reply=`{0}`\nExistierende Alias:\n{1}
quickstart.unknown.reply=Unbekanntes RPG: `{0}`
Expand Down
2 changes: 1 addition & 1 deletion bot/src/main/resources/botMessages_fr.properties
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ channel_config.deleted.reply=`{0}`\n Suppression de la configuration du canal de
channel_config.unknown.reply=Options inconnues de l'événement slash
channel_config.savedAlias.reply=`{0}`\nIl a enregistré un nouvel alias
channel_config.noNameValue.reply=`{0}`\n Pas de couple nom/valeur
channel_config.missingSeparator.reply=`{0}`\n Séparateur de nom et de valeur manquant `:` dans: {1}
channel_config.missingSeparator.reply=`{0}`\nMauvais format dans `{1}`, il doit s'agir de `nom:valeur`.
channel_config.deletedAlias.reply=`{0}`\n alias supprimé
channel_config.listAlias.reply=`{0}`\n supprimé existant:\n{1}
quickstart.unknown.reply=Rpg inconnu: `{0}`
Expand Down
2 changes: 1 addition & 1 deletion bot/src/main/resources/botMessages_pt_BR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ channel_config.save.reply=`{0}`\nConfiguração para canal de rolagem direta sal
channel_config.deleted.reply=`{0}`\nConfiguração para canal de rolagem direta deletada
channel_config.unknown.reply=Opções de evento de barra desconhecida
channel_config.savedAlias.reply=`{0}`\nNovo apelido salvo
channel_config.noNameValue.reply=`{0}`\nSem par de nome/valor
channel_config.noNameValue.reply=`{0}`\nFormato incorreto em `{1}`, deve ser `nome:valor`
channel_config.missingSeparator.reply=`{0}`\nEstá faltando o separador do valor de nome `:` em: {1}
channel_config.deletedAlias.reply=`{0}`\nApelido deletado
channel_config.listAlias.reply=`{0}`\nApelido já existe:\n{1}
Expand Down
Loading

0 comments on commit 40b4f36

Please sign in to comment.