From 77585ef2d16176e1566df10386f68253ec3471e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Fri, 23 Sep 2022 15:11:56 +0200 Subject: [PATCH] Refactor to use a bot struct Contains the config and the twitch client. Declaring methods on this struct allows for more concise functions. --- bot.go | 17 +++++++++++++++++ main.go | 33 ++++++++++++--------------------- 2 files changed, 29 insertions(+), 21 deletions(-) create mode 100644 bot.go diff --git a/bot.go b/bot.go new file mode 100644 index 0000000..31ce87a --- /dev/null +++ b/bot.go @@ -0,0 +1,17 @@ +package main + +import "github.com/gempir/go-twitch-irc/v3" + +type bot struct { + client *twitch.Client + cfg *config +} + +func (b *bot) say(msg *twitch.PrivateMessage, text string) { + useReply := b.cfg.Channels[msg.Channel].UseReply + if useReply { + b.client.Reply(msg.Channel, msg.ID, text) + } else { + b.client.Say(msg.Channel, text) + } +} diff --git a/main.go b/main.go index 54727e0..bb2ce32 100644 --- a/main.go +++ b/main.go @@ -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 { -- 2.44.0