Responding using events¶
The framework's events extend a few interfaces:
- Interactions which can be replied to:
LocalizableReplyCallback - Interactions which can edit:
LocalizableEditCallback - Interaction hooks also have
LocalizableInteractionHook - Text command events have
LocalizableTextCommand
These interfaces allow you to:
- Reply/edit using the user (
replyUser), guild locale (replyGuild) or best locale (replyLocalized) - Get messages using the user or guild locale (
get(User/Guild/Localized)Message) - Get the bundle of default messages (
getBotCommandsMessages) - Create localization contexts (
getLocalizationContext)
You can also configure the event to use a particular bundle, or change the path prefix.
Example¶
Using the following localization bundle:
Root bundle @ src/main/resources/bc_localization/EventReplies.json
{
"commands" : {
"replies": {
"event_localization": "I am directly translated using an event ! Sent {send_timestamp}"
}
}
}
The root bundle must exist as a fallback.
French bundle @ src/main/resources/bc_localization/EventReplies_fr.json
{
"commands" : {
"replies": {
"event_localization": "Je suis directement traduis avec l'événement ! Envoyé {send_timestamp}"
}
}
}
Which we configure to use:
BotCommands.create {
// ...
localization {
// This is only for localization methods directly present on the event
addResponseBundle("EventReplies")
}
}
Then, reply using the user's locale (as provided by UserLocaleProvider), passing the send_timestamp argument.
@Command
class SlashEventLocalization {
@JDASlashCommand(name = "event_localization", description = "Send localized responses using the event!")
fun onSlashEventLocalization(event: GuildSlashEvent) {
event.replyUser("commands.replies.event_localization", "send_timestamp" to TimeFormat.RELATIVE.now())
.setEphemeral(true)
.queue()
}
}
@Command
public class SlashEventLocalization {
@JDASlashCommand(name = "event_localization", description = "Send localized responses using the event!")
public void onSlashEventLocalization(GuildSlashEvent event) {
// Use a static import for io.github.freya022.botcommands.api.localization.Localization.Entry.entry
event.replyUser("commands.replies.event_localization", entry("send_timestamp", TimeFormat.RELATIVE.now()))
.setEphemeral(true)
.queue();
}
}