one step closer
This commit is contained in:
		
							
								
								
									
										57
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										57
									
								
								main.go
									
									
									
									
									
								
							| @@ -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.InteractionApplicationCommandAutocomplete { | ||||||
|  |  | ||||||
|  | 		commandUseCount++ | ||||||
|  |  | ||||||
|  | 		choices := generateDynamicChoices(commandUseCount) | ||||||
|  |  | ||||||
|  | 		s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ | ||||||
|  | 			Type: discordgo.InteractionApplicationCommandAutocompleteResult, | ||||||
|  | 			Data: &discordgo.InteractionResponseData{ | ||||||
|  | 				Choices: choices, | ||||||
|  | 			}, | ||||||
|  | 		}) | ||||||
|  | 	} | ||||||
| 	if i.Type == discordgo.InteractionApplicationCommand { | 	if i.Type == discordgo.InteractionApplicationCommand { | ||||||
| 		if i.ApplicationCommandData().Name == "test" { | 		if i.ApplicationCommandData().Name == "get" { | ||||||
| 			s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ |  | ||||||
| 				Type: discordgo.InteractionResponseChannelMessageWithSource, |  | ||||||
| 				Data: &discordgo.InteractionResponseData{ |  | ||||||
| 					Content: "You tested me!", |  | ||||||
| 				}, |  | ||||||
| 			}) |  | ||||||
| 		} |  | ||||||
| 		if i.ApplicationCommandData().Name == "secondtest" { |  | ||||||
| 			// 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{ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user