~xenrox/twitch-bot

77585ef2d16176e1566df10386f68253ec3471e7 — Thorben Günther 1 year, 6 months ago 3d25b31
Refactor to use a bot struct

Contains the config and the twitch client. Declaring methods on this
struct allows for more concise functions.
2 files changed, 29 insertions(+), 21 deletions(-)

A bot.go
M main.go
A bot.go => bot.go +17 -0
@@ 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)
	}
}

M main.go => main.go +12 -21
@@ 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 {