M faceit.go => faceit.go +0 -2
@@ 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 {
M main.go => main.go +13 -39
@@ 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)
}
}
})
M settings.go => settings.go +15 -3
@@ 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
}
M static_commands.go => static_commands.go +17 -8
@@ 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
}