@@ 34,41 34,40 @@ func main() {
}
client := twitch.NewClient(cfg.UserName, cfg.Token)
+ bot := &bot{client: client, cfg: cfg}
client.OnPrivateMessage(func(message twitch.PrivateMessage) {
parsed := strings.Split(message.Message, " ")
// NOTE: Handle commands here
if parsed[0][0:1] == "!" {
- useReply := cfg.Channels[message.Channel].UseReply
-
// TODO: refactor for unified error handling
switch parsed[0] {
case "!set":
err := handleSet(parsed, message, cfg.FaceitAPIKey)
if errors.Is(err, errNotAuthorized) {
- say(useReply, client, message, "You are not allowed to do this.")
+ bot.say(&message, "You are not allowed to do this.")
} else if errors.Is(err, errTooShort) {
- say(useReply, client, message, "The command you entered is too short.")
+ bot.say(&message, "The command you entered is too short.")
} else if errors.Is(err, errNoFaceitAccount) {
- say(useReply, client, message, "No Faceit account with that name found.")
+ 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 {
- say(useReply, client, message, "Command successful.")
+ bot.say(&message, "Command successful.")
}
case "!commands":
err := handleCommands(parsed, message)
if errors.Is(err, errNotAuthorized) {
- say(useReply, client, message, "You are not allowed to do this.")
+ bot.say(&message, "You are not allowed to do this.")
} else if errors.Is(err, errTooShort) {
- say(useReply, client, message, "The command you entered is too short.")
+ bot.say(&message, "The command you entered is too short.")
} else if err == errNotFound {
- say(useReply, client, message, "The command was not found.")
+ bot.say(&message, "The command was not found.")
} else if err != nil {
log.Printf("commands (channel %s) failed: %v\n", message.Channel, err)
} else {
- say(useReply, client, message, "Command successfully updated.")
+ bot.say(&message, "Command successfully updated.")
}
case "!elo":
@@ 76,12 75,12 @@ func main() {
if errors.Is(err, sql.ErrNoRows) {
// Only notify channel owner
if message.User.Name == message.Channel {
- say(useReply, client, message, "No Faceit account linked yet. Use '!set faceit ACCOUNTNAME")
+ bot.say(&message, "No Faceit account linked yet. Use '!set faceit ACCOUNTNAME")
}
} else if err != nil {
log.Printf("getElo (channel %s) failed: %v\n", message.Channel, err)
} else {
- say(useReply, client, message, strconv.Itoa(elo))
+ bot.say(&message, strconv.Itoa(elo))
}
default:
@@ 91,7 90,7 @@ func main() {
} else if err != nil {
log.Println(err)
} else {
- say(useReply, client, message, resp)
+ bot.say(&message, resp)
}
}
}
@@ 111,14 110,6 @@ func main() {
shutdown(<-sigs)
}
-func say(reply bool, client *twitch.Client, msg twitch.PrivateMessage, text string) {
- if reply {
- client.Reply(msg.Channel, msg.ID, text)
- } else {
- client.Say(msg.Channel, text)
- }
-}
-
func shutdown(sig os.Signal) {
err := database.Close()
if err != nil {