one step closer

This commit is contained in:
2024-02-14 12:27:33 +01:00
parent 8634cede8a
commit 789f8a6a40

45
main.go
View File

@@ -41,18 +41,15 @@ func main() {
func ready(s *discordgo.Session, event *discordgo.Ready) { func ready(s *discordgo.Session, event *discordgo.Ready) {
commands := []*discordgo.ApplicationCommand{ commands := []*discordgo.ApplicationCommand{
{ {
Name: "test", Name: "get",
Description: "A test command.", Description: "A command to get messages saved to the bot.",
},
{
Name: "secondtest",
Description: "A second test command.",
Options: []*discordgo.ApplicationCommandOption{ Options: []*discordgo.ApplicationCommandOption{
{ {
Type: discordgo.ApplicationCommandOptionString, Type: discordgo.ApplicationCommandOptionString,
Name: "query", Name: "tag",
Description: "The query to search for.", Description: "Your predefined tag for the saved message",
Required: true, Required: true,
Autocomplete: true,
}, },
}, },
}, },
@@ -69,25 +66,43 @@ func ready(s *discordgo.Session, event *discordgo.Ready) {
} }
} }
func generateDynamicChoices(count int) []*discordgo.ApplicationCommandOptionChoice {
choices := []*discordgo.ApplicationCommandOptionChoice{}
for i := 1; i <= count; i++ {
choices = append(choices, &discordgo.ApplicationCommandOptionChoice{
Name: fmt.Sprintf("Option %d", i),
Value: fmt.Sprintf("option_%d", i),
})
}
return choices
}
var commandUseCount int
func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) { func interactionCreate(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.Type == discordgo.InteractionApplicationCommand { if i.Type == discordgo.InteractionApplicationCommandAutocomplete {
if i.ApplicationCommandData().Name == "test" {
commandUseCount++
choices := generateDynamicChoices(commandUseCount)
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionApplicationCommandAutocompleteResult,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{
Content: "You tested me!", Choices: choices,
}, },
}) })
} }
if i.ApplicationCommandData().Name == "secondtest" { if i.Type == discordgo.InteractionApplicationCommand {
if i.ApplicationCommandData().Name == "get" {
// Check if the command has options // Check if the command has options
if len(i.ApplicationCommandData().Options) > 0 { if len(i.ApplicationCommandData().Options) > 0 {
// Loop through the options and handle them // Loop through the options and handle them
for _, option := range i.ApplicationCommandData().Options { for _, option := range i.ApplicationCommandData().Options {
switch option.Name { switch option.Name {
case "query": case "tag":
value := option.Value.(string) value := option.Value.(string)
response := fmt.Sprintf("You provided the query: %s", value) response := fmt.Sprintf("You provided the tag: %s", value)
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource, Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{ Data: &discordgo.InteractionResponseData{