prepared json saving, added some code structuring
This commit is contained in:
54
command_tag.go
Normal file
54
command_tag.go
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var tag_command Command = Command{
|
||||||
|
Definition: discordgo.ApplicationCommand{
|
||||||
|
Name: "get",
|
||||||
|
Description: "A command to get messages saved to the bot.",
|
||||||
|
Options: []*discordgo.ApplicationCommandOption{
|
||||||
|
{
|
||||||
|
Type: discordgo.ApplicationCommandOptionString,
|
||||||
|
Name: "tag",
|
||||||
|
Description: "Your predefined tag for the saved message",
|
||||||
|
Required: true,
|
||||||
|
Autocomplete: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tag_command Command) Interaction(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 len(i.ApplicationCommandData().Options) > 0 {
|
||||||
|
// Loop through the options and handle them
|
||||||
|
for _, option := range i.ApplicationCommandData().Options {
|
||||||
|
switch option.Name {
|
||||||
|
case "tag":
|
||||||
|
value := option.Value.(string)
|
||||||
|
response := fmt.Sprintf("You provided the tag: %s", value)
|
||||||
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: response,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
52
main.go
52
main.go
@@ -10,8 +10,13 @@ import (
|
|||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Command struct {
|
||||||
|
Definition discordgo.ApplicationCommand
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
godotenv.Load()
|
godotenv.Load()
|
||||||
|
debugTags()
|
||||||
discord, err := discordgo.New("Bot " + os.Getenv("BOT_TOKEN"))
|
discord, err := discordgo.New("Bot " + os.Getenv("BOT_TOKEN"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("error creating Discord session,", err)
|
fmt.Println("error creating Discord session,", err)
|
||||||
@@ -40,19 +45,7 @@ func main() {
|
|||||||
|
|
||||||
func ready(s *discordgo.Session, event *discordgo.Ready) {
|
func ready(s *discordgo.Session, event *discordgo.Ready) {
|
||||||
commands := []*discordgo.ApplicationCommand{
|
commands := []*discordgo.ApplicationCommand{
|
||||||
{
|
&tag_command.Definition,
|
||||||
Name: "get",
|
|
||||||
Description: "A command to get messages saved to the bot.",
|
|
||||||
Options: []*discordgo.ApplicationCommandOption{
|
|
||||||
{
|
|
||||||
Type: discordgo.ApplicationCommandOptionString,
|
|
||||||
Name: "tag",
|
|
||||||
Description: "Your predefined tag for the saved message",
|
|
||||||
Required: true,
|
|
||||||
Autocomplete: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, guild := range event.Guilds {
|
for _, guild := range event.Guilds {
|
||||||
@@ -80,39 +73,8 @@ func generateDynamicChoices(count int) []*discordgo.ApplicationCommandOptionChoi
|
|||||||
var commandUseCount int
|
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.ApplicationCommandData().Name == "get" {
|
if i.ApplicationCommandData().Name == "get" {
|
||||||
// Check if the command has options
|
tag_command.Interaction(s, i)
|
||||||
if len(i.ApplicationCommandData().Options) > 0 {
|
|
||||||
// Loop through the options and handle them
|
|
||||||
for _, option := range i.ApplicationCommandData().Options {
|
|
||||||
switch option.Name {
|
|
||||||
case "tag":
|
|
||||||
value := option.Value.(string)
|
|
||||||
response := fmt.Sprintf("You provided the tag: %s", value)
|
|
||||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
|
||||||
Data: &discordgo.InteractionResponseData{
|
|
||||||
Content: response,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
70
manage_data.go
Normal file
70
manage_data.go
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Tags struct {
|
||||||
|
Tags map[string]string `json:"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
|
||||||
|
}
|
||||||
|
|
||||||
|
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.Tags[tagKey] = tagValue
|
||||||
|
}
|
||||||
|
|
||||||
|
func removeTag(tags *Tags, tagKey string) {
|
||||||
|
delete(tags.Tags, tagKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
func modifyTag(tags *Tags, tagKey string, newTagValue string) {
|
||||||
|
if _, exists := tags.Tags[tagKey]; exists {
|
||||||
|
tags.Tags[tagKey] = newTagValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func debugTags() {
|
||||||
|
tags, err := readTags("tags.json")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to read tags: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
addTag(tags, "new_command", "a new command description")
|
||||||
|
removeTag(tags, "test_command")
|
||||||
|
modifyTag(tags, "another_test_command", "updated command description")
|
||||||
|
|
||||||
|
err = writeTags("tags.json", tags)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to write tags: %v", err)
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user