Skip to content

Commit 3954ee8

Browse files
committed
typesafe-messages: Replace example with wiki link
1 parent 87a89d1 commit 3954ee8

File tree

1 file changed

+3
-98
lines changed

1 file changed

+3
-98
lines changed

BotCommands-typesafe-messages/README.md

Lines changed: 3 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -6,107 +6,12 @@ without having to implement anything, alongside a few other benefits:
66
- Checks if template arguments map to function parameters, so all arguments have values
77
- Removes the need for magic strings (for the arguments), improving type safety and making regressions appear immediately
88

9-
## Example
10-
> [!NOTE]
11-
> This example will use Kotlin but any other language should work.
9+
## Usage
1210

13-
### Creating a localization bundle
14-
Let's start by creating a localization bundle at `src/main/resources/bc_localization/MyBotMessages.json`,
15-
for our example it will contain a single localization template
16-
17-
```json
18-
{
19-
"bot.info": "I am in {guild_count, number} {guild_count, choice, 0#guilds|1#guild|1<guilds} and I am up since {uptime_timestamp}."
20-
}
21-
```
22-
23-
- The key is `bot.info`
24-
- The template is `I am in {guild_count, number} {guild_count, choice, 0#guilds|1#guild|1<guilds} and I am up since {uptime_timestamp}.`
25-
- `guild_count` and `uptime_timestamp` are variables
26-
- `number` and `choice` are format types
27-
- `0#guilds|1#guild|1<guilds` is a subformat pattern for [ChoiceFormat](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/text/ChoiceFormat.html)
28-
- See [MessageFormat](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/text/MessageFormat.html) for more details
29-
30-
### Creating our message source
31-
32-
Create an interface which extends `IMessageSource`;
33-
it will contain functions annotated with `@LocalizedContent`,
34-
the annotation's value is the key present in your localization bundle,
35-
and the function needs to return a `String`.
36-
37-
```kt
38-
interface CommandReplies : IMessageSource {
39-
40-
// The function can have any name you want
41-
@LocalizedContent("bot.info")
42-
fun botInfo(
43-
// Parameter names are converted to snake_case for use in the template, here it's 'guild_count'
44-
guildCount: Int,
45-
// You could also pass a Timestamp as it has a proper `toString()`
46-
uptimeTimestamp: String
47-
): String
48-
}
49-
```
50-
51-
> [!NOTE]
52-
> You do not need to implement this interface.
53-
54-
> [!TIP]
55-
> You can inject instances of this interface in any interaction handler such as application commands, components and modals.
56-
57-
> [!TIP]
58-
> You can override the locale using `@PreferLocale` or by passing a `DiscordLocale` or a `Locale` in the first parameter.
59-
60-
### Creating a factory for our source
61-
62-
We then need a way to get instances of our source;
63-
create an interface extending `IMessageSourceFactory<CommandReplies>`
64-
and annotate it with `@MessageSourceFactory("MyBotMessages")`,
65-
the `MyBotMessages` string is the name of the bundle we added in the first step.
66-
67-
```kt
68-
@MessageSourceFactory("MyBotMessages")
69-
interface CommandRepliesFactory : IMessageSourceFactory<CommandReplies>
70-
```
71-
72-
Instances of this interface can be injected like any other service,
73-
and will allow you to create `CommandReplies` instances from an `Interaction`.
74-
75-
> [!NOTE]
76-
> You do not need to implement this interface.
77-
78-
### Usage
79-
80-
```kt
81-
@Command
82-
class SlashInfo {
83-
84-
@JDASlashCommand(
85-
name = "info",
86-
description = "Sends info about the bot",
87-
)
88-
fun onSlashInfo(event: GuildSlashEvent, replies: CommandReplies) {
89-
val response = replies.botInfo(
90-
// Use named parameters to make the arguments clearer!
91-
guildCount = event.jda.guildCache.size(),
92-
uptimeTimestamp = TimeFormat.RELATIVE.format(ManagementFactory.getRuntimeMXBean().startTime),
93-
)
94-
95-
event.reply(response)
96-
.setEphemeral(true)
97-
.queue()
98-
}
99-
}
100-
```
101-
102-
> [!TIP]
103-
> Injecting the `CommandReplies` instance in the slash command function
104-
> is the same as injecting `CommandRepliesFactory` in your class then using it in your command to create instances of `CommandReplies`.
105-
106-
Try out `/info`!
11+
See ["Responding with type-safe messages"](https://bc.freya02.dev/3.X/using-botcommands/localization/typesafe-messages/) in the wiki.
10712

10813
## Installation
109-
There are different dependencies based on what dependency injection you use:
14+
There are different dependencies based on which dependency injection you use:
11015

11116
![](https://img.shields.io/maven-central/v/io.github.freya022/BotCommands-typesafe-messages-core?versionPrefix=3)
11217

0 commit comments

Comments
 (0)