|
3 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this
|
4 | 4 | * file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
5 | 5 | */
|
| 6 | +package dev.qixils.quasicord.commands |
6 | 7 |
|
7 |
| -package dev.qixils.quasicord.commands; |
8 |
| - |
9 |
| -import com.mongodb.client.model.ReplaceOptions; |
10 |
| -import dev.qixils.quasicord.Key; |
11 |
| -import dev.qixils.quasicord.Quasicord; |
12 |
| -import dev.qixils.quasicord.autocomplete.impl.LocaleAutoCompleter; |
13 |
| -import dev.qixils.quasicord.autocomplete.impl.TimeZoneAutoCompleter; |
14 |
| -import dev.qixils.quasicord.db.collection.LocaleConfig; |
15 |
| -import dev.qixils.quasicord.db.collection.TimeZoneConfig; |
16 |
| -import dev.qixils.quasicord.decorators.Namespace; |
17 |
| -import dev.qixils.quasicord.decorators.option.AutoCompleteWith; |
18 |
| -import dev.qixils.quasicord.decorators.option.Contextual; |
19 |
| -import dev.qixils.quasicord.decorators.option.Option; |
20 |
| -import dev.qixils.quasicord.decorators.slash.SlashCommand; |
21 |
| -import dev.qixils.quasicord.decorators.slash.SlashSubCommand; |
22 |
| -import dev.qixils.quasicord.text.Text; |
23 |
| -import dev.qixils.quasicord.utils.QuasiMessage; |
24 |
| -import net.dv8tion.jda.api.entities.User; |
25 |
| -import net.dv8tion.jda.api.entities.channel.Channel; |
26 |
| -import net.dv8tion.jda.api.entities.channel.ChannelType; |
27 |
| -import net.dv8tion.jda.api.interactions.commands.OptionType; |
28 |
| -import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction; |
29 |
| -import org.reactivestreams.Publisher; |
30 |
| -import reactor.core.publisher.Mono; |
31 |
| - |
32 |
| -import java.time.ZoneId; |
33 |
| -import java.util.Locale; |
34 |
| - |
35 |
| -import static com.mongodb.client.model.Filters.eq; |
| 8 | +import com.mongodb.client.model.Filters |
| 9 | +import com.mongodb.client.model.ReplaceOptions |
| 10 | +import dev.minn.jda.ktx.coroutines.await |
| 11 | +import dev.minn.jda.ktx.messages.reply_ |
| 12 | +import dev.qixils.quasicord.Key.Companion.library |
| 13 | +import dev.qixils.quasicord.Quasicord |
| 14 | +import dev.qixils.quasicord.autocomplete.impl.LocaleAutoCompleter |
| 15 | +import dev.qixils.quasicord.autocomplete.impl.TimeZoneAutoCompleter |
| 16 | +import dev.qixils.quasicord.db.collection.LocaleConfig |
| 17 | +import dev.qixils.quasicord.db.collection.TimeZoneConfig |
| 18 | +import dev.qixils.quasicord.decorators.Namespace |
| 19 | +import dev.qixils.quasicord.decorators.option.AutoCompleteWith |
| 20 | +import dev.qixils.quasicord.decorators.option.Contextual |
| 21 | +import dev.qixils.quasicord.decorators.option.Option |
| 22 | +import dev.qixils.quasicord.decorators.slash.SlashCommand |
| 23 | +import dev.qixils.quasicord.decorators.slash.SlashSubCommand |
| 24 | +import dev.qixils.quasicord.locale.Context |
| 25 | +import dev.qixils.quasicord.text.Text |
| 26 | +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent |
| 27 | +import net.dv8tion.jda.api.interactions.commands.OptionType |
| 28 | +import java.time.ZoneId |
| 29 | +import java.time.format.TextStyle |
| 30 | +import java.util.* |
36 | 31 |
|
37 | 32 | @Namespace("quasicord")
|
38 | 33 | @SlashCommand("user-config")
|
39 |
| -public class UserConfigCommand extends ConfigCommand { |
40 |
| - |
41 |
| - public UserConfigCommand(Quasicord library) { |
42 |
| - super(library); |
43 |
| - } |
| 34 | +class UserConfigCommand(library: Quasicord) : ConfigCommand(library) { |
| 35 | + @SlashSubCommand("language") |
| 36 | + suspend fun setLocaleCommand( |
| 37 | + @Option( |
| 38 | + value = "language", |
| 39 | + type = OptionType.STRING |
| 40 | + ) @AutoCompleteWith(LocaleAutoCompleter::class) locale: Locale?, |
| 41 | + @Contextual event: SlashCommandInteractionEvent, |
| 42 | + @Contextual ctx: Context, |
| 43 | + ) { |
| 44 | + setLocale(locale, LocaleConfig.EntryType.USER, event.user) |
| 45 | + val text = if (locale == null) ctx.text(library("user-config.language.output.removed")) |
| 46 | + else ctx.text(library("user-config.language.output.updated"), locale.getDisplayName(locale)) |
| 47 | + event.reply_(text, ephemeral = event.isFromGuild).await() |
| 48 | + } |
44 | 49 |
|
45 |
| - @SlashSubCommand("language") |
46 |
| - public Mono<QuasiMessage> setLocaleCommand( |
47 |
| - @Option(value = "language", type = OptionType.STRING) |
48 |
| - @AutoCompleteWith(LocaleAutoCompleter.class) |
49 |
| - Locale locale, |
50 |
| - @Contextual |
51 |
| - User user, |
52 |
| - @Contextual |
53 |
| - Channel channel |
54 |
| - ) { |
55 |
| - return setLocale(locale, LocaleConfig.EntryType.USER, user).map($ -> locale == null |
56 |
| - ? Text.single(Key.library("user-config.language.output.removed")) |
57 |
| - : Text.single(Key.library("user-config.language.output.updated"), locale.getDisplayName(locale))) |
58 |
| - .map(text -> new QuasiMessage(text, request -> { |
59 |
| - if (request instanceof ReplyCallbackAction action && channel.getType() != ChannelType.PRIVATE) { |
60 |
| - //noinspection ResultOfMethodCallIgnored |
61 |
| - action.setEphemeral(true); |
62 |
| - } |
63 |
| - })); |
64 |
| - } |
| 50 | + @SlashSubCommand("timezone") |
| 51 | + suspend fun setTimeZoneCommand( |
| 52 | + @Option( |
| 53 | + value = "timezone", |
| 54 | + type = OptionType.STRING |
| 55 | + ) @AutoCompleteWith(TimeZoneAutoCompleter::class) tz: ZoneId?, |
| 56 | + @Contextual event: SlashCommandInteractionEvent, |
| 57 | + @Contextual ctx: Context, |
| 58 | + ) { |
| 59 | + val collection = library.databaseManager.collection<TimeZoneConfig>() |
| 60 | + val filter = Filters.eq("snowflake", event.user.idLong) |
| 61 | + if (tz == null) { |
| 62 | + collection.deleteOne(filter) |
| 63 | + } else { |
| 64 | + collection.replaceOne( |
| 65 | + filter, |
| 66 | + TimeZoneConfig(event.user.idLong, tz), |
| 67 | + ReplaceOptions().upsert(true) |
| 68 | + ) |
| 69 | + } |
65 | 70 |
|
66 |
| - @SlashSubCommand("timezone") |
67 |
| - public Mono<QuasiMessage> setTimeZoneCommand( |
68 |
| - @Option(value = "timezone", type = OptionType.STRING) |
69 |
| - @AutoCompleteWith(TimeZoneAutoCompleter.class) |
70 |
| - ZoneId tz, |
71 |
| - @Contextual |
72 |
| - User user, |
73 |
| - @Contextual |
74 |
| - Channel channel |
75 |
| - ) { |
76 |
| - var collection = library.getDatabaseManager().collection(TimeZoneConfig.class); |
77 |
| - var filter = eq("_id", user.getIdLong()); |
78 |
| - Publisher<?> result; |
79 |
| - if (tz == null) { |
80 |
| - result = collection.deleteOne(filter); |
81 |
| - } else { |
82 |
| - result = collection.replaceOne( |
83 |
| - filter, |
84 |
| - new TimeZoneConfig(user.getIdLong(), tz), |
85 |
| - new ReplaceOptions().upsert(true) |
86 |
| - ); |
87 |
| - } |
88 |
| - return Mono.empty(); // TODO |
89 |
| -// return Mono.from(result).map($ -> tz == null |
90 |
| -// ? Text.single(Key.library("user-config.timezone.output.removed")) |
91 |
| -// : Text.single(Key.library("user-config.timezone.output.updated"), (Text) locale -> tz.getDisplayName(TextStyle.FULL_STANDALONE, locale))) |
92 |
| -// .map(text -> new QuasiMessage(text, request -> { |
93 |
| -// if (request instanceof ReplyCallbackAction action && channel.getType() != ChannelType.PRIVATE) { |
94 |
| -// //noinspection ResultOfMethodCallIgnored |
95 |
| -// action.setEphemeral(true); |
96 |
| -// } |
97 |
| -// })); |
98 |
| - } |
| 71 | + val text = if (tz == null) ctx.text(library("user-config.timezone.output.removed")) |
| 72 | + else ctx.text(library("user-config.timezone.output.updated"), Text { locale -> tz.getDisplayName(TextStyle.FULL_STANDALONE, locale) }) |
| 73 | + event.reply_(text, ephemeral = event.isFromGuild).await() |
| 74 | + } |
99 | 75 | }
|
0 commit comments