using UUIDs for tag command
This commit is contained in:
@@ -84,7 +84,7 @@ func GetTagCommand(s *discordgo.Session, i *discordgo.InteractionCreate, option
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: option.Value.(string),
|
||||
Content: getTagContent(i.GuildID, option.Value.(string)),
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -103,24 +103,18 @@ func AutocompleteTag(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
|
||||
func generateDynamicChoices(guildID string) []*discordgo.ApplicationCommandOptionChoice {
|
||||
choices := []*discordgo.ApplicationCommandOptionChoice{}
|
||||
keys, err := getTagKeys(guildID)
|
||||
IDs, err := getTagIDs(guildID)
|
||||
if err != nil {
|
||||
log.Println("Error getting tag keys:", err)
|
||||
return choices // Return empty choices if there's an error
|
||||
return choices
|
||||
}
|
||||
|
||||
for _, key := range keys {
|
||||
tagContent, err := getTag(guildID, key) // Assuming you have a getTag function
|
||||
if err != nil {
|
||||
log.Println("Error getting tag content:", err)
|
||||
continue // Skip this tag if there's an error
|
||||
}
|
||||
for _, id := range IDs {
|
||||
id_name := getTagName(guildID, id)
|
||||
choices = append(choices, &discordgo.ApplicationCommandOptionChoice{
|
||||
Name: key,
|
||||
Value: tagContent, //TODO: use IDs instead as PK
|
||||
Name: id_name,
|
||||
Value: id,
|
||||
})
|
||||
}
|
||||
|
||||
return choices
|
||||
}
|
||||
|
||||
|
1
go.mod
1
go.mod
@@ -8,6 +8,7 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/gorilla/websocket v1.4.2 // indirect
|
||||
github.com/iancoleman/strcase v0.3.0 // indirect
|
||||
github.com/lib/pq v1.10.9 // indirect
|
||||
|
2
go.sum
2
go.sum
@@ -1,5 +1,7 @@
|
||||
github.com/bwmarrin/discordgo v0.27.1 h1:ib9AIc/dom1E/fSIulrBwnez0CToJE113ZGt4HoliGY=
|
||||
github.com/bwmarrin/discordgo v0.27.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
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=
|
||||
|
1
main.go
1
main.go
@@ -32,6 +32,7 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
initTable()
|
||||
|
||||
discord, err := discordgo.New("Bot " + os.Getenv("BOT_TOKEN"))
|
||||
if err != nil {
|
||||
|
@@ -2,68 +2,92 @@ package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func initTable() {
|
||||
createTableQuery := `CREATE TABLE IF NOT EXISTS tags (
|
||||
tag_id TEXT NOT NULL,
|
||||
tag_name TEXT NOT NULL,
|
||||
tag_content TEXT,
|
||||
guild_id TEXT NOT NULL,
|
||||
PRIMARY KEY (tag_id,guild_id)
|
||||
);`
|
||||
|
||||
_, err := db.Exec(createTableQuery)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func addTag(guildID, tagName, tagContent string) bool {
|
||||
var exists bool
|
||||
err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM tags WHERE guild_id = $1 AND tag_name = $2)", guildID, tagName).Scan(&exists)
|
||||
id := uuid.New()
|
||||
err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM tags WHERE guild_id = $1 AND tag_id = $2)", guildID, id).Scan(&exists)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
// If the tag exists it updates it but TODO: needs to return a discord message to use the modify command with autocomplete
|
||||
if exists {
|
||||
_, err = db.Exec("UPDATE tags SET tag_content = $1 WHERE guild_id = $2 AND tag_name = $3", tagContent, guildID, tagName)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
} else {
|
||||
_, err = db.Exec("INSERT INTO tags (guild_id, tag_name, tag_content) VALUES ($1, $2, $3)", guildID, tagName, tagContent)
|
||||
// TODO: add modify command
|
||||
for exists {
|
||||
id = uuid.New()
|
||||
err = db.QueryRow("SELECT EXISTS (SELECT 1 FROM tags WHERE guild_id = $1 AND tag_id = $2)", guildID, id).Scan(&exists)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
_, err = db.Exec("INSERT INTO tags (guild_id, tag_name, tag_content, tag_id) VALUES ($1, $2, $3, $4)", guildID, tagName, tagContent, id)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
return exists
|
||||
}
|
||||
|
||||
func removeTag(guildID, tagContent string) {
|
||||
func removeTag(guildID string, tagID string) {
|
||||
var exists bool
|
||||
err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM tags WHERE guild_id = $1 AND tag_content = $2)", guildID, tagContent).Scan(&exists) // that is so dumb next commit i will make the tag IDs
|
||||
err := db.QueryRow("SELECT EXISTS (SELECT 1 FROM tags WHERE guild_id = $1 AND tag_id = $2)", guildID, tagID).Scan(&exists)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
if exists {
|
||||
_, err = db.Exec("DELETE FROM tags WHERE guild_id = $1 AND tag_content = $2", guildID, tagContent)
|
||||
_, err = db.Exec("DELETE FROM tags WHERE guild_id = $1 AND tag_id = $2", guildID, tagID)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getTagKeys(guildID string) ([]string, error) {
|
||||
var keys []string
|
||||
rows, err := db.Query("SELECT tag_name FROM tags WHERE guild_id = $1", guildID)
|
||||
func getTagIDs(guildID string) ([]string, error) {
|
||||
var IDs []string
|
||||
rows, err := db.Query("SELECT tag_id FROM tags WHERE guild_id = $1", guildID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var key string
|
||||
if err := rows.Scan(&key); err != nil {
|
||||
var id string
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keys = append(keys, key)
|
||||
IDs = append(IDs, id)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return keys, nil
|
||||
return IDs, nil
|
||||
}
|
||||
|
||||
func getTag(guildID, tagName string) (string, error) {
|
||||
var tagContent string
|
||||
err := db.QueryRow("SELECT tag_content FROM tags WHERE guild_id = $1 AND tag_name = $2", guildID, tagName).Scan(&tagContent)
|
||||
return tagContent, err
|
||||
func getTagName(guildID string, tagID string) string {
|
||||
var tagName string
|
||||
db.QueryRow("SELECT tag_name FROM tags WHERE guild_id = $1 AND tag_id = $2", guildID, tagID).Scan(&tagName)
|
||||
return tagName
|
||||
}
|
||||
func getTagContent(guildID string, tagID string) string {
|
||||
var tagContent string
|
||||
db.QueryRow("SELECT tag_content FROM tags WHERE guild_id = $1 AND tag_id = $2", guildID, tagID).Scan(&tagContent)
|
||||
return tagContent
|
||||
}
|
||||
|
Reference in New Issue
Block a user