Merge pull request #1 from lyx0/rewrite

Rewrite
This commit is contained in:
lyx0 2021-10-19 23:55:03 +02:00 committed by GitHub
commit 1b7828032b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
34 changed files with 239 additions and 270 deletions

View file

@ -4,63 +4,33 @@ import (
"time" "time"
twitch "github.com/gempir/go-twitch-irc/v2" twitch "github.com/gempir/go-twitch-irc/v2"
cfg "github.com/lyx0/nourybot/pkg/config"
"github.com/lyx0/nourybot/pkg/handlers"
log "github.com/sirupsen/logrus"
) )
type Bot struct { type Bot struct {
twitchClient *twitch.Client TwitchClient *twitch.Client
cfg *cfg.Config
Uptime time.Time Uptime time.Time
} }
func NewBot(cfg *cfg.Config) *Bot { type Channel struct {
Name string
log.Info("fn Newbot")
twitchClient := twitch.NewClient(cfg.Username, cfg.Oauth)
return &Bot{
cfg: cfg,
twitchClient: twitchClient,
}
} }
func (b *Bot) Connect() error { func (b *Bot) Send(target, text string) {
log.Info("fn Connect") if len(text) == 0 {
return
b.Uptime = time.Now()
b.twitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) {
handlers.HandlePrivateMessage(message, b.twitchClient, b.cfg, b.Uptime)
})
b.twitchClient.OnWhisperMessage(func(message twitch.WhisperMessage) {
handlers.HandleWhisperMessage(message, b.twitchClient)
})
err := b.twitchClient.Connect()
if err != nil {
log.Error("Error Connecting from Twitch: ", err)
} }
return err // if message[0] == '.' || message[0] == '/' {
} // message = ". " + message
// }
func (b *Bot) Disconnect() error { if len(text) > 500 {
err := b.twitchClient.Disconnect() firstMessage := text[0:499]
if err != nil { secondMessage := text[499:]
log.Error("Error Disconnecting from Twitch: ", err) b.TwitchClient.Say(target, firstMessage)
b.TwitchClient.Say(target, secondMessage)
return
} }
return err b.TwitchClient.Say(target, text)
}
func (b *Bot) Say(channel string, message string) {
b.twitchClient.Say(channel, message)
}
func (b *Bot) Join(channel string) {
log.Info("fn Join")
b.twitchClient.Join(channel)
} }

View file

@ -1,17 +1,42 @@
package main package main
import ( import (
"fmt"
"time"
"github.com/gempir/go-twitch-irc/v2"
"github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/config" "github.com/lyx0/nourybot/pkg/config"
"github.com/lyx0/nourybot/pkg/handlers"
) )
var nb *bot.Bot
func main() { func main() {
cfg := config.LoadConfig() conf := config.LoadConfig()
nb := bot.NewBot(cfg)
nb.Join("nourybot") nb = &bot.Bot{
TwitchClient: twitch.NewClient(conf.Username, conf.Oauth),
Uptime: time.Now(),
}
nb.Say("nourybot", "HeyGuys") nb.TwitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) {
// If channelID is missing something must have gone wrong.
channelID := message.Tags["room-id"]
if channelID == "" {
fmt.Printf("Missing room-id tag in message")
return
}
nb.Connect() // So that the bot doesn't repeat itself.
if message.Tags["user-id"] == "596581605" {
return
}
handlers.PrivateMessage(message, nb)
})
nb.TwitchClient.Join("nouryqt", "nourybot")
nb.Send("nourybot", "HeyGuys")
nb.TwitchClient.Connect()
} }

View file

@ -3,18 +3,17 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api/aiden" "github.com/lyx0/nourybot/pkg/api/aiden"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
func BotStatus(channel string, name string, client *twitch.Client) { func BotStatus(channel string, name string, nb *bot.Bot) {
resp, err := aiden.ApiCall(fmt.Sprintf("api/v1/twitch/botStatus/%s?includeLimits=1", name)) resp, err := aiden.ApiCall(fmt.Sprintf("api/v1/twitch/botStatus/%s?includeLimits=1", name))
if err != nil { if err != nil {
client.Say(channel, "Something went wrong FeelsBadMan") nb.Send(channel, "Something went wrong FeelsBadMan")
log.Error(err) log.Error(err)
} }
client.Say(channel, resp) nb.Send(channel, resp)
} }

View file

@ -3,17 +3,17 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api/aiden" "github.com/lyx0/nourybot/pkg/api/aiden"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
func BttvEmotes(channel string, client *twitch.Client) { func BttvEmotes(channel string, nb *bot.Bot) {
resp, err := aiden.ApiCall(fmt.Sprintf("api/v1/emotes/%s/bttv", channel)) resp, err := aiden.ApiCall(fmt.Sprintf("api/v1/emotes/%s/bttv", channel))
if err != nil { if err != nil {
log.Error(err) log.Error(err)
client.Say(channel, "Something went wrong FeelsBadMan") nb.Send(channel, "Something went wrong FeelsBadMan")
} }
client.Say(channel, string(resp)) nb.Send(channel, string(resp))
} }

View file

@ -1,16 +1,16 @@
package commands package commands
import ( import (
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/utils" "github.com/lyx0/nourybot/pkg/utils"
) )
func Coinflip(channel string, client *twitch.Client) { func Coinflip(channel string, nb *bot.Bot) {
result := utils.GenerateRandomNumber(2) result := utils.GenerateRandomNumber(2)
if result == 1 { if result == 1 {
client.Say(channel, "Heads") nb.Send(channel, "Heads")
} else { } else {
client.Say(channel, "Tails") nb.Send(channel, "Tails")
} }
} }

View file

@ -4,11 +4,12 @@ import (
"fmt" "fmt"
"github.com/gempir/go-twitch-irc/v2" "github.com/gempir/go-twitch-irc/v2"
"github.com/lyx0/nourybot/cmd/bot"
) )
func Color(message twitch.PrivateMessage, client *twitch.Client) { func Color(message twitch.PrivateMessage, nb *bot.Bot) {
reply := fmt.Sprintf("@%v, your color is %v", message.User.DisplayName, message.User.Color) reply := fmt.Sprintf("@%v, your color is %v", message.User.DisplayName, message.User.Color)
client.Say(message.Channel, reply) nb.Send(message.Channel, reply)
} }

View file

@ -1,7 +1,7 @@
package commands package commands
import "github.com/gempir/go-twitch-irc/v2" import "github.com/lyx0/nourybot/cmd/bot"
func Echo(channel string, message string, client *twitch.Client) { func Echo(channel string, message string, nb *bot.Bot) {
client.Say(channel, message) nb.Send(channel, message)
} }

View file

@ -1,17 +1,17 @@
package commands package commands
import ( import (
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api/aiden" "github.com/lyx0/nourybot/pkg/api/aiden"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
func EightBall(channel string, client *twitch.Client) { func EightBall(channel string, nb *bot.Bot) {
resp, err := aiden.ApiCall("api/v1/misc/8ball") resp, err := aiden.ApiCall("api/v1/misc/8ball")
if err != nil { if err != nil {
log.Error(err) log.Error(err)
client.Say(channel, "Something went wrong FeelsBadMan") nb.Send(channel, "Something went wrong FeelsBadMan")
} }
client.Say(channel, string(resp)) nb.Send(channel, string(resp))
} }

View file

@ -3,18 +3,18 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api/aiden" "github.com/lyx0/nourybot/pkg/api/aiden"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
func FfzEmotes(channel string, client *twitch.Client) { func FfzEmotes(channel string, nb *bot.Bot) {
resp, err := aiden.ApiCall(fmt.Sprintf("api/v1/emotes/%s/ffz", channel)) resp, err := aiden.ApiCall(fmt.Sprintf("api/v1/emotes/%s/ffz", channel))
if err != nil { if err != nil {
log.Error(err) log.Error(err)
client.Say(channel, "Something went wrong FeelsBadMan") nb.Send(channel, "Something went wrong FeelsBadMan")
} }
client.Say(channel, string(resp)) nb.Send(channel, string(resp))
} }

View file

@ -4,12 +4,12 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
) )
func Fill(channel string, emote string, client *twitch.Client) { func Fill(channel string, emote string, nb *bot.Bot) {
if emote[0] == '.' || emote[0] == '/' { if emote[0] == '.' || emote[0] == '/' {
client.Say(channel, ":tf:") nb.Send(channel, ":tf:")
return return
} }
@ -20,6 +20,6 @@ func Fill(channel string, emote string, client *twitch.Client) {
repeatCount := (499 / emoteLength) repeatCount := (499 / emoteLength)
reply := strings.Repeat(fmt.Sprintf(emote+" "), repeatCount) reply := strings.Repeat(fmt.Sprintf(emote+" "), repeatCount)
client.Say(channel, reply) nb.Send(channel, reply)
} }

View file

@ -3,15 +3,15 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api/ivr" "github.com/lyx0/nourybot/pkg/api/ivr"
) )
func Firstline(channel string, streamer string, username string, client *twitch.Client) { func Firstline(channel string, streamer string, username string, nb *bot.Bot) {
ivrResponse, err := ivr.FirstLine(streamer, username) ivrResponse, err := ivr.FirstLine(streamer, username)
if err != nil { if err != nil {
client.Say(channel, fmt.Sprint(err)) nb.Send(channel, fmt.Sprint(err))
return return
} }
client.Say(channel, ivrResponse) nb.Send(channel, ivrResponse)
} }

View file

@ -3,14 +3,14 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api/ivr" "github.com/lyx0/nourybot/pkg/api/ivr"
) )
func Followage(channel string, streamer string, username string, client *twitch.Client) { func Followage(channel string, streamer string, username string, nb *bot.Bot) {
ivrResponse, err := ivr.Followage(streamer, username) ivrResponse, err := ivr.Followage(streamer, username)
if err != nil { if err != nil {
client.Say(channel, fmt.Sprint(err)) nb.Send(channel, fmt.Sprint(err))
} }
client.Say(channel, ivrResponse) nb.Send(channel, ivrResponse)
} }

View file

@ -3,18 +3,18 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api/aiden" "github.com/lyx0/nourybot/pkg/api/aiden"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
func Game(channel string, name string, client *twitch.Client) { func Game(channel string, name string, nb *bot.Bot) {
game, err := aiden.ApiCall(fmt.Sprintf("api/v1/twitch/channel/%s/game", name)) game, err := aiden.ApiCall(fmt.Sprintf("api/v1/twitch/channel/%s/game", name))
if err != nil { if err != nil {
client.Say(channel, "Something went wrong FeelsBadMan") nb.Send(channel, "Something went wrong FeelsBadMan")
log.Error(err) log.Error(err)
} }
reply := fmt.Sprintf("@%s was last seen playing: %s", name, game) reply := fmt.Sprintf("@%s was last seen playing: %s", name, game)
client.Say(channel, reply) nb.Send(channel, reply)
} }

View file

@ -3,11 +3,11 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
) )
func Godocs(channel string, searchTerm string, client *twitch.Client) { func Godocs(channel string, searchTerm string, nb *bot.Bot) {
resp := fmt.Sprintf("https://godocs.io/?q=%s", searchTerm) resp := fmt.Sprintf("https://godocs.io/?q=%s", searchTerm)
client.Say(channel, resp) nb.Send(channel, resp)
} }

View file

@ -1,16 +1,16 @@
package commands package commands
import ( import (
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api" "github.com/lyx0/nourybot/pkg/api"
) )
func RandomNumber(channel string, client *twitch.Client) { func RandomNumber(channel string, nb *bot.Bot) {
reply := api.RandomNumber() reply := api.RandomNumber()
client.Say(channel, string(reply)) nb.Send(channel, string(reply))
} }
func Number(channel string, number string, client *twitch.Client) { func Number(channel string, number string, nb *bot.Bot) {
reply := api.Number(number) reply := api.Number(number)
client.Say(channel, string(reply)) nb.Send(channel, string(reply))
} }

View file

@ -2,17 +2,17 @@ package commands
import ( import (
"fmt" "fmt"
"time"
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/humanize" "github.com/lyx0/nourybot/pkg/humanize"
"github.com/lyx0/nourybot/pkg/utils" "github.com/lyx0/nourybot/pkg/utils"
) )
func Ping(channel string, client *twitch.Client, uptime time.Time) { func Ping(target string, nb *bot.Bot) {
commandCount := fmt.Sprint(utils.GetCommandsUsed()) commandCount := fmt.Sprint(utils.GetCommandsUsed())
botUptime := humanize.Time(uptime) botUptime := humanize.Time(nb.Uptime)
reply := fmt.Sprintf("Pong! :) Commands used: %v, Last restart: %v", commandCount, botUptime) reply := fmt.Sprintf("Pong! :) Commands Used: %v, Last restart: %v", commandCount, botUptime)
client.Say(channel, reply)
nb.Send(target, reply)
} }

View file

@ -3,12 +3,11 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
) )
func Pingme(channel string, user string, client *twitch.Client) { func Pingme(channel string, user string, nb *bot.Bot) {
response := fmt.Sprintf("@%s", user) response := fmt.Sprintf("@%s", user)
client.Say(channel, response) nb.Send(channel, response)
} }

View file

@ -3,16 +3,16 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api/ivr" "github.com/lyx0/nourybot/pkg/api/ivr"
) )
func ProfilePicture(channel string, target string, client *twitch.Client) { func ProfilePicture(channel string, target string, nb *bot.Bot) {
reply, err := ivr.ProfilePicture(target) reply, err := ivr.ProfilePicture(target)
if err != nil { if err != nil {
client.Say(channel, fmt.Sprint(err)) nb.Send(channel, fmt.Sprint(err))
return return
} }
client.Say(channel, reply) nb.Send(channel, reply)
} }

View file

@ -5,17 +5,17 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
) )
func Pyramid(channel string, size string, emote string, client *twitch.Client) { func Pyramid(channel string, size string, emote string, nb *bot.Bot) {
if size[0] == '.' || size[0] == '/' { if size[0] == '.' || size[0] == '/' {
client.Say(channel, ":tf:") nb.Send(channel, ":tf:")
return return
} }
if emote[0] == '.' || emote[0] == '/' { if emote[0] == '.' || emote[0] == '/' {
client.Say(channel, ":tf:") nb.Send(channel, ":tf:")
return return
} }
@ -23,27 +23,27 @@ func Pyramid(channel string, size string, emote string, client *twitch.Client) {
pyramidEmote := fmt.Sprint(emote + " ") pyramidEmote := fmt.Sprint(emote + " ")
if err != nil { if err != nil {
client.Say(channel, "Something went wrong") nb.Send(channel, "Something went wrong")
} }
if pyramidSize == 1 { if pyramidSize == 1 {
client.Say(channel, fmt.Sprint(pyramidEmote+"...")) nb.Send(channel, fmt.Sprint(pyramidEmote+"..."))
return return
} }
if pyramidSize > 20 { if pyramidSize > 20 {
client.Say(channel, "Max pyramid size is 20") nb.Send(channel, "Max pyramid size is 20")
return return
} }
for i := 0; i <= pyramidSize; i++ { for i := 0; i <= pyramidSize; i++ {
pyramidMessageAsc := strings.Repeat(pyramidEmote, i) pyramidMessageAsc := strings.Repeat(pyramidEmote, i)
// fmt.Println(pyramidMessageAsc) // fmt.Println(pyramidMessageAsc)
client.Say(channel, pyramidMessageAsc) nb.Send(channel, pyramidMessageAsc)
} }
for j := pyramidSize - 1; j >= 0; j-- { for j := pyramidSize - 1; j >= 0; j-- {
pyramidMessageDesc := strings.Repeat(pyramidEmote, j) pyramidMessageDesc := strings.Repeat(pyramidEmote, j)
// fmt.Println(pyramidMessageDesc) // fmt.Println(pyramidMessageDesc)
client.Say(channel, pyramidMessageDesc) nb.Send(channel, pyramidMessageDesc)
} }
} }

View file

@ -1,12 +1,12 @@
package commands package commands
import ( import (
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api" "github.com/lyx0/nourybot/pkg/api"
) )
func RandomCat(channel string, client *twitch.Client) { func RandomCat(channel string, nb *bot.Bot) {
reply := api.RandomCat() reply := api.RandomCat()
client.Say(channel, reply) nb.Send(channel, reply)
} }

View file

@ -1,12 +1,12 @@
package commands package commands
import ( import (
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api" "github.com/lyx0/nourybot/pkg/api"
) )
func RandomDog(channel string, client *twitch.Client) { func RandomDog(channel string, nb *bot.Bot) {
reply := api.RandomDog() reply := api.RandomDog()
client.Say(channel, reply) nb.Send(channel, reply)
} }

View file

@ -1,12 +1,12 @@
package commands package commands
import ( import (
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api" "github.com/lyx0/nourybot/pkg/api"
) )
func RandomFox(channel string, client *twitch.Client) { func RandomFox(channel string, nb *bot.Bot) {
reply := api.RandomFox() reply := api.RandomFox()
client.Say(channel, reply) nb.Send(channel, reply)
} }

View file

@ -1,11 +1,11 @@
package commands package commands
import ( import (
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api" "github.com/lyx0/nourybot/pkg/api"
) )
func RandomXkcd(channel string, client *twitch.Client) { func RandomXkcd(channel string, nb *bot.Bot) {
reply := api.RandomXkcd() reply := api.RandomXkcd()
client.Say(channel, reply) nb.Send(channel, reply)
} }

View file

@ -3,16 +3,16 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api/ivr" "github.com/lyx0/nourybot/pkg/api/ivr"
) )
func Subage(channel string, username string, streamer string, client *twitch.Client) { func Subage(channel string, username string, streamer string, nb *bot.Bot) {
ivrResponse, err := ivr.Subage(username, streamer) ivrResponse, err := ivr.Subage(username, streamer)
if err != nil { if err != nil {
client.Say(channel, fmt.Sprint(err)) nb.Send(channel, fmt.Sprint(err))
return return
} }
client.Say(channel, ivrResponse) nb.Send(channel, ivrResponse)
} }

View file

@ -3,14 +3,14 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/utils" "github.com/lyx0/nourybot/pkg/utils"
) )
func Thumbnail(channel string, target string, client *twitch.Client) { func Thumbnail(channel string, target string, nb *bot.Bot) {
imageHeight := utils.GenerateRandomNumberRange(1040, 1080) imageHeight := utils.GenerateRandomNumberRange(1040, 1080)
imageWidth := utils.GenerateRandomNumberRange(1890, 1920) imageWidth := utils.GenerateRandomNumberRange(1890, 1920)
response := fmt.Sprintf("https://static-cdn.jtvnw.net/previews-ttv/live_user_%v-%vx%v.jpg", target, imageWidth, imageHeight) response := fmt.Sprintf("https://static-cdn.jtvnw.net/previews-ttv/live_user_%v-%vx%v.jpg", target, imageWidth, imageHeight)
client.Say(channel, response) nb.Send(channel, response)
} }

View file

@ -3,17 +3,17 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api/aiden" "github.com/lyx0/nourybot/pkg/api/aiden"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
func Title(channel string, target string, client *twitch.Client) { func Title(channel string, target string, nb *bot.Bot) {
title, err := aiden.ApiCall(fmt.Sprintf("api/v1/twitch/channel/%s/title", target)) title, err := aiden.ApiCall(fmt.Sprintf("api/v1/twitch/channel/%s/title", target))
if err != nil { if err != nil {
client.Say(channel, "Something went wrong FeelsBadMan") nb.Send(channel, "Something went wrong FeelsBadMan")
log.Error(err) log.Error(err)
} }
reply := fmt.Sprintf("%s title is: %s", target, title) reply := fmt.Sprintf("%s title is: %s", target, title)
client.Say(channel, reply) nb.Send(channel, reply)
} }

View file

@ -3,19 +3,19 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api/aiden" "github.com/lyx0/nourybot/pkg/api/aiden"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
func Uptime(channel string, name string, client *twitch.Client) { func Uptime(channel string, name string, nb *bot.Bot) {
resp, err := aiden.ApiCall(fmt.Sprintf("api/v1/twitch/channel/%s/uptime", name)) resp, err := aiden.ApiCall(fmt.Sprintf("api/v1/twitch/channel/%s/uptime", name))
if err != nil { if err != nil {
client.Say(channel, "Something went wrong FeelsBadMan") nb.Send(channel, "Something went wrong FeelsBadMan")
log.Error(err) log.Error(err)
} }
client.Say(channel, resp) nb.Send(channel, resp)
} }

View file

@ -1,11 +1,11 @@
package commands package commands
import ( import (
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api/ivr" "github.com/lyx0/nourybot/pkg/api/ivr"
) )
func Userid(channel string, target string, client *twitch.Client) { func Userid(channel string, target string, nb *bot.Bot) {
reply := ivr.Userid(target) reply := ivr.Userid(target)
client.Say(channel, reply) nb.Send(channel, reply)
} }

View file

@ -3,18 +3,18 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api/aiden" "github.com/lyx0/nourybot/pkg/api/aiden"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
func Weather(channel string, location string, client *twitch.Client) { func Weather(channel string, location string, nb *bot.Bot) {
reply, err := aiden.ApiCall(fmt.Sprintf("api/v1/misc/weather/%s", location)) reply, err := aiden.ApiCall(fmt.Sprintf("api/v1/misc/weather/%s", location))
if err != nil { if err != nil {
client.Say(channel, "Something went wrong FeelsBadMan") nb.Send(channel, "Something went wrong FeelsBadMan")
log.Error(err) log.Error(err)
} }
client.Say(channel, reply) nb.Send(channel, reply)
} }

View file

@ -1,7 +1,7 @@
package commands package commands
import "github.com/gempir/go-twitch-irc/v2" import "github.com/lyx0/nourybot/cmd/bot"
func Xd(channel string, client *twitch.Client) { func Xd(channel string, nb *bot.Bot) {
client.Say(channel, "xd") nb.Send(channel, "xd")
} }

View file

@ -1,12 +1,13 @@
package commands package commands
import ( import (
"github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api" "github.com/lyx0/nourybot/pkg/api"
) )
func Xkcd(channel string, client *twitch.Client) { func Xkcd(channel string, nb *bot.Bot) {
reply := api.Xkcd() reply := api.Xkcd()
client.Say(channel, reply)
nb.Send(channel, reply)
} }

View file

@ -2,23 +2,16 @@ package handlers
import ( import (
"strings" "strings"
"time"
"github.com/gempir/go-twitch-irc/v2" "github.com/gempir/go-twitch-irc/v2"
"github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/commands" "github.com/lyx0/nourybot/pkg/commands"
"github.com/lyx0/nourybot/pkg/utils" "github.com/lyx0/nourybot/pkg/utils"
log "github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
// HandleCommand receives a twitch.PrivateMessage from func Command(message twitch.PrivateMessage, nb *bot.Bot) {
// HandlePrivateMessage where it found a command in it. logrus.Info("fn Command")
// HandleCommand passes on the message to the specific
// command handler for further action.
func HandleCommand(message twitch.PrivateMessage, twitchClient *twitch.Client, uptime time.Time) {
log.Info("fn HandleCommand")
// Counter that increments on every command call.
utils.CommandUsed()
// commandName is the actual command name without the prefix. // commandName is the actual command name without the prefix.
commandName := strings.ToLower(strings.SplitN(message.Message, " ", 3)[0][2:]) commandName := strings.ToLower(strings.SplitN(message.Message, " ", 3)[0][2:])
@ -34,238 +27,242 @@ func HandleCommand(message twitch.PrivateMessage, twitchClient *twitch.Client, u
// Useful for checking if enough cmdParams are given. // Useful for checking if enough cmdParams are given.
msgLen := len(strings.SplitN(message.Message, " ", -2)) msgLen := len(strings.SplitN(message.Message, " ", -2))
// target channel
target := message.Channel
switch commandName { switch commandName {
case "": case "":
if msgLen == 1 { if msgLen == 1 {
twitchClient.Say(message.Channel, "Why yes, that's my prefix :)") nb.Send(target, "xd")
return
} }
case "botstatus": case "botstatus":
if msgLen == 1 { if msgLen == 1 {
twitchClient.Say(message.Channel, "Usage: ()botstatus [username]") nb.Send(target, "Usage: ()botstatus [username]")
return return
} else { } else {
commands.BotStatus(message.Channel, cmdParams[1], twitchClient) commands.BotStatus(target, cmdParams[1], nb)
return return
} }
case "bttvemotes": case "bttvemotes":
commands.BttvEmotes(message.Channel, twitchClient) commands.BttvEmotes(target, nb)
return return
case "cf": case "cf":
commands.Coinflip(message.Channel, twitchClient) commands.Coinflip(target, nb)
return return
case "coin": case "coin":
commands.Coinflip(message.Channel, twitchClient) commands.Coinflip(target, nb)
return return
case "coinflip": case "coinflip":
commands.Coinflip(message.Channel, twitchClient) commands.Coinflip(target, nb)
return return
case "color": case "color":
commands.Color(message, twitchClient) commands.Color(message, nb)
return return
case "mycolor": case "mycolor":
commands.Color(message, twitchClient) commands.Color(message, nb)
return return
case "echo": case "echo":
commands.Echo(message.Channel, message.Message[7:len(message.Message)], twitchClient) commands.Echo(target, message.Message[7:len(message.Message)], nb)
return return
case "8ball": case "8ball":
commands.EightBall(message.Channel, twitchClient) commands.EightBall(target, nb)
return return
case "ffzemotes": case "ffzemotes":
commands.FfzEmotes(message.Channel, twitchClient) commands.FfzEmotes(target, nb)
return return
case "fill": case "fill":
if msgLen == 1 { if msgLen == 1 {
twitchClient.Say(message.Channel, "Usage: ()fill [emote]") nb.Send(target, "Usage: ()fill [emote]")
return return
} else { } else {
commands.Fill(message.Channel, message.Message[7:len(message.Message)], twitchClient) commands.Fill(target, message.Message[7:len(message.Message)], nb)
return return
} }
case "firstline": case "firstline":
if msgLen == 1 { if msgLen == 1 {
twitchClient.Say(message.Channel, "Usage: ()firstline [channel] [user]") nb.Send(target, "Usage: ()firstline [channel] [user]")
return return
} else if msgLen == 2 { } else if msgLen == 2 {
commands.Firstline(message.Channel, message.Channel, cmdParams[1], twitchClient) commands.Firstline(target, target, cmdParams[1], nb)
return return
} else { } else {
commands.Firstline(message.Channel, cmdParams[1], cmdParams[2], twitchClient) commands.Firstline(target, cmdParams[1], cmdParams[2], nb)
return return
} }
case "fl": case "fl":
if msgLen == 1 { if msgLen == 1 {
twitchClient.Say(message.Channel, "Usage: ()firstline [channel] [user]") nb.Send(target, "Usage: ()firstline [channel] [user]")
return return
} else if msgLen == 2 { } else if msgLen == 2 {
commands.Firstline(message.Channel, message.Channel, cmdParams[1], twitchClient) commands.Firstline(target, target, cmdParams[1], nb)
return return
} else { } else {
commands.Firstline(message.Channel, cmdParams[1], cmdParams[2], twitchClient) commands.Firstline(target, cmdParams[1], cmdParams[2], nb)
return return
} }
case "followage": case "followage":
if msgLen <= 2 { if msgLen <= 2 {
twitchClient.Say(message.Channel, "Usage: ()followage [channel] [user]") nb.Send(target, "Usage: ()followage [channel] [user]")
return return
} else { } else {
commands.Followage(message.Channel, cmdParams[1], cmdParams[2], twitchClient) commands.Followage(target, cmdParams[1], cmdParams[2], nb)
return return
} }
case "game": case "game":
if msgLen == 1 { if msgLen == 1 {
twitchClient.Say(message.Channel, "Usage: ()game [channel]") nb.Send(target, "Usage: ()game [channel]")
return return
} else { } else {
commands.Game(message.Channel, cmdParams[1], twitchClient) commands.Game(target, cmdParams[1], nb)
} }
case "godoc": case "godoc":
if msgLen == 1 { if msgLen == 1 {
twitchClient.Say(message.Channel, "Usage: ()godoc [term]") nb.Send(target, "Usage: ()godoc [term]")
return return
} else { } else {
commands.Godocs(message.Channel, message.Message[8:len(message.Message)], twitchClient) commands.Godocs(target, message.Message[8:len(message.Message)], nb)
return return
} }
case "godocs": case "godocs":
if msgLen == 1 { if msgLen == 1 {
twitchClient.Say(message.Channel, "Usage: ()godoc [term]") nb.Send(target, "Usage: ()godoc [term]")
return return
} else { } else {
commands.Godocs(message.Channel, message.Message[9:len(message.Message)], twitchClient) commands.Godocs(target, message.Message[9:len(message.Message)], nb)
return return
} }
case "num": case "num":
if msgLen == 1 { if msgLen == 1 {
commands.RandomNumber(message.Channel, twitchClient) commands.RandomNumber(target, nb)
} else { } else {
commands.Number(message.Channel, cmdParams[1], twitchClient) commands.Number(target, cmdParams[1], nb)
} }
case "number": case "number":
if msgLen == 1 { if msgLen == 1 {
commands.RandomNumber(message.Channel, twitchClient) commands.RandomNumber(target, nb)
} else { } else {
commands.Number(message.Channel, cmdParams[1], twitchClient) commands.Number(target, cmdParams[1], nb)
} }
case "ping": case "ping":
commands.Ping(message.Channel, twitchClient, uptime) commands.Ping(target, nb)
return return
case "pingme": case "pingme":
commands.Pingme(message.Channel, message.User.DisplayName, twitchClient) commands.Pingme(target, message.User.DisplayName, nb)
return return
case "preview": case "preview":
if msgLen == 1 { if msgLen == 1 {
twitchClient.Say(message.Channel, "Usage: ()preview [channel]") nb.Send(target, "Usage: ()preview [channel]")
return return
} else { } else {
commands.Thumbnail(message.Channel, cmdParams[1], twitchClient) commands.Thumbnail(target, cmdParams[1], nb)
return return
} }
case "profilepicture": case "profilepicture":
if msgLen == 1 { if msgLen == 1 {
twitchClient.Say(message.Channel, "Usage: ()profilepicture [user]") nb.Send(target, "Usage: ()profilepicture [user]")
return return
} }
commands.ProfilePicture(message.Channel, cmdParams[1], twitchClient) commands.ProfilePicture(target, cmdParams[1], nb)
return return
case "pfp": case "pfp":
if msgLen == 1 { if msgLen == 1 {
twitchClient.Say(message.Channel, "Usage: ()pfp [user]") nb.Send(target, "Usage: ()pfp [user]")
return return
} }
commands.ProfilePicture(message.Channel, cmdParams[1], twitchClient) commands.ProfilePicture(target, cmdParams[1], nb)
return return
case "pyramid": case "pyramid":
if msgLen != 3 { if msgLen != 3 {
twitchClient.Say(message.Channel, "Usage: ()pyramid [size] [emote]") nb.Send(target, "Usage: ()pyramid [size] [emote]")
} else if utils.ElevatedPrivsMessage(message) { } else if utils.ElevatedPrivsMessage(message) {
commands.Pyramid(message.Channel, cmdParams[1], cmdParams[2], twitchClient) commands.Pyramid(target, cmdParams[1], cmdParams[2], nb)
} else { } else {
twitchClient.Say(message.Channel, "Pleb's can't pyramid FeelsBadMan") nb.Send(target, "Pleb's can't pyramid FeelsBadMan")
} }
case "randomcat": case "randomcat":
commands.RandomCat(message.Channel, twitchClient) commands.RandomCat(target, nb)
return return
case "randomdog": case "randomdog":
commands.RandomDog(message.Channel, twitchClient) commands.RandomDog(target, nb)
return return
case "randomfox": case "randomfox":
commands.RandomFox(message.Channel, twitchClient) commands.RandomFox(target, nb)
return return
case "randomxkcd": case "randomxkcd":
commands.RandomXkcd(message.Channel, twitchClient) commands.RandomXkcd(target, nb)
return return
case "subage": case "subage":
if msgLen < 3 { if msgLen < 3 {
twitchClient.Say(message.Channel, "Usage: ()subage [user] [streamer]") nb.Send(target, "Usage: ()subage [user] [streamer]")
return return
} else { } else {
commands.Subage(message.Channel, cmdParams[1], cmdParams[2], twitchClient) commands.Subage(target, cmdParams[1], cmdParams[2], nb)
return return
} }
case "thumb": case "thumb":
if msgLen == 1 { if msgLen == 1 {
twitchClient.Say(message.Channel, "Usage: ()thumbnail [channel]") nb.Send(target, "Usage: ()thumbnail [channel]")
return return
} else { } else {
commands.Thumbnail(message.Channel, cmdParams[1], twitchClient) commands.Thumbnail(target, cmdParams[1], nb)
return return
} }
case "thumbnail": case "thumbnail":
if msgLen == 1 { if msgLen == 1 {
twitchClient.Say(message.Channel, "Usage: ()thumbnail [channel]") nb.Send(target, "Usage: ()thumbnail [channel]")
return return
} else { } else {
commands.Thumbnail(message.Channel, cmdParams[1], twitchClient) commands.Thumbnail(target, cmdParams[1], nb)
return return
} }
case "title": case "title":
if msgLen == 1 { if msgLen == 1 {
commands.Title(message.Channel, message.Channel, twitchClient) commands.Title(target, target, nb)
return return
} else { } else {
commands.Title(message.Channel, cmdParams[1], twitchClient) commands.Title(target, cmdParams[1], nb)
return return
} }
case "uptime": case "uptime":
if msgLen == 1 { if msgLen == 1 {
commands.Uptime(message.Channel, message.Channel, twitchClient) commands.Uptime(target, target, nb)
return return
} else { } else {
commands.Uptime(message.Channel, cmdParams[1], twitchClient) commands.Uptime(target, cmdParams[1], nb)
return return
} }
case "uid": case "uid":
if msgLen == 1 { if msgLen == 1 {
twitchClient.Say(message.Channel, "Usage: ()uid [username]") nb.Send(target, "Usage: ()uid [username]")
return return
} else { } else {
commands.Userid(message.Channel, cmdParams[1], twitchClient) commands.Userid(target, cmdParams[1], nb)
return return
} }
case "userid": case "userid":
if msgLen == 1 { if msgLen == 1 {
twitchClient.Say(message.Channel, "Usage: ()userid [username]") nb.Send(target, "Usage: ()userid [username]")
return return
} else { } else {
commands.Userid(message.Channel, cmdParams[1], twitchClient) commands.Userid(target, cmdParams[1], nb)
return return
} }
case "weather": case "weather":
if msgLen == 1 { if msgLen == 1 {
twitchClient.Say(message.Channel, "Usage: ()weather [location]") nb.Send(target, "Usage: ()weather [location]")
return return
} else { } else {
commands.Weather(message.Channel, message.Message[9:len(message.Message)], twitchClient) commands.Weather(target, message.Message[9:len(message.Message)], nb)
return return
} }
case "xd": case "xd":
commands.Xd(message.Channel, twitchClient) commands.Xd(target, nb)
return
case "xkcd": case "xkcd":
commands.Xkcd(message.Channel, twitchClient) commands.Xkcd(target, nb)
return return
} }
} }

View file

@ -1,10 +1,8 @@
package handlers package handlers
import ( import (
"time"
"github.com/gempir/go-twitch-irc/v2" "github.com/gempir/go-twitch-irc/v2"
"github.com/lyx0/nourybot/pkg/config" "github.com/lyx0/nourybot/cmd/bot"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@ -12,8 +10,8 @@ import (
// *twitch.Client and *config.Config and has the logic to decide if the provided // *twitch.Client and *config.Config and has the logic to decide if the provided
// PrivateMessage is a command or not and passes it on accordingly. // PrivateMessage is a command or not and passes it on accordingly.
// Typical twitch message tags https://paste.ivr.fi/nopiradodo.lua // Typical twitch message tags https://paste.ivr.fi/nopiradodo.lua
func HandlePrivateMessage(message twitch.PrivateMessage, client *twitch.Client, cfg *config.Config, uptime time.Time) { func PrivateMessage(message twitch.PrivateMessage, nb *bot.Bot) {
log.Info("fn HandlePrivateMessage") log.Info("fn PrivateMessage")
// log.Info(message) // log.Info(message)
// roomId is the Twitch UserID of the channel the message // roomId is the Twitch UserID of the channel the message
@ -26,13 +24,6 @@ func HandlePrivateMessage(message twitch.PrivateMessage, client *twitch.Client,
return return
} }
// Message was sent from the Bot. Don't act on it
// so that we don't repeat ourself.
botUserId := cfg.BotUserId
if message.Tags["user-id"] == botUserId {
return
}
// Since our command prefix is () ignore every message // Since our command prefix is () ignore every message
// that is less than 2 // that is less than 2
if len(message.Message) >= 2 { if len(message.Message) >= 2 {
@ -40,7 +31,7 @@ func HandlePrivateMessage(message twitch.PrivateMessage, client *twitch.Client,
// Message starts with (), pass it on to // Message starts with (), pass it on to
// the command handler. // the command handler.
if message.Message[:2] == "()" { if message.Message[:2] == "()" {
HandleCommand(message, client, uptime) Command(message, nb)
return return
} }
} }

View file

@ -1,14 +0,0 @@
package handlers
import (
"github.com/gempir/go-twitch-irc/v2"
log "github.com/sirupsen/logrus"
)
func HandleWhisperMessage(whisper twitch.WhisperMessage, client *twitch.Client) {
log.Info("fn HandleWhisperMessage")
log.Info(whisper)
if whisper.Message == "xd" {
client.Whisper(whisper.User.Name, "xd")
}
}