Compare commits

1 Commits

Author SHA1 Message Date
02c543922d horsebot: make skill responses ephemeral with share button 2026-03-09 11:09:53 -04:00
2 changed files with 29 additions and 8 deletions

View File

@@ -84,7 +84,8 @@ func main() {
r.Route("/skill", func(r handler.Router) { r.Route("/skill", func(r handler.Router) {
r.SlashCommand("/", skillSrv.slash) r.SlashCommand("/", skillSrv.slash)
r.Autocomplete("/", skillSrv.autocomplete) r.Autocomplete("/", skillSrv.autocomplete)
r.ButtonComponent("/{id}", skillSrv.button) r.ButtonComponent("/swap/{id}", skillSrv.button)
r.ButtonComponent("/share/{id}", skillSrv.share)
}) })
opts := []bot.ConfigOpt{bot.WithDefaultGateway(), bot.WithEventListeners(r)} opts := []bot.ConfigOpt{bot.WithDefaultGateway(), bot.WithEventListeners(r)}

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"log/slog"
"strconv" "strconv"
"strings" "strings"
@@ -64,8 +65,8 @@ func (s *skillServer) slash(data discord.SlashCommandInteractionData, e *handler
id = int64(v) id = int64(v)
} }
m := discord.MessageCreate{ m := discord.MessageCreate{
Components: []discord.LayoutComponent{s.render(horse.SkillID(id))}, Components: []discord.LayoutComponent{s.render(horse.SkillID(id), false)},
Flags: discord.MessageFlagIsComponentsV2, Flags: discord.MessageFlagIsComponentsV2 | discord.MessageFlagEphemeral,
} }
return e.CreateMessage(m) return e.CreateMessage(m)
} }
@@ -86,15 +87,31 @@ func (s *skillServer) button(data discord.ButtonInteractionData, e *handler.Comp
return e.CreateMessage(m) return e.CreateMessage(m)
} }
m := discord.MessageUpdate{ m := discord.MessageUpdate{
Components: &[]discord.LayoutComponent{s.render(horse.SkillID(id))}, Components: &[]discord.LayoutComponent{s.render(horse.SkillID(id), false)},
} }
return e.UpdateMessage(m) return e.UpdateMessage(m)
} }
func (s *skillServer) render(id horse.SkillID) discord.ContainerComponent { func (s *skillServer) share(data discord.ButtonInteractionData, e *handler.ComponentEvent) error {
id, err := strconv.ParseInt(e.Vars["id"], 10, 32)
if err != nil {
m := discord.MessageCreate{
Content: "That button produced an invalid skill ID. That's not supposed to happen.",
Flags: discord.MessageFlagEphemeral,
}
return e.CreateMessage(m)
}
m := discord.MessageCreate{
Components: []discord.LayoutComponent{s.render(horse.SkillID(id), true)},
}
return e.CreateMessage(m)
}
func (s *skillServer) render(id horse.SkillID, share bool) discord.ContainerComponent {
skill, ok := s.skills[id] skill, ok := s.skills[id]
if !ok { if !ok {
return discord.NewContainer(discord.NewTextDisplayf("invalid skill ID %v made it to RenderSkill", id)) slog.Error("invalid skill id", slog.Int("id", int(id)), slog.Bool("share", share))
return discord.NewContainer(discord.NewTextDisplayf("invalid skill ID %v made it to render", id))
} }
thumburl := fmt.Sprintf("https://gametora.com/images/umamusume/skill_icons/utx_ico_skill_%d.png", skill.IconID) thumburl := fmt.Sprintf("https://gametora.com/images/umamusume/skill_icons/utx_ico_skill_%d.png", skill.IconID)
@@ -180,15 +197,18 @@ func (s *skillServer) render(id horse.SkillID) discord.ContainerComponent {
rel = append(rel, s.skills[id]) rel = append(rel, s.skills[id])
} }
} }
if len(rel) > 1 { if len(rel) > 1 || !share {
buttons := make([]discord.InteractiveComponent, 0, 4) buttons := make([]discord.InteractiveComponent, 0, 4)
for _, rs := range rel { for _, rs := range rel {
b := discord.NewSecondaryButton(rs.Name, fmt.Sprintf("/skill/%d", rs.ID)) b := discord.NewSecondaryButton(rs.Name, fmt.Sprintf("/skill/swap/%d", rs.ID))
if rs.ID == id { if rs.ID == id {
b = b.AsDisabled() b = b.AsDisabled()
} }
buttons = append(buttons, b) buttons = append(buttons, b)
} }
if !share {
buttons = append(buttons, discord.NewPrimaryButton("Share", fmt.Sprintf("/skill/share/%d", skill.ID)))
}
r.Components = append(r.Components, discord.NewActionRow(buttons...)) r.Components = append(r.Components, discord.NewActionRow(buttons...))
} }
return r return r