diff --git a/cmd/bot/bot.go b/cmd/bot/bot.go index 5845575..1480910 100644 --- a/cmd/bot/bot.go +++ b/cmd/bot/bot.go @@ -4,63 +4,33 @@ import ( "time" 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 { - twitchClient *twitch.Client - cfg *cfg.Config + TwitchClient *twitch.Client Uptime time.Time } -func NewBot(cfg *cfg.Config) *Bot { - - log.Info("fn Newbot") - twitchClient := twitch.NewClient(cfg.Username, cfg.Oauth) - - return &Bot{ - cfg: cfg, - twitchClient: twitchClient, - } +type Channel struct { + Name string } -func (b *Bot) Connect() error { - log.Info("fn Connect") - - 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) +func (b *Bot) Send(target, text string) { + if len(text) == 0 { + return } - return err -} + // if message[0] == '.' || message[0] == '/' { + // message = ". " + message + // } -func (b *Bot) Disconnect() error { - err := b.twitchClient.Disconnect() - if err != nil { - log.Error("Error Disconnecting from Twitch: ", err) + if len(text) > 500 { + firstMessage := text[0:499] + secondMessage := text[499:] + b.TwitchClient.Say(target, firstMessage) + b.TwitchClient.Say(target, secondMessage) + return } - return err -} - -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) + b.TwitchClient.Say(target, text) } diff --git a/cmd/main.go b/cmd/main.go index 990c7b8..88376ec 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,17 +1,38 @@ package main import ( + "fmt" + "time" + + "github.com/gempir/go-twitch-irc/v2" "github.com/lyx0/nourybot/cmd/bot" "github.com/lyx0/nourybot/pkg/config" + "github.com/lyx0/nourybot/pkg/handlers" ) +var nb *bot.Bot + func main() { - cfg := config.LoadConfig() - nb := bot.NewBot(cfg) + conf := config.LoadConfig() - 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) + }) } diff --git a/pkg/api/aiden/aiden.go b/pkg/api/aiden/aiden.go deleted file mode 100644 index 497f46e..0000000 --- a/pkg/api/aiden/aiden.go +++ /dev/null @@ -1,30 +0,0 @@ -package aiden - -import ( - "fmt" - "io/ioutil" - "net/http" - - log "github.com/sirupsen/logrus" -) - -var ( - basePath = "https://customapi.aidenwallis.co.uk/" -) - -func ApiCall(uri string) (string, error) { - resp, err := http.Get(fmt.Sprint(basePath + uri)) - if err != nil { - log.Error(err) - return "Something went wrong FeelsBadMan", err - } - - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Error(err) - return "Something went wrong FeelsBadMan", err - } - return string(body), nil -} diff --git a/pkg/api/ivr/firstline.go b/pkg/api/ivr/firstline.go deleted file mode 100644 index 3f2ae15..0000000 --- a/pkg/api/ivr/firstline.go +++ /dev/null @@ -1,47 +0,0 @@ -package ivr - -import ( - "encoding/json" - "fmt" - "io/ioutil" - "net/http" - - log "github.com/sirupsen/logrus" -) - -type firstLineApiResponse struct { - User string `json:"user"` - Message string `json:"message"` - Time string `json:"time"` - Error string `json:"error"` -} - -var ( - firstLineBaseUrl = "https://api.ivr.fi/logs/firstmessage" -) - -func FirstLine(streamer string, username string) (string, error) { - resp, err := http.Get(fmt.Sprintf("%s/%s/%s", firstLineBaseUrl, streamer, username)) - if err != nil { - log.Error(err) - return "Something went wrong FeelsBadMan", err - } - - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Error(err) - } - - var responseObject firstLineApiResponse - json.Unmarshal(body, &responseObject) - - // User or channel was not found - if responseObject.Error != "" { - return fmt.Sprintf(responseObject.Error + " FeelsBadMan"), nil - } else { - return fmt.Sprintf(username + ": " + responseObject.Message + " (" + responseObject.Time + " ago)."), nil - } - -} diff --git a/pkg/api/ivr/followage.go b/pkg/api/ivr/followage.go deleted file mode 100644 index 2ab1ea9..0000000 --- a/pkg/api/ivr/followage.go +++ /dev/null @@ -1,47 +0,0 @@ -package ivr - -import ( - "encoding/json" - "fmt" - "io/ioutil" - "net/http" - - log "github.com/sirupsen/logrus" -) - -// https://api.ivr.fi -type followageApiResponse struct { - User string `json:"user"` - UserID string `json:"userid"` - Channel string `json:"channel"` - ChannelId string `json:"channelid"` - FollowedAt string `json:"followedAt"` - Error string `json:"error"` -} - -func Followage(streamer string, username string) (string, error) { - resp, err := http.Get(fmt.Sprintf("https://api.ivr.fi/twitch/subage/%s/%s", username, streamer)) - if err != nil { - log.Error(err) - } - - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Error(err) - } - - var responseObject followageApiResponse - json.Unmarshal(body, &responseObject) - - // User or channel was not found - if responseObject.Error != "" { - return fmt.Sprintf(responseObject.Error + " FeelsBadMan"), nil - } else if responseObject.FollowedAt == "" { - return fmt.Sprintf(username + " is not following " + streamer), nil - } else { - d := responseObject.FollowedAt[:10] - return fmt.Sprintf(username + " has been following " + streamer + " since " + d + "."), nil - } -} diff --git a/pkg/api/ivr/profilepicture.go b/pkg/api/ivr/profilepicture.go deleted file mode 100644 index 5494996..0000000 --- a/pkg/api/ivr/profilepicture.go +++ /dev/null @@ -1,44 +0,0 @@ -package ivr - -import ( - "encoding/json" - "fmt" - "io/ioutil" - "net/http" - - log "github.com/sirupsen/logrus" -) - -// https://api.ivr.fi -type pfpApiResponse struct { - Logo string `json:"logo"` - Error string `json:"error"` -} - -var ( - baseUrl = "https://api.ivr.fi/twitch/resolve" -) - -func ProfilePicture(username string) (string, error) { - resp, err := http.Get(fmt.Sprintf("%s/%s", baseUrl, username)) - if err != nil { - log.Error(err) - } - - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Error(err) - } - - var responseObject pfpApiResponse - json.Unmarshal(body, &responseObject) - - // User not found - if responseObject.Error != "" { - return fmt.Sprintf(responseObject.Error + " FeelsBadMan"), nil - } else { - return fmt.Sprintf(responseObject.Logo), nil - } -} diff --git a/pkg/api/ivr/subage.go b/pkg/api/ivr/subage.go deleted file mode 100644 index 1c03781..0000000 --- a/pkg/api/ivr/subage.go +++ /dev/null @@ -1,70 +0,0 @@ -package ivr - -import ( - "encoding/json" - "fmt" - "io/ioutil" - "net/http" - - log "github.com/sirupsen/logrus" -) - -type subageApiResponse struct { - User string `json:"user"` - UserID string `json:"userId"` - Channel string `json:"channel"` - ChannelId string `json:"channelid"` - SubageHidden bool `json:"hidden"` - Subscribed bool `json:"subscribed"` - FollowedAt string `json:"followedAt"` - Cumulative cumulative `json:"cumulative"` - Streak subStreak `json:"streak"` - Error string `json:"error"` -} - -type cumulative struct { - Months int `json:"months"` -} - -type subStreak struct { - Months int `json:"months"` -} - -var ( - subageBaseUrl = "https://api.ivr.fi/twitch/subage" -) - -func Subage(username string, streamer string) (string, error) { - resp, err := http.Get(fmt.Sprintf("%s/%s/%s", subageBaseUrl, username, streamer)) - if err != nil { - log.Error(err) - return "Something went wrong FeelsBadMan", err - } - - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Error(err) - } - - var responseObject subageApiResponse - json.Unmarshal(body, &responseObject) - - // User or channel was not found - if responseObject.Error != "" { - reply := fmt.Sprintf(responseObject.Error + " FeelsBadMan") - // client.Say(channel, fmt.Sprintf(responseObject.Error+" FeelsBadMan")) - return reply, nil - } - - if responseObject.SubageHidden { - - reply := fmt.Sprintf(username + " has their subscription status hidden. FeelsBadMan") - return reply, nil - } else { - months := fmt.Sprint(responseObject.Cumulative.Months) - reply := fmt.Sprintf(username + " has been subscribed to " + streamer + " for " + months + " months.") - return reply, nil - } -} diff --git a/pkg/api/ivr/userid.go b/pkg/api/ivr/userid.go deleted file mode 100644 index 80c8e7c..0000000 --- a/pkg/api/ivr/userid.go +++ /dev/null @@ -1,40 +0,0 @@ -package ivr - -import ( - "encoding/json" - "fmt" - "io/ioutil" - "net/http" - - log "github.com/sirupsen/logrus" -) - -// https://api.ivr.fi -type uidApiResponse struct { - Id string `json:"id"` - Error string `json:"error"` -} - -func Userid(username string) string { - resp, err := http.Get(fmt.Sprintf("https://api.ivr.fi/twitch/resolve/%s", username)) - if err != nil { - log.Error(err) - } - - defer resp.Body.Close() - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - log.Error(err) - } - - var responseObject uidApiResponse - json.Unmarshal(body, &responseObject) - - // User not found - if responseObject.Error != "" { - return fmt.Sprintf(responseObject.Error + " FeelsBadMan") - } else { - return fmt.Sprintf(responseObject.Id) - } -} diff --git a/pkg/api/numbersapi.go b/pkg/api/numbersapi.go deleted file mode 100644 index e1d4799..0000000 --- a/pkg/api/numbersapi.go +++ /dev/null @@ -1,34 +0,0 @@ -package api - -import ( - "fmt" - "io/ioutil" - "net/http" - - log "github.com/sirupsen/logrus" -) - -func RandomNumber() string { - response, err := http.Get("http://numbersapi.com/random/trivia") - if err != nil { - log.Error(err) - } - responseData, err := ioutil.ReadAll(response.Body) - if err != nil { - log.Error(err) - } - - return string(responseData) -} - -func Number(number string) string { - response, err := http.Get(fmt.Sprint("http://numbersapi.com/" + string(number))) - if err != nil { - log.Error(err) - } - responseData, err := ioutil.ReadAll(response.Body) - if err != nil { - log.Error(err) - } - return string(responseData) -} diff --git a/pkg/api/randomcat.go b/pkg/api/randomcat.go deleted file mode 100644 index efede29..0000000 --- a/pkg/api/randomcat.go +++ /dev/null @@ -1,30 +0,0 @@ -package api - -import ( - "encoding/json" - "io/ioutil" - "net/http" - - log "github.com/sirupsen/logrus" -) - -type randomCatResponse struct { - File string `json:"file"` -} - -func RandomCat() string { - response, err := http.Get("https://aws.random.cat/meow") - if err != nil { - log.Error(err) - } - - responseData, err := ioutil.ReadAll(response.Body) - if err != nil { - log.Error(err) - } - - var responseObject randomCatResponse - json.Unmarshal(responseData, &responseObject) - - return string(responseObject.File) -} diff --git a/pkg/api/randomdog.go b/pkg/api/randomdog.go deleted file mode 100644 index 7b17af5..0000000 --- a/pkg/api/randomdog.go +++ /dev/null @@ -1,30 +0,0 @@ -package api - -import ( - "encoding/json" - "io/ioutil" - "net/http" - - log "github.com/sirupsen/logrus" -) - -type randomDogResponse struct { - Url string `json:"url"` -} - -func RandomDog() string { - response, err := http.Get("https://random.dog/woof.json") - if err != nil { - log.Error(err) - } - - responseData, err := ioutil.ReadAll(response.Body) - if err != nil { - log.Error(err) - } - - var responseObject randomDogResponse - json.Unmarshal(responseData, &responseObject) - - return string(responseObject.Url) -} diff --git a/pkg/api/randomfox.go b/pkg/api/randomfox.go deleted file mode 100644 index 6a4bfc5..0000000 --- a/pkg/api/randomfox.go +++ /dev/null @@ -1,31 +0,0 @@ -package api - -import ( - "encoding/json" - "io/ioutil" - "net/http" - - log "github.com/sirupsen/logrus" -) - -type randomFoxResponse struct { - Image string `json:"image"` - Link string `json:"link"` -} - -func RandomFox() string { - response, err := http.Get("https://randomfox.ca/floof/") - if err != nil { - log.Error(err) - } - - responseData, err := ioutil.ReadAll(response.Body) - if err != nil { - log.Error(err) - } - - var responseObject randomFoxResponse - json.Unmarshal(responseData, &responseObject) - - return string(responseObject.Image) -} diff --git a/pkg/api/randomxkcd.go b/pkg/api/randomxkcd.go deleted file mode 100644 index 0384758..0000000 --- a/pkg/api/randomxkcd.go +++ /dev/null @@ -1,35 +0,0 @@ -package api - -import ( - "encoding/json" - "fmt" - "io/ioutil" - "net/http" - - "github.com/lyx0/nourybot/pkg/utils" - log "github.com/sirupsen/logrus" -) - -type XkcdResponse struct { - Num int `json:"num"` - SafeTitle string `json:"safe_title"` - Img string `json:"img"` -} - -func RandomXkcd() string { - comicNum := fmt.Sprint(utils.GenerateRandomNumber(2468)) - response, err := http.Get(fmt.Sprint("http://xkcd.com/" + comicNum + "/info.0.json")) - if err != nil { - log.Error(err) - } - responseData, err := ioutil.ReadAll(response.Body) - if err != nil { - log.Error(err) - } - var responseObject XkcdResponse - json.Unmarshal(responseData, &responseObject) - - reply := fmt.Sprint("Random Xkcd #", responseObject.Num, " Title: ", responseObject.SafeTitle, " ", responseObject.Img) - - return reply -} diff --git a/pkg/api/xkcd.go b/pkg/api/xkcd.go deleted file mode 100644 index 6a3776d..0000000 --- a/pkg/api/xkcd.go +++ /dev/null @@ -1,27 +0,0 @@ -package api - -import ( - "encoding/json" - "fmt" - "io/ioutil" - "net/http" - - log "github.com/sirupsen/logrus" -) - -func Xkcd() string { - response, err := http.Get("https://xkcd.com/info.0.json") - if err != nil { - log.Error(err) - } - responseData, err := ioutil.ReadAll(response.Body) - if err != nil { - log.Error(err) - } - var responseObject XkcdResponse - json.Unmarshal(responseData, &responseObject) - - reply := fmt.Sprint("Current Xkcd #", responseObject.Num, " Title: ", responseObject.SafeTitle, " ", responseObject.Img) - - return reply -} diff --git a/pkg/commands/botstatus.go b/pkg/commands/botstatus.go deleted file mode 100644 index 1632da4..0000000 --- a/pkg/commands/botstatus.go +++ /dev/null @@ -1,20 +0,0 @@ -package commands - -import ( - "fmt" - - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/api/aiden" - log "github.com/sirupsen/logrus" -) - -func BotStatus(channel string, name string, client *twitch.Client) { - resp, err := aiden.ApiCall(fmt.Sprintf("api/v1/twitch/botStatus/%s?includeLimits=1", name)) - if err != nil { - client.Say(channel, "Something went wrong FeelsBadMan") - log.Error(err) - } - - client.Say(channel, resp) - -} diff --git a/pkg/commands/bttvemotes.go b/pkg/commands/bttvemotes.go deleted file mode 100644 index db389c8..0000000 --- a/pkg/commands/bttvemotes.go +++ /dev/null @@ -1,19 +0,0 @@ -package commands - -import ( - "fmt" - - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/api/aiden" - log "github.com/sirupsen/logrus" -) - -func BttvEmotes(channel string, client *twitch.Client) { - resp, err := aiden.ApiCall(fmt.Sprintf("api/v1/emotes/%s/bttv", channel)) - if err != nil { - log.Error(err) - client.Say(channel, "Something went wrong FeelsBadMan") - } - - client.Say(channel, string(resp)) -} diff --git a/pkg/commands/coinflip.go b/pkg/commands/coinflip.go deleted file mode 100644 index c713041..0000000 --- a/pkg/commands/coinflip.go +++ /dev/null @@ -1,16 +0,0 @@ -package commands - -import ( - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/utils" -) - -func Coinflip(channel string, client *twitch.Client) { - result := utils.GenerateRandomNumber(2) - - if result == 1 { - client.Say(channel, "Heads") - } else { - client.Say(channel, "Tails") - } -} diff --git a/pkg/commands/color.go b/pkg/commands/color.go deleted file mode 100644 index 1740603..0000000 --- a/pkg/commands/color.go +++ /dev/null @@ -1,14 +0,0 @@ -package commands - -import ( - "fmt" - - "github.com/gempir/go-twitch-irc/v2" -) - -func Color(message twitch.PrivateMessage, client *twitch.Client) { - reply := fmt.Sprintf("@%v, your color is %v", message.User.DisplayName, message.User.Color) - - client.Say(message.Channel, reply) - -} diff --git a/pkg/commands/echo.go b/pkg/commands/echo.go deleted file mode 100644 index cb615bb..0000000 --- a/pkg/commands/echo.go +++ /dev/null @@ -1,7 +0,0 @@ -package commands - -import "github.com/gempir/go-twitch-irc/v2" - -func Echo(channel string, message string, client *twitch.Client) { - client.Say(channel, message) -} diff --git a/pkg/commands/eightball.go b/pkg/commands/eightball.go deleted file mode 100644 index 47aa4ea..0000000 --- a/pkg/commands/eightball.go +++ /dev/null @@ -1,17 +0,0 @@ -package commands - -import ( - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/api/aiden" - log "github.com/sirupsen/logrus" -) - -func EightBall(channel string, client *twitch.Client) { - resp, err := aiden.ApiCall("api/v1/misc/8ball") - if err != nil { - log.Error(err) - client.Say(channel, "Something went wrong FeelsBadMan") - } - - client.Say(channel, string(resp)) -} diff --git a/pkg/commands/ffzemotes.go b/pkg/commands/ffzemotes.go deleted file mode 100644 index 7ab9fdd..0000000 --- a/pkg/commands/ffzemotes.go +++ /dev/null @@ -1,20 +0,0 @@ -package commands - -import ( - "fmt" - - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/api/aiden" - log "github.com/sirupsen/logrus" -) - -func FfzEmotes(channel string, client *twitch.Client) { - - resp, err := aiden.ApiCall(fmt.Sprintf("api/v1/emotes/%s/ffz", channel)) - if err != nil { - log.Error(err) - client.Say(channel, "Something went wrong FeelsBadMan") - } - - client.Say(channel, string(resp)) -} diff --git a/pkg/commands/fill.go b/pkg/commands/fill.go deleted file mode 100644 index c5f6379..0000000 --- a/pkg/commands/fill.go +++ /dev/null @@ -1,25 +0,0 @@ -package commands - -import ( - "fmt" - "strings" - - "github.com/gempir/go-twitch-irc/v2" -) - -func Fill(channel string, emote string, client *twitch.Client) { - if emote[0] == '.' || emote[0] == '/' { - client.Say(channel, ":tf:") - return - } - - // Get the length of the emote - emoteLength := (len(emote) + 1) - - // Check how often the emote fits into a single message - repeatCount := (499 / emoteLength) - - reply := strings.Repeat(fmt.Sprintf(emote+" "), repeatCount) - client.Say(channel, reply) - -} diff --git a/pkg/commands/firstline.go b/pkg/commands/firstline.go deleted file mode 100644 index f387e24..0000000 --- a/pkg/commands/firstline.go +++ /dev/null @@ -1,17 +0,0 @@ -package commands - -import ( - "fmt" - - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/api/ivr" -) - -func Firstline(channel string, streamer string, username string, client *twitch.Client) { - ivrResponse, err := ivr.FirstLine(streamer, username) - if err != nil { - client.Say(channel, fmt.Sprint(err)) - return - } - client.Say(channel, ivrResponse) -} diff --git a/pkg/commands/followage.go b/pkg/commands/followage.go deleted file mode 100644 index 0825050..0000000 --- a/pkg/commands/followage.go +++ /dev/null @@ -1,16 +0,0 @@ -package commands - -import ( - "fmt" - - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/api/ivr" -) - -func Followage(channel string, streamer string, username string, client *twitch.Client) { - ivrResponse, err := ivr.Followage(streamer, username) - if err != nil { - client.Say(channel, fmt.Sprint(err)) - } - client.Say(channel, ivrResponse) -} diff --git a/pkg/commands/game.go b/pkg/commands/game.go deleted file mode 100644 index 797e11c..0000000 --- a/pkg/commands/game.go +++ /dev/null @@ -1,20 +0,0 @@ -package commands - -import ( - "fmt" - - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/api/aiden" - log "github.com/sirupsen/logrus" -) - -func Game(channel string, name string, client *twitch.Client) { - - game, err := aiden.ApiCall(fmt.Sprintf("api/v1/twitch/channel/%s/game", name)) - if err != nil { - client.Say(channel, "Something went wrong FeelsBadMan") - log.Error(err) - } - reply := fmt.Sprintf("@%s was last seen playing: %s", name, game) - client.Say(channel, reply) -} diff --git a/pkg/commands/godocs.go b/pkg/commands/godocs.go deleted file mode 100644 index 65da7e2..0000000 --- a/pkg/commands/godocs.go +++ /dev/null @@ -1,13 +0,0 @@ -package commands - -import ( - "fmt" - - "github.com/gempir/go-twitch-irc/v2" -) - -func Godocs(channel string, searchTerm string, client *twitch.Client) { - resp := fmt.Sprintf("https://godocs.io/?q=%s", searchTerm) - - client.Say(channel, resp) -} diff --git a/pkg/commands/number.go b/pkg/commands/number.go deleted file mode 100644 index 11942b9..0000000 --- a/pkg/commands/number.go +++ /dev/null @@ -1,16 +0,0 @@ -package commands - -import ( - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/api" -) - -func RandomNumber(channel string, client *twitch.Client) { - reply := api.RandomNumber() - client.Say(channel, string(reply)) -} - -func Number(channel string, number string, client *twitch.Client) { - reply := api.Number(number) - client.Say(channel, string(reply)) -} diff --git a/pkg/commands/ping.go b/pkg/commands/ping.go deleted file mode 100644 index 78a0ced..0000000 --- a/pkg/commands/ping.go +++ /dev/null @@ -1,18 +0,0 @@ -package commands - -import ( - "fmt" - "time" - - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/humanize" - "github.com/lyx0/nourybot/pkg/utils" -) - -func Ping(channel string, client *twitch.Client, uptime time.Time) { - commandCount := fmt.Sprint(utils.GetCommandsUsed()) - botUptime := humanize.Time(uptime) - - reply := fmt.Sprintf("Pong! :) Commands used: %v, Last restart: %v", commandCount, botUptime) - client.Say(channel, reply) -} diff --git a/pkg/commands/pingme.go b/pkg/commands/pingme.go deleted file mode 100644 index 26cc98c..0000000 --- a/pkg/commands/pingme.go +++ /dev/null @@ -1,14 +0,0 @@ -package commands - -import ( - "fmt" - - "github.com/gempir/go-twitch-irc/v2" -) - -func Pingme(channel string, user string, client *twitch.Client) { - response := fmt.Sprintf("@%s", user) - - client.Say(channel, response) - -} diff --git a/pkg/commands/profilepicture.go b/pkg/commands/profilepicture.go deleted file mode 100644 index e52f412..0000000 --- a/pkg/commands/profilepicture.go +++ /dev/null @@ -1,18 +0,0 @@ -package commands - -import ( - "fmt" - - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/api/ivr" -) - -func ProfilePicture(channel string, target string, client *twitch.Client) { - reply, err := ivr.ProfilePicture(target) - if err != nil { - client.Say(channel, fmt.Sprint(err)) - return - } - - client.Say(channel, reply) -} diff --git a/pkg/commands/pyramid.go b/pkg/commands/pyramid.go deleted file mode 100644 index aa2e7e7..0000000 --- a/pkg/commands/pyramid.go +++ /dev/null @@ -1,49 +0,0 @@ -package commands - -import ( - "fmt" - "strconv" - "strings" - - "github.com/gempir/go-twitch-irc/v2" -) - -func Pyramid(channel string, size string, emote string, client *twitch.Client) { - if size[0] == '.' || size[0] == '/' { - client.Say(channel, ":tf:") - return - } - - if emote[0] == '.' || emote[0] == '/' { - client.Say(channel, ":tf:") - return - } - - pyramidSize, err := strconv.Atoi(size) - pyramidEmote := fmt.Sprint(emote + " ") - - if err != nil { - client.Say(channel, "Something went wrong") - } - - if pyramidSize == 1 { - client.Say(channel, fmt.Sprint(pyramidEmote+"...")) - return - } - - if pyramidSize > 20 { - client.Say(channel, "Max pyramid size is 20") - return - } - - for i := 0; i <= pyramidSize; i++ { - pyramidMessageAsc := strings.Repeat(pyramidEmote, i) - // fmt.Println(pyramidMessageAsc) - client.Say(channel, pyramidMessageAsc) - } - for j := pyramidSize - 1; j >= 0; j-- { - pyramidMessageDesc := strings.Repeat(pyramidEmote, j) - // fmt.Println(pyramidMessageDesc) - client.Say(channel, pyramidMessageDesc) - } -} diff --git a/pkg/commands/randomcat.go b/pkg/commands/randomcat.go deleted file mode 100644 index 2987d56..0000000 --- a/pkg/commands/randomcat.go +++ /dev/null @@ -1,12 +0,0 @@ -package commands - -import ( - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/api" -) - -func RandomCat(channel string, client *twitch.Client) { - reply := api.RandomCat() - - client.Say(channel, reply) -} diff --git a/pkg/commands/randomdog.go b/pkg/commands/randomdog.go deleted file mode 100644 index ece7677..0000000 --- a/pkg/commands/randomdog.go +++ /dev/null @@ -1,12 +0,0 @@ -package commands - -import ( - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/api" -) - -func RandomDog(channel string, client *twitch.Client) { - reply := api.RandomDog() - - client.Say(channel, reply) -} diff --git a/pkg/commands/randomfox.go b/pkg/commands/randomfox.go deleted file mode 100644 index 97c7e5e..0000000 --- a/pkg/commands/randomfox.go +++ /dev/null @@ -1,12 +0,0 @@ -package commands - -import ( - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/api" -) - -func RandomFox(channel string, client *twitch.Client) { - reply := api.RandomFox() - - client.Say(channel, reply) -} diff --git a/pkg/commands/randomxkcd.go b/pkg/commands/randomxkcd.go deleted file mode 100644 index 0dffb1a..0000000 --- a/pkg/commands/randomxkcd.go +++ /dev/null @@ -1,11 +0,0 @@ -package commands - -import ( - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/api" -) - -func RandomXkcd(channel string, client *twitch.Client) { - reply := api.RandomXkcd() - client.Say(channel, reply) -} diff --git a/pkg/commands/subage.go b/pkg/commands/subage.go deleted file mode 100644 index c101818..0000000 --- a/pkg/commands/subage.go +++ /dev/null @@ -1,18 +0,0 @@ -package commands - -import ( - "fmt" - - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/api/ivr" -) - -func Subage(channel string, username string, streamer string, client *twitch.Client) { - - ivrResponse, err := ivr.Subage(username, streamer) - if err != nil { - client.Say(channel, fmt.Sprint(err)) - return - } - client.Say(channel, ivrResponse) -} diff --git a/pkg/commands/thumbnail.go b/pkg/commands/thumbnail.go deleted file mode 100644 index 311257c..0000000 --- a/pkg/commands/thumbnail.go +++ /dev/null @@ -1,16 +0,0 @@ -package commands - -import ( - "fmt" - - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/utils" -) - -func Thumbnail(channel string, target string, client *twitch.Client) { - imageHeight := utils.GenerateRandomNumberRange(1040, 1080) - imageWidth := utils.GenerateRandomNumberRange(1890, 1920) - response := fmt.Sprintf("https://static-cdn.jtvnw.net/previews-ttv/live_user_%v-%vx%v.jpg", target, imageWidth, imageHeight) - - client.Say(channel, response) -} diff --git a/pkg/commands/title.go b/pkg/commands/title.go deleted file mode 100644 index 0a69bea..0000000 --- a/pkg/commands/title.go +++ /dev/null @@ -1,19 +0,0 @@ -package commands - -import ( - "fmt" - - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/api/aiden" - log "github.com/sirupsen/logrus" -) - -func Title(channel string, target string, client *twitch.Client) { - title, err := aiden.ApiCall(fmt.Sprintf("api/v1/twitch/channel/%s/title", target)) - if err != nil { - client.Say(channel, "Something went wrong FeelsBadMan") - log.Error(err) - } - reply := fmt.Sprintf("%s title is: %s", target, title) - client.Say(channel, reply) -} diff --git a/pkg/commands/uptime.go b/pkg/commands/uptime.go deleted file mode 100644 index 12bcbbf..0000000 --- a/pkg/commands/uptime.go +++ /dev/null @@ -1,21 +0,0 @@ -package commands - -import ( - "fmt" - - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/api/aiden" - log "github.com/sirupsen/logrus" -) - -func Uptime(channel string, name string, client *twitch.Client) { - - resp, err := aiden.ApiCall(fmt.Sprintf("api/v1/twitch/channel/%s/uptime", name)) - if err != nil { - client.Say(channel, "Something went wrong FeelsBadMan") - log.Error(err) - } - - client.Say(channel, resp) - -} diff --git a/pkg/commands/userid.go b/pkg/commands/userid.go deleted file mode 100644 index 2cdaf0e..0000000 --- a/pkg/commands/userid.go +++ /dev/null @@ -1,11 +0,0 @@ -package commands - -import ( - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/api/ivr" -) - -func Userid(channel string, target string, client *twitch.Client) { - reply := ivr.Userid(target) - client.Say(channel, reply) -} diff --git a/pkg/commands/weather.go b/pkg/commands/weather.go deleted file mode 100644 index 7dffced..0000000 --- a/pkg/commands/weather.go +++ /dev/null @@ -1,20 +0,0 @@ -package commands - -import ( - "fmt" - - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/api/aiden" - log "github.com/sirupsen/logrus" -) - -func Weather(channel string, location string, client *twitch.Client) { - - reply, err := aiden.ApiCall(fmt.Sprintf("api/v1/misc/weather/%s", location)) - if err != nil { - client.Say(channel, "Something went wrong FeelsBadMan") - log.Error(err) - } - client.Say(channel, reply) - -} diff --git a/pkg/commands/xd.go b/pkg/commands/xd.go deleted file mode 100644 index 1afda65..0000000 --- a/pkg/commands/xd.go +++ /dev/null @@ -1,7 +0,0 @@ -package commands - -import "github.com/gempir/go-twitch-irc/v2" - -func Xd(channel string, client *twitch.Client) { - client.Say(channel, "xd") -} diff --git a/pkg/commands/xkcd.go b/pkg/commands/xkcd.go deleted file mode 100644 index 646dd91..0000000 --- a/pkg/commands/xkcd.go +++ /dev/null @@ -1,12 +0,0 @@ -package commands - -import ( - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/api" -) - -func Xkcd(channel string, client *twitch.Client) { - reply := api.Xkcd() - client.Say(channel, reply) - -} diff --git a/pkg/handlers/command.go b/pkg/handlers/command.go index 9da2edf..5e1dd1b 100644 --- a/pkg/handlers/command.go +++ b/pkg/handlers/command.go @@ -1,271 +1,14 @@ package handlers import ( - "strings" - "time" - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/commands" - "github.com/lyx0/nourybot/pkg/utils" - log "github.com/sirupsen/logrus" + "github.com/lyx0/nourybot/cmd/bot" + "github.com/sirupsen/logrus" ) -// HandleCommand receives a twitch.PrivateMessage from -// HandlePrivateMessage where it found a command in it. -// 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") +func Command(message twitch.PrivateMessage, nb *bot.Bot) { + logrus.Info("fn Command") - // Counter that increments on every command call. - utils.CommandUsed() + nb.Send("nourybot", "xd") - // commandName is the actual command name without the prefix. - commandName := strings.ToLower(strings.SplitN(message.Message, " ", 3)[0][2:]) - - // cmdParams are additional command inputs. - // example: - // weather san antonio - // is - // commandName cmdParams[0] cmdParams[1] - cmdParams := strings.SplitN(message.Message, " ", 500) - - // msgLen is the amount of words in the message without the prefix. - // Useful for checking if enough cmdParams are given. - msgLen := len(strings.SplitN(message.Message, " ", -2)) - - switch commandName { - case "": - if msgLen == 1 { - twitchClient.Say(message.Channel, "Why yes, that's my prefix :)") - return - } - case "botstatus": - if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()botstatus [username]") - return - } else { - commands.BotStatus(message.Channel, cmdParams[1], twitchClient) - return - } - case "bttvemotes": - commands.BttvEmotes(message.Channel, twitchClient) - return - case "cf": - commands.Coinflip(message.Channel, twitchClient) - return - case "coin": - commands.Coinflip(message.Channel, twitchClient) - return - case "coinflip": - commands.Coinflip(message.Channel, twitchClient) - return - case "color": - commands.Color(message, twitchClient) - return - case "mycolor": - commands.Color(message, twitchClient) - return - case "echo": - commands.Echo(message.Channel, message.Message[7:len(message.Message)], twitchClient) - return - case "8ball": - commands.EightBall(message.Channel, twitchClient) - return - case "ffzemotes": - commands.FfzEmotes(message.Channel, twitchClient) - return - case "fill": - if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()fill [emote]") - return - } else { - commands.Fill(message.Channel, message.Message[7:len(message.Message)], twitchClient) - return - } - case "firstline": - if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()firstline [channel] [user]") - return - } else if msgLen == 2 { - commands.Firstline(message.Channel, message.Channel, cmdParams[1], twitchClient) - return - } else { - commands.Firstline(message.Channel, cmdParams[1], cmdParams[2], twitchClient) - return - } - case "fl": - if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()firstline [channel] [user]") - return - } else if msgLen == 2 { - commands.Firstline(message.Channel, message.Channel, cmdParams[1], twitchClient) - return - } else { - commands.Firstline(message.Channel, cmdParams[1], cmdParams[2], twitchClient) - return - } - case "followage": - if msgLen <= 2 { - twitchClient.Say(message.Channel, "Usage: ()followage [channel] [user]") - return - } else { - commands.Followage(message.Channel, cmdParams[1], cmdParams[2], twitchClient) - return - } - case "game": - if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()game [channel]") - return - } else { - commands.Game(message.Channel, cmdParams[1], twitchClient) - } - case "godoc": - if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()godoc [term]") - return - } else { - commands.Godocs(message.Channel, message.Message[8:len(message.Message)], twitchClient) - return - } - case "godocs": - if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()godoc [term]") - return - } else { - commands.Godocs(message.Channel, message.Message[9:len(message.Message)], twitchClient) - return - } - case "num": - if msgLen == 1 { - commands.RandomNumber(message.Channel, twitchClient) - } else { - commands.Number(message.Channel, cmdParams[1], twitchClient) - } - case "number": - if msgLen == 1 { - commands.RandomNumber(message.Channel, twitchClient) - } else { - commands.Number(message.Channel, cmdParams[1], twitchClient) - } - case "ping": - commands.Ping(message.Channel, twitchClient, uptime) - return - case "pingme": - commands.Pingme(message.Channel, message.User.DisplayName, twitchClient) - return - case "preview": - if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()preview [channel]") - return - } else { - commands.Thumbnail(message.Channel, cmdParams[1], twitchClient) - return - } - case "profilepicture": - if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()profilepicture [user]") - return - } - commands.ProfilePicture(message.Channel, cmdParams[1], twitchClient) - return - case "pfp": - if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()pfp [user]") - return - } - commands.ProfilePicture(message.Channel, cmdParams[1], twitchClient) - return - case "pyramid": - if msgLen != 3 { - twitchClient.Say(message.Channel, "Usage: ()pyramid [size] [emote]") - } else if utils.ElevatedPrivsMessage(message) { - commands.Pyramid(message.Channel, cmdParams[1], cmdParams[2], twitchClient) - } else { - twitchClient.Say(message.Channel, "Pleb's can't pyramid FeelsBadMan") - } - case "randomcat": - commands.RandomCat(message.Channel, twitchClient) - return - case "randomdog": - commands.RandomDog(message.Channel, twitchClient) - return - case "randomfox": - commands.RandomFox(message.Channel, twitchClient) - return - case "randomxkcd": - commands.RandomXkcd(message.Channel, twitchClient) - return - case "subage": - if msgLen < 3 { - twitchClient.Say(message.Channel, "Usage: ()subage [user] [streamer]") - return - } else { - commands.Subage(message.Channel, cmdParams[1], cmdParams[2], twitchClient) - return - } - case "thumb": - if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()thumbnail [channel]") - return - } else { - commands.Thumbnail(message.Channel, cmdParams[1], twitchClient) - return - } - case "thumbnail": - if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()thumbnail [channel]") - return - } else { - commands.Thumbnail(message.Channel, cmdParams[1], twitchClient) - return - } - case "title": - if msgLen == 1 { - commands.Title(message.Channel, message.Channel, twitchClient) - return - } else { - commands.Title(message.Channel, cmdParams[1], twitchClient) - return - } - case "uptime": - if msgLen == 1 { - commands.Uptime(message.Channel, message.Channel, twitchClient) - return - } else { - commands.Uptime(message.Channel, cmdParams[1], twitchClient) - return - } - case "uid": - if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()uid [username]") - return - } else { - commands.Userid(message.Channel, cmdParams[1], twitchClient) - return - } - case "userid": - if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()userid [username]") - return - } else { - commands.Userid(message.Channel, cmdParams[1], twitchClient) - return - } - case "weather": - if msgLen == 1 { - twitchClient.Say(message.Channel, "Usage: ()weather [location]") - return - } else { - commands.Weather(message.Channel, message.Message[9:len(message.Message)], twitchClient) - return - } - case "xd": - commands.Xd(message.Channel, twitchClient) - return - - case "xkcd": - commands.Xkcd(message.Channel, twitchClient) - return - } } diff --git a/pkg/handlers/privatemessage.go b/pkg/handlers/privatemessage.go index fabb72a..c18cb64 100644 --- a/pkg/handlers/privatemessage.go +++ b/pkg/handlers/privatemessage.go @@ -1,10 +1,8 @@ package handlers import ( - "time" - "github.com/gempir/go-twitch-irc/v2" - "github.com/lyx0/nourybot/pkg/config" + "github.com/lyx0/nourybot/cmd/bot" log "github.com/sirupsen/logrus" ) @@ -12,7 +10,7 @@ import ( // *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. // 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(message) @@ -28,10 +26,6 @@ func HandlePrivateMessage(message twitch.PrivateMessage, client *twitch.Client, // 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 // that is less than 2 @@ -40,7 +34,7 @@ func HandlePrivateMessage(message twitch.PrivateMessage, client *twitch.Client, // Message starts with (), pass it on to // the command handler. if message.Message[:2] == "()" { - HandleCommand(message, client, uptime) + Command(message, nb) return } } diff --git a/pkg/handlers/whisper.go b/pkg/handlers/whisper.go deleted file mode 100644 index d8e10a9..0000000 --- a/pkg/handlers/whisper.go +++ /dev/null @@ -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") - } -} diff --git a/pkg/humanize/time.go b/pkg/humanize/time.go deleted file mode 100644 index 811a4f6..0000000 --- a/pkg/humanize/time.go +++ /dev/null @@ -1,13 +0,0 @@ -package humanize - -import ( - "time" - - "github.com/dustin/go-humanize" -) - -// Time returns a more human readable -// time as a string for a given time.Time -func Time(t time.Time) string { - return humanize.Time(t) -} diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go deleted file mode 100644 index bf1642d..0000000 --- a/pkg/utils/utils.go +++ /dev/null @@ -1,79 +0,0 @@ -package utils - -import ( - "fmt" - "math/rand" - "strconv" - "time" - - "github.com/gempir/go-twitch-irc/v2" -) - -var ( - tempCommands = 0 -) - -// CommandUsed is called on every command and -// Incremenents tempCommands by 1 -func CommandUsed() { - tempCommands++ -} - -// GetCommandsUsed gets tempCommands and -// returns it. Only used in ping command -func GetCommandsUsed() int { - return tempCommands -} - -// StrGenerateRandomNumber generates a random number from -// a given max value as a string -func StrGenerateRandomNumber(max string) int { - num, err := strconv.Atoi(max) - if num < 1 { - return 0 - } - - if err != nil { - fmt.Printf("Supplied value %v is not a number", num) - return 0 - } else { - rand.Seed(time.Now().UnixNano()) - return rand.Intn(num) - } -} - -// GenerateRandomNumber returns a random number from -// a given max value as a int -func GenerateRandomNumber(max int) int { - rand.Seed(time.Now().UnixNano()) - return rand.Intn(max) -} - -// GenerateRandomNumberRange returns a random number -// over a given minimum and maximum range. -func GenerateRandomNumberRange(min int, max int) int { - return (rand.Intn(max-min) + min) -} - -// ElevatedPrivsMessage is checking a given message twitch.PrivateMessage -// if it came from a moderator/vip/or broadcaster and returns a bool -func ElevatedPrivsMessage(message twitch.PrivateMessage) bool { - if message.User.Badges["moderator"] == 1 || - message.User.Badges["vip"] == 1 || - message.User.Badges["broadcaster"] == 1 { - return true - } else { - return false - } -} - -// ModPrivsMessage is checking a given message twitch.PrivateMessage -// if it came from a moderator or broadcaster and returns a bool -func ModPrivsMessage(message twitch.PrivateMessage) bool { - if message.User.Badges["moderator"] == 1 || - message.User.Badges["broadcaster"] == 1 { - return true - } else { - return false - } -}