Files
thoughtful-dcbot/commands/setup.go
Space-Banane 66cf67d389 first commit
2026-01-21 22:55:58 +01:00

113 lines
2.7 KiB
Go

package commands
import (
"strings"
"github.com/bwmarrin/discordgo"
)
func newSetupCommand() *Cmd {
return &Cmd{
Command: &discordgo.ApplicationCommand{
Name: "setup",
Description: "Configure your Thoughtful instance",
},
Handler: handleSetup,
}
}
func handleSetup(s *discordgo.Session, ic *discordgo.InteractionCreate) {
err := s.InteractionRespond(ic.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseModal,
Data: &discordgo.InteractionResponseData{
CustomID: "setup_modal",
Title: "Thoughtful Setup",
Components: []discordgo.MessageComponent{
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.TextInput{
CustomID: "instance_url",
Label: "Instance URL",
Style: discordgo.TextInputShort,
Placeholder: "https://your-thoughtful-instance.com",
Required: true,
},
},
},
discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
discordgo.TextInput{
CustomID: "api_key",
Label: "API Key",
Style: discordgo.TextInputShort,
Placeholder: "Your API Key",
Required: true,
},
},
},
},
},
})
if err != nil {
// fallback if modal fails (rare)
}
}
func handleSetupSubmit(s *discordgo.Session, ic *discordgo.InteractionCreate) {
data := ic.ModalSubmitData()
url := ""
key := ""
for _, comp := range data.Components {
// ActionsRow -> TextInput
if row, ok := comp.(*discordgo.ActionsRow); ok {
for _, c := range row.Components {
if input, ok := c.(*discordgo.TextInput); ok {
if input.CustomID == "instance_url" {
url = strings.TrimSpace(input.Value)
} else if input.CustomID == "api_key" {
key = strings.TrimSpace(input.Value)
}
}
}
}
}
// Normalize URL
url = strings.TrimSuffix(url, "/")
if url == "" || key == "" {
s.InteractionRespond(ic.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Both URL and API Key are required.",
Flags: discordgo.MessageFlagsEphemeral,
},
})
return
}
user := ic.User
if user == nil {
user = ic.Member.User
}
err := SaveUserConfig(user.ID, UserConfig{
InstanceURL: url,
APIKey: key,
})
content := "Configuration saved successfully!"
if err != nil {
content = "Error saving configuration: " + err.Error()
}
s.InteractionRespond(ic.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: content,
Flags: discordgo.MessageFlagsEphemeral,
},
})
}