added tag add command
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/iancoleman/strcase"
|
||||
)
|
||||
|
||||
var tag_command Command = Command{
|
||||
@@ -23,6 +24,25 @@ var tag_command Command = Command{
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Name: "add",
|
||||
Description: "A command to add messages saved to the bot.",
|
||||
Type: discordgo.ApplicationCommandOptionSubCommand,
|
||||
Options: []*discordgo.ApplicationCommandOption{
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
Name: "tag",
|
||||
Description: "Your tag for the saved message",
|
||||
Required: true,
|
||||
},
|
||||
{
|
||||
Type: discordgo.ApplicationCommandOptionString,
|
||||
Name: "content",
|
||||
Description: "Your content for the saved message",
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}}
|
||||
|
||||
@@ -68,6 +88,16 @@ func (tag_command Command) Interaction(s *discordgo.Session, i *discordgo.Intera
|
||||
switch i.ApplicationCommandData().Options[0].Name {
|
||||
case "get":
|
||||
GetTagCommand(s, i, i.ApplicationCommandData().Options[0].Options[0])
|
||||
case "add":
|
||||
option := i.ApplicationCommandData().Options[0]
|
||||
addTag(&tags, strcase.ToSnake(option.Options[0].StringValue()) /*TODO: tag regex*/, option.Options[1].StringValue())
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "Tag added!",
|
||||
Flags: 1 << 6, // ephemeral message
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
1
go.mod
1
go.mod
@@ -9,6 +9,7 @@ require (
|
||||
|
||||
require (
|
||||
github.com/gorilla/websocket v1.4.2 // indirect
|
||||
github.com/iancoleman/strcase v0.3.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
|
||||
)
|
||||
|
2
go.sum
2
go.sum
@@ -2,6 +2,8 @@ github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4Ho
|
||||
github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
||||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/iancoleman/strcase v0.3.0 h1:nTXanmYxhfFAMjZL34Ov6gkzEsSJZ5DbhxWjvSASxEI=
|
||||
github.com/iancoleman/strcase v0.3.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
|
||||
|
@@ -12,13 +12,13 @@ type Tags struct {
|
||||
Tags map[string]string `json:"tags"`
|
||||
}
|
||||
|
||||
var tags Tags
|
||||
|
||||
func readTags(filename string) (*Tags, error) {
|
||||
bytes, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tags Tags
|
||||
err = json.Unmarshal(bytes, &tags)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -42,7 +42,15 @@ func writeTags(filename string, tags *Tags) error {
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to write tags: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func removeTag(tags *Tags, tagKey string) {
|
||||
@@ -56,15 +64,5 @@ func modifyTag(tags *Tags, tagKey string, newTagValue string) {
|
||||
}
|
||||
|
||||
func debugTags() {
|
||||
tags, err := readTags("data.json")
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to read tags: %v", err)
|
||||
}
|
||||
|
||||
addTag(tags, "new_command", "a new command description")
|
||||
|
||||
err = writeTags("data.json", tags)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to write tags: %v", err)
|
||||
}
|
||||
addTag(&tags, "new_command", "a new command description")
|
||||
}
|
||||
|
Reference in New Issue
Block a user