diff options
Diffstat (limited to 'commands/config.go')
| -rw-r--r-- | commands/config.go | 216 |
1 files changed, 130 insertions, 86 deletions
diff --git a/commands/config.go b/commands/config.go index c4644bc..336e66a 100644 --- a/commands/config.go +++ b/commands/config.go @@ -2,24 +2,32 @@ package commands import ( "fmt" + "slices" "strings" "git.anhgelus.world/anhgelus/les-copaings-bot/config" "git.anhgelus.world/anhgelus/les-copaings-bot/exp" "github.com/anhgelus/gokord/cmd" - "github.com/anhgelus/gokord/component" - "github.com/anhgelus/gokord/logger" - discordgo "github.com/nyttikord/gokord" + "github.com/nyttikord/gokord/bot" + "github.com/nyttikord/gokord/channel" + "github.com/nyttikord/gokord/component" + "github.com/nyttikord/gokord/discord/types" + "github.com/nyttikord/gokord/event" + "github.com/nyttikord/gokord/interaction" ) const ( ConfigModify = "config_modify" + OpenConfig = "config" ) -func Config(_ *discordgo.Session, i *discordgo.InteractionCreate, _ cmd.OptionMap, resp *cmd.ResponseBuilder) { +func ConfigResponse(i *event.InteractionCreate) *interaction.Response { cfg := config.GetGuildConfig(i.GuildID) roles := "" l := len(cfg.XpRoles) - 1 + slices.SortFunc(cfg.XpRoles, func(xp1, xp2 config.XpRole) int { + return int(xp2.XP) - int(xp1.XP) + }) for i, r := range cfg.XpRoles { if i == l { roles += fmt.Sprintf("> Niveau %d - <@&%s>", exp.Level(r.XP), r.RoleID) @@ -28,94 +36,130 @@ func Config(_ *discordgo.Session, i *discordgo.InteractionCreate, _ cmd.OptionMa } } if len(roles) == 0 { - roles = "Aucun rôle configuré :(" + roles = "Aucun rôle configuré" } disChans := strings.Split(cfg.DisabledChannels, ";") - l = len(disChans) - 1 - chans := "" - for i, c := range disChans { - if i == l-1 { - chans += fmt.Sprintf("> <#%s>", c) - } else if i != l { - chans += fmt.Sprintf("> <#%s>\n", c) + var disChansDefault []component.SelectMenuDefaultValue + for _, c := range disChans { + if c != "" { + disChansDefault = append(disChansDefault, component.SelectMenuDefaultValue{ + ID: c, + Type: types.SelectMenuDefaultValueChannel, + }) } } - if len(chans) == 0 { - chans = "Aucun salon désactivé :)" + var defaultChan []component.SelectMenuDefaultValue + if len(cfg.FallbackChannel) > 0 { + defaultChan = append(defaultChan, component.SelectMenuDefaultValue{ + ID: cfg.FallbackChannel, + Type: types.SelectMenuDefaultValueChannel, + }) } - var defaultChan string - if len(cfg.FallbackChannel) == 0 { - defaultChan = "Pas de valeur" - } else { - defaultChan = fmt.Sprintf("<#%s>", cfg.FallbackChannel) + zero := 0 + content := []component.Component{ + &component.Container{ + Components: []component.Message{ + &component.TextDisplay{Content: "## Configuration"}, + &component.Separator{}, + &component.TextDisplay{Content: "**Salons par défaut**\n-# Les niveaux obtenue grâce à un appel sont affichés ici"}, + &component.ActionsRow{ + Components: []component.Message{ + &component.SelectMenu{ + MenuType: types.SelectMenuChannel, + CustomID: config.ModifyFallbackChannel, + Placeholder: "Pas de salon par défaut", + MinValues: &zero, + MaxValues: 1, + DefaultValues: defaultChan, + }, + }, + }, + &component.TextDisplay{Content: "**Salons désactivé**\n-# Les messages ne donneront pas d'expérience dans ces salons"}, + &component.ActionsRow{ + Components: []component.Message{ + &component.SelectMenu{ + MenuType: types.SelectMenuChannel, + CustomID: config.ModifyDisChannel, + Placeholder: "Pas de salons désactivé", + MinValues: &zero, + MaxValues: 25, + DefaultValues: disChansDefault, + }, + }, + }, + &component.Section{ + Components: []component.Message{ + &component.TextDisplay{Content: "**Rôles de niveau**\n" + roles}, + }, + Accessory: &component.Button{ + Label: "Modifier", + Style: component.ButtonStyleSecondary, + CustomID: config.ModifyXpRole, + }, + }, + &component.Section{ + Components: []component.Message{ + &component.TextDisplay{ + Content: fmt.Sprintf("**Jours avant la réduction**\n-# Seule l'expérience gagnée les x derniers jours est comptabilisée dans le niveau par défaut\n%d jours", cfg.DaysXPRemains), + }, + }, + Accessory: &component.Button{ + Label: "Modifier", + Style: component.ButtonStyleSecondary, + CustomID: config.ModifyTimeReduce, + }, + }, + }, + }, } - //comp := component.New(). - // Add(component.NewTextDisplay("# Config")). - // Add(component.NewTextDisplay("**Salon par défaut**\n" + defaultChan)). - // Add(component.NewSeparator()). - // Add(component.NewTextDisplay("**Rôles liés aux niveaux**\n" + roles)). - // Add(component.NewSeparator()). - // Add(component.NewTextDisplay("**Salons désactivés**\n" + chans)). - // Add(component.NewSeparator()). - // Add(component.NewTextDisplay(fmt.Sprintf("**%s**\n%d", "Jours avant la réduction", cfg.DaysXPRemains))). - // Add(component.NewActionRow().Add(component.NewStringSelect(ConfigModify). - // SetPlaceholder("Modifier..."). - // AddOption( - // component.NewSelectOption("Rôles liés à l'XP", config.ModifyXpRole). - // SetDescription("Gère les rôles liés à l'XP"). - // SetEmoji(&discordgo.ComponentEmoji{Name: "🏅"}), - // ). - // AddOption( - // component.NewSelectOption("Salons désactivés", config.ModifyDisChannel). - // SetDescription("Gère les salons désactivés"). - // SetEmoji(&discordgo.ComponentEmoji{Name: "❌"}), - // ). - // AddOption( - // // I don't have a better idea for this... - // component.NewSelectOption("Salons par défaut", config.ModifyFallbackChannel). - // SetDescription("Spécifie le salon par défaut"). - // SetEmoji(&discordgo.ComponentEmoji{Name: "💾"}), - // ). - // AddOption( - // component.NewSelectOption("Temps avec la réduction", config.ModifyTimeReduce). - // SetDescription("Gère le temps avant la réduction d'XP"). - // SetEmoji(&discordgo.ComponentEmoji{Name: "⌛"}), - // ), - // )) - comp := component.New(). - Add(component.NewActionRow().Add(component.NewStringSelect(ConfigModify). - SetPlaceholder("Modifier..."). - AddOption( - component.NewSelectOption("Rôles liés à l'XP", config.ModifyXpRole). - SetDescription("Gère les rôles liés à l'XP"). - SetEmoji(&discordgo.ComponentEmoji{Name: "🏅"}), - ). - AddOption( - component.NewSelectOption("Salons désactivés", config.ModifyDisChannel). - SetDescription("Gère les salons désactivés"). - SetEmoji(&discordgo.ComponentEmoji{Name: "❌"}), - ). - AddOption( - // I don't have a better idea for this... - component.NewSelectOption("Salons par défaut", config.ModifyFallbackChannel). - SetDescription("Spécifie le salon par défaut"). - SetEmoji(&discordgo.ComponentEmoji{Name: "💾"}), - ). - AddOption( - component.NewSelectOption("Temps avec la réduction", config.ModifyTimeReduce). - SetDescription("Gère le temps avant la réduction d'XP"). - SetEmoji(&discordgo.ComponentEmoji{Name: "⌛"}), - ), - )) - msg := fmt.Sprintf( - "# Config\n**Salon par défaut**\n%s\n\n**Rôles liés aux niveaux**\n%s\n\n**Salons désactivés**\n%s\n\n**Jours avant la réduction**\n%d", - defaultChan, - roles, - chans, - cfg.DaysXPRemains, - ) - err := resp.SetComponents(comp).SetMessage(msg).IsEphemeral().Send() + return &interaction.Response{ + Type: types.InteractionResponseChannelMessageWithSource, + Data: &interaction.ResponseData{ + Components: content, + Flags: channel.MessageFlagsEphemeral | channel.MessageFlagsIsComponentsV2, + }, + } +} + +func ConfigCommand( + s bot.Session, + i *event.InteractionCreate, + _ cmd.OptionMap, + resp *cmd.ResponseBuilder, +) { + err := s.InteractionAPI().Respond(i.Interaction, ConfigResponse(i)) + + if err != nil { + s.Logger().Error("sending config", "error", err) + } +} + +func ConfigMessageComponent( + s bot.Session, + i *event.InteractionCreate, + _ *interaction.MessageComponentData, + _ *cmd.ResponseBuilder, +) { + response := ConfigResponse(i) + response.Type = types.InteractionResponseUpdateMessage + err := s.InteractionAPI().Respond(i.Interaction, response) + + if err != nil { + s.Logger().Error("sending config", "error", err) + } +} + +func ConfigModal( + s bot.Session, + i *event.InteractionCreate, + _ *interaction.ModalSubmitData, + _ *cmd.ResponseBuilder, +) { + response := ConfigResponse(i) + response.Type = types.InteractionResponseUpdateMessage + err := s.InteractionAPI().Respond(i.Interaction, response) + if err != nil { - logger.Alert("config/guild.go - Sending config", err.Error()) + s.Logger().Error("sending config", "error", err) } } |
