Update tag commands to use error channel

pull/1/head
DustyP 6 years ago
parent 4744a54079
commit 73a5bad338

@ -2,6 +2,7 @@ package exts
import ( import (
"djpianalto.com/goff/djpianalto.com/goff/utils" "djpianalto.com/goff/djpianalto.com/goff/utils"
"errors"
"fmt" "fmt"
"github.com/dustinpianalto/disgoman" "github.com/dustinpianalto/disgoman"
"github.com/kballard/go-shellquote" "github.com/kballard/go-shellquote"
@ -9,12 +10,16 @@ import (
"strings" "strings"
) )
func addTagCommand(ctx disgoman.Context, args []string) error { func addTagCommand(ctx disgoman.Context, args []string) {
if len(args) >= 1 { if len(args) >= 1 {
args, err := shellquote.Split(strings.Join(args, " ")) args, err := shellquote.Split(strings.Join(args, " "))
if err != nil { if err != nil {
ctx.Send(err.Error()) ctx.ErrorChannel <- disgoman.CommandError{
return err Context: ctx,
Message: "",
Error: err,
}
return
} }
queryString := `SELECT tags.id, tags.tag, tags.content from tags queryString := `SELECT tags.id, tags.tag, tags.content from tags
WHERE tags.guild_id = $1 WHERE tags.guild_id = $1
@ -24,45 +29,75 @@ func addTagCommand(ctx disgoman.Context, args []string) error {
if err := row.Scan(&dest); err != nil { if err := row.Scan(&dest); err != nil {
tag := args[0] tag := args[0]
if tag == "" { if tag == "" {
ctx.Send("That is not a valid tag name") ctx.ErrorChannel <- disgoman.CommandError{
return nil Context: ctx,
Message: "That is not a valid tag name",
Error: err,
}
return
} }
if len(args) <= 1 { if len(args) <= 1 {
ctx.Send("I got a name but no value.") ctx.ErrorChannel <- disgoman.CommandError{
return nil Context: ctx,
Message: "I got a name but no value",
Error: err,
}
return
} }
value := args[1] value := args[1]
if value == "" { if value == "" {
ctx.Send("You have to include a content for the tag") ctx.ErrorChannel <- disgoman.CommandError{
return nil Context: ctx,
Message: "You have to include a content for the tag",
Error: err,
}
return
} }
queryString = `INSERT INTO tags (tag, content, creator, guild_id) VALUES ($1, $2, $3, $4);` queryString = `INSERT INTO tags (tag, content, creator, guild_id) VALUES ($1, $2, $3, $4);`
_, err := utils.Database.Exec(queryString, tag, value, ctx.Message.Author.ID, ctx.Guild.ID) _, err := utils.Database.Exec(queryString, tag, value, ctx.Message.Author.ID, ctx.Guild.ID)
if err != nil { if err != nil {
ctx.Send(err.Error()) ctx.Send(err.Error())
return err ctx.ErrorChannel <- disgoman.CommandError{
Context: ctx,
Message: "",
Error: err,
}
return
} }
ctx.Send(fmt.Sprintf("Tag %v added successfully.", tag)) ctx.Send(fmt.Sprintf("Tag %v added successfully.", tag))
return nil return
} else { } else {
ctx.Send("That tag already exists.") ctx.ErrorChannel <- disgoman.CommandError{
return nil Context: ctx,
Message: "That tag already exists",
Error: err,
}
return
} }
} else { } else {
ctx.Send("You need to tell me what tag you want to add...") ctx.Send("You need to tell me what tag you want to add...")
return nil ctx.ErrorChannel <- disgoman.CommandError{
Context: ctx,
Message: "You need to tell me what tag you want to add...",
Error: errors.New("nothing to do"),
}
return
} }
} }
func tagCommand(ctx disgoman.Context, args []string) error { func tagCommand(ctx disgoman.Context, args []string) {
if len(args) >= 1 { if len(args) >= 1 {
tagString := strings.Join(args, " ") tagString := strings.Join(args, " ")
queryString := `SELECT tags.id, tags.tag, tags.content from tags queryString := `SELECT tags.id, tags.tag, tags.content from tags
WHERE tags.guild_id = $1;` WHERE tags.guild_id = $1;`
rows, err := utils.Database.Query(queryString, ctx.Guild.ID) rows, err := utils.Database.Query(queryString, ctx.Guild.ID)
if err != nil { if err != nil {
ctx.Send(err.Error()) ctx.ErrorChannel <- disgoman.CommandError{
return err Context: ctx,
Message: "",
Error: err,
}
return
} else { } else {
for rows.Next() { for rows.Next() {
var ( var (
@ -75,14 +110,23 @@ func tagCommand(ctx disgoman.Context, args []string) error {
} }
if tagString == tag { if tagString == tag {
ctx.Send(content) ctx.Send(content)
return nil return
} }
} }
ctx.Send(fmt.Sprintf("Tag %v not found", args[0])) ctx.ErrorChannel <- disgoman.CommandError{
return nil Context: ctx,
Message: fmt.Sprintf("Tag %v not found", args[0]),
Error: err,
}
return
} }
} else { } else {
ctx.Send("I need a tag to check fetch...") ctx.Send("I need a tag to fetch...")
return nil ctx.ErrorChannel <- disgoman.CommandError{
Context: ctx,
Message: "I need a tag to fetch...",
Error: errors.New("nothing to do"),
}
return
} }
} }

Loading…
Cancel
Save