basic functionality works

This commit is contained in:
2024-02-15 16:54:26 +01:00
parent 6d1a81a279
commit 1fc565a3ec
3 changed files with 45 additions and 46 deletions

View File

@@ -63,8 +63,7 @@ var short_get_tag_command Command = Command{
func GetTagCommand(s *discordgo.Session, i *discordgo.InteractionCreate, option *discordgo.ApplicationCommandInteractionDataOption) {
if i.Type == discordgo.InteractionApplicationCommandAutocomplete {
commandUseCount++
choices := generateDynamicChoices(commandUseCount)
choices := generateDynamicChoices()
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionApplicationCommandAutocompleteResult,
Data: &discordgo.InteractionResponseData{
@@ -84,6 +83,18 @@ func GetTagCommand(s *discordgo.Session, i *discordgo.InteractionCreate, option
}
}
func generateDynamicChoices() []*discordgo.ApplicationCommandOptionChoice {
choices := []*discordgo.ApplicationCommandOptionChoice{}
keys := tags.getTagKeys()
for i := 0; i <= len(keys)-1; i++ {
choices = append(choices, &discordgo.ApplicationCommandOptionChoice{
Name: keys[i],
Value: tags.Tags[keys[i]],
})
}
return choices
}
func (tag_command Command) Interaction(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.ApplicationCommandData().Options[0].Name {
case "get":
@@ -95,7 +106,7 @@ func (tag_command Command) Interaction(s *discordgo.Session, i *discordgo.Intera
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Tag added!",
Flags: 1 << 6, // ephemeral message
Flags: discordgo.MessageFlagsEphemeral,
},
})
}

13
main.go
View File

@@ -60,19 +60,6 @@ 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) {
switch i.ApplicationCommandData().Name {
case "tag":

View File

@@ -13,48 +13,49 @@ type Tags struct {
}
var tags Tags
var filename string = "data.json"
func readTags(filename string) (*Tags, error) {
func readTags() {
bytes, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
err = json.Unmarshal(bytes, &tags)
if err != nil {
return nil, err
}
return &tags, nil
}
func writeTags(filename string, tags *Tags) error {
jsonBytes, err := json.MarshalIndent(tags, "", " ")
if err != nil {
return err
}
err = os.WriteFile(filename, jsonBytes, 0644)
if err != nil {
return err
}
return nil
}
func addTag(tags *Tags, tagKey string, tagValue string) {
tags, err := readTags("data.json")
if err != nil {
log.Fatalf("Failed to read tags: %v", err)
}
tags.Tags[tagKey] = tagValue
err = writeTags("data.json", tags)
err = json.Unmarshal(bytes, &tags)
if err != nil {
log.Fatalf("Failed to read tags: %v", err)
}
}
func writeTags() {
jsonBytes, err := json.MarshalIndent(&tags, "", " ")
if err != nil {
log.Fatalf("Failed to write tags: %v", err)
}
err = os.WriteFile(filename, jsonBytes, 0644)
if err != nil {
log.Fatalf("Failed to write tags: %v", err)
}
}
func addTag(tags *Tags, tagKey string, tagValue string) {
readTags()
tags.Tags[tagKey] = tagValue
writeTags()
}
func removeTag(tags *Tags, tagKey string) {
readTags()
delete(tags.Tags, tagKey)
writeTags()
}
func (tags Tags) getTagKeys() []string {
readTags()
keys := make([]string, 0, len(tags.Tags))
for k := range tags.Tags {
keys = append(keys, k)
}
return keys
}
func modifyTag(tags *Tags, tagKey string, newTagValue string) {