From ea8192eb4916587027eab878bd60482c268bc9ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Fri, 30 Sep 2022 12:16:53 +0200 Subject: [PATCH] Refactor for unified error handling --- faceit.go | 2 -- main.go | 52 ++++++++++++---------------------------------- settings.go | 18 +++++++++++++--- static_commands.go | 25 +++++++++++++++------- 4 files changed, 45 insertions(+), 52 deletions(-) diff --git a/faceit.go b/faceit.go index 34d72b0..91db106 100644 --- a/faceit.go +++ b/faceit.go @@ -14,8 +14,6 @@ import ( "github.com/gempir/go-twitch-irc/v3" ) -// TODO: check if faceitAPIKey is set - var errNoFaceitAccount = errors.New("no faceit account found") type faceitClient struct { diff --git a/main.go b/main.go index 790f8c5..39fa05f 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - "database/sql" "errors" "flag" "log" @@ -37,53 +36,28 @@ func main() { client.OnPrivateMessage(func(message twitch.PrivateMessage) { parsed := strings.Split(message.Message, " ") - // NOTE: Handle commands here if parsed[0][0:1] == "!" { - // TODO: refactor for unified error handling + var err error switch parsed[0] { case "!set": - err := bot.handleSet(parsed, message, cfg.FaceitAPIKey) - if errors.Is(err, errNotAuthorized) { - bot.say(&message, "You are not allowed to do this.") - } else if errors.Is(err, errTooShort) { - bot.say(&message, "The command you entered is too short.") - } else if errors.Is(err, errNoFaceitAccount) { - bot.say(&message, "No Faceit account with that name found.") - } else if err != nil { - log.Printf("set (channel %s) failed: %v\n", message.Channel, err) - } else { - bot.say(&message, "Command successful.") - } + err = bot.handleSet(parsed, &message) case "!commands": - err := bot.handleCommands(parsed, message) - if errors.Is(err, errNotAuthorized) { - bot.say(&message, "You are not allowed to do this.") - } else if errors.Is(err, errTooShort) { - bot.say(&message, "The command you entered is too short.") - } else if err == errNotFound { - bot.say(&message, "The command was not found.") - } else if err != nil { - log.Printf("commands (channel %s) failed: %v\n", message.Channel, err) - } else { - bot.say(&message, "Command successfully updated.") - } + err = bot.handleCommands(parsed, &message) case "!elo": - err := bot.handleFaceitCommands(parsed, &message) - if err != nil { - log.Printf("faceit command %v (channel %s) failed: %v\n", parsed, message.Channel, err) - } + err = bot.handleFaceitCommands(parsed, &message) default: - resp, err := bot.lookupCommand(parsed[0], message.Channel) - if errors.Is(err, sql.ErrNoRows) { - log.Printf("command %q not found\n", parsed[0]) - } else if err != nil { - log.Println(err) - } else { - bot.say(&message, resp) - } + err = bot.lookupCommand(parsed[0], &message) + } + + if errors.Is(err, errNotAuthorized) { + bot.say(&message, "You are not allowed to do this.") + } else if errors.Is(err, errTooShort) { + bot.say(&message, "The command you entered is too short.") + } else if err != nil { + log.Printf("command %v (channel %s) failed: %v\n", parsed, message.Channel, err) } } }) diff --git a/settings.go b/settings.go index fb6f72c..abb32cb 100644 --- a/settings.go +++ b/settings.go @@ -1,8 +1,12 @@ package main -import "github.com/gempir/go-twitch-irc/v3" +import ( + "errors" -func (b *bot) handleSet(parsed []string, msg twitch.PrivateMessage, faceitAPIKey string) error { + "github.com/gempir/go-twitch-irc/v3" +) + +func (b *bot) handleSet(parsed []string, msg *twitch.PrivateMessage) error { // NOTE: Only allow mods or owner to use this // Wait for https://github.com/gempir/go-twitch-irc/pull/188 to get merged if msg.User.Name != msg.Channel { @@ -14,8 +18,16 @@ func (b *bot) handleSet(parsed []string, msg twitch.PrivateMessage, faceitAPIKey } if parsed[1] == "faceit" { - return b.setFaceitID(faceitAPIKey, msg.Channel, parsed[2]) + err := b.setFaceitID(b.cfg.FaceitAPIKey, msg.Channel, parsed[2]) + if errors.Is(err, errNoFaceitAccount) { + b.say(msg, "No Faceit account with that name found.") + return nil + } else if err != nil { + b.say(msg, "Command failed.") + return err + } } + b.say(msg, "Success.") return nil } diff --git a/static_commands.go b/static_commands.go index 4ee3fd3..511a153 100644 --- a/static_commands.go +++ b/static_commands.go @@ -1,7 +1,9 @@ package main import ( + "database/sql" "errors" + "log" "strings" "github.com/gempir/go-twitch-irc/v3" @@ -9,9 +11,8 @@ import ( var errNotAuthorized = errors.New("not authorized") var errTooShort = errors.New("input too short") -var errNotFound = errors.New("command not found") -func (b *bot) handleCommands(parsed []string, msg twitch.PrivateMessage) error { +func (b *bot) handleCommands(parsed []string, msg *twitch.PrivateMessage) error { // NOTE: Only allow mods or owner to use this // Wait for https://github.com/gempir/go-twitch-irc/pull/188 to get merged if msg.User.Name != msg.Channel { @@ -41,6 +42,7 @@ func (b *bot) handleCommands(parsed []string, msg twitch.PrivateMessage) error { return err } + b.say(msg, "Success.") return nil } } @@ -59,16 +61,18 @@ func (b *bot) handleCommands(parsed []string, msg twitch.PrivateMessage) error { if err != nil { return err } else if count == 0 { - return errNotFound + b.say(msg, "Command not found.") + return errors.New("command not found") } + b.say(msg, "Success.") return nil } return nil } -func (b *bot) lookupCommand(name string, channel string) (string, error) { +func (b *bot) lookupCommand(name string, msg *twitch.PrivateMessage) error { var response string query := ` SELECT response @@ -77,9 +81,14 @@ func (b *bot) lookupCommand(name string, channel string) (string, error) { name = strings.TrimLeft(name, "!") - err := b.db.DB.QueryRow(query, channel, name).Scan(&response) - if err != nil { - return "", err + err := b.db.DB.QueryRow(query, msg.Channel, name).Scan(&response) + if errors.Is(err, sql.ErrNoRows) { + log.Printf("command %q not found (channel %s)\n", name, msg.Channel) + return nil + } else if err != nil { + return err } - return response, nil + + b.say(msg, response) + return nil } -- 2.44.0