add commands

This commit is contained in:
lyx0 2021-10-19 23:53:41 +02:00
parent 7bc69ff5e0
commit 4e6451462d
22 changed files with 651 additions and 6 deletions

View file

@ -0,0 +1,44 @@
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
}
}

70
pkg/api/ivr/subage.go Normal file
View file

@ -0,0 +1,70 @@
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
}
}

40
pkg/api/ivr/userid.go Normal file
View file

@ -0,0 +1,40 @@
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)
}
}

34
pkg/api/numbersapi.go Normal file
View file

@ -0,0 +1,34 @@
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)
}

30
pkg/api/randomcat.go Normal file
View file

@ -0,0 +1,30 @@
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)
}

30
pkg/api/randomdog.go Normal file
View file

@ -0,0 +1,30 @@
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)
}

31
pkg/api/randomfox.go Normal file
View file

@ -0,0 +1,31 @@
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)
}

13
pkg/commands/godocs.go Normal file
View file

@ -0,0 +1,13 @@
package commands
import (
"fmt"
"github.com/lyx0/nourybot/cmd/bot"
)
func Godocs(channel string, searchTerm string, nb *bot.Bot) {
resp := fmt.Sprintf("https://godocs.io/?q=%s", searchTerm)
nb.Send(channel, resp)
}

16
pkg/commands/number.go Normal file
View file

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

13
pkg/commands/pingme.go Normal file
View file

@ -0,0 +1,13 @@
package commands
import (
"fmt"
"github.com/lyx0/nourybot/cmd/bot"
)
func Pingme(channel string, user string, nb *bot.Bot) {
response := fmt.Sprintf("@%s", user)
nb.Send(channel, response)
}

View file

@ -0,0 +1,18 @@
package commands
import (
"fmt"
"github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api/ivr"
)
func ProfilePicture(channel string, target string, nb *bot.Bot) {
reply, err := ivr.ProfilePicture(target)
if err != nil {
nb.Send(channel, fmt.Sprint(err))
return
}
nb.Send(channel, reply)
}

49
pkg/commands/pyramid.go Normal file
View file

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

12
pkg/commands/randomcat.go Normal file
View file

@ -0,0 +1,12 @@
package commands
import (
"github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api"
)
func RandomCat(channel string, nb *bot.Bot) {
reply := api.RandomCat()
nb.Send(channel, reply)
}

12
pkg/commands/randomdog.go Normal file
View file

@ -0,0 +1,12 @@
package commands
import (
"github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api"
)
func RandomDog(channel string, nb *bot.Bot) {
reply := api.RandomDog()
nb.Send(channel, reply)
}

12
pkg/commands/randomfox.go Normal file
View file

@ -0,0 +1,12 @@
package commands
import (
"github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api"
)
func RandomFox(channel string, nb *bot.Bot) {
reply := api.RandomFox()
nb.Send(channel, reply)
}

18
pkg/commands/subage.go Normal file
View file

@ -0,0 +1,18 @@
package commands
import (
"fmt"
"github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/api/ivr"
)
func Subage(channel string, username string, streamer string, nb *bot.Bot) {
ivrResponse, err := ivr.Subage(username, streamer)
if err != nil {
nb.Send(channel, fmt.Sprint(err))
return
}
nb.Send(channel, ivrResponse)
}

16
pkg/commands/thumbnail.go Normal file
View file

@ -0,0 +1,16 @@
package commands
import (
"fmt"
"github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/utils"
)
func Thumbnail(channel string, target string, nb *bot.Bot) {
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)
nb.Send(channel, response)
}

19
pkg/commands/title.go Normal file
View file

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

21
pkg/commands/uptime.go Normal file
View file

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

11
pkg/commands/userid.go Normal file
View file

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

7
pkg/commands/xd.go Normal file
View file

@ -0,0 +1,7 @@
package commands
import "github.com/lyx0/nourybot/cmd/bot"
func Xd(channel string, nb *bot.Bot) {
nb.Send(channel, "xd")
}

View file

@ -6,6 +6,7 @@ import (
"github.com/gempir/go-twitch-irc/v2"
"github.com/lyx0/nourybot/cmd/bot"
"github.com/lyx0/nourybot/pkg/commands"
"github.com/lyx0/nourybot/pkg/utils"
"github.com/sirupsen/logrus"
)
@ -83,7 +84,7 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) {
nb.Send(target, "Usage: ()firstline [channel] [user]")
return
} else if msgLen == 2 {
commands.Firstline(target, message.Channel, cmdParams[1], nb)
commands.Firstline(target, target, cmdParams[1], nb)
return
} else {
commands.Firstline(target, cmdParams[1], cmdParams[2], nb)
@ -94,7 +95,7 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) {
nb.Send(target, "Usage: ()firstline [channel] [user]")
return
} else if msgLen == 2 {
commands.Firstline(target, message.Channel, cmdParams[1], nb)
commands.Firstline(target, target, cmdParams[1], nb)
return
} else {
commands.Firstline(target, cmdParams[1], cmdParams[2], nb)
@ -115,13 +116,139 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) {
} else {
commands.Game(target, cmdParams[1], nb)
}
case "randomxkcd":
commands.RandomXkcd(target, nb)
return
case "godoc":
if msgLen == 1 {
nb.Send(target, "Usage: ()godoc [term]")
return
} else {
commands.Godocs(target, message.Message[8:len(message.Message)], nb)
return
}
case "godocs":
if msgLen == 1 {
nb.Send(target, "Usage: ()godoc [term]")
return
} else {
commands.Godocs(target, message.Message[9:len(message.Message)], nb)
return
}
case "num":
if msgLen == 1 {
commands.RandomNumber(target, nb)
} else {
commands.Number(target, cmdParams[1], nb)
}
case "number":
if msgLen == 1 {
commands.RandomNumber(target, nb)
} else {
commands.Number(target, cmdParams[1], nb)
}
case "ping":
commands.Ping(target, nb)
return
case "pingme":
commands.Pingme(target, message.User.DisplayName, nb)
return
case "preview":
if msgLen == 1 {
nb.Send(target, "Usage: ()preview [channel]")
return
} else {
commands.Thumbnail(target, cmdParams[1], nb)
return
}
case "profilepicture":
if msgLen == 1 {
nb.Send(target, "Usage: ()profilepicture [user]")
return
}
commands.ProfilePicture(target, cmdParams[1], nb)
return
case "pfp":
if msgLen == 1 {
nb.Send(target, "Usage: ()pfp [user]")
return
}
commands.ProfilePicture(target, cmdParams[1], nb)
return
case "pyramid":
if msgLen != 3 {
nb.Send(target, "Usage: ()pyramid [size] [emote]")
} else if utils.ElevatedPrivsMessage(message) {
commands.Pyramid(target, cmdParams[1], cmdParams[2], nb)
} else {
nb.Send(target, "Pleb's can't pyramid FeelsBadMan")
}
case "randomcat":
commands.RandomCat(target, nb)
return
case "randomdog":
commands.RandomDog(target, nb)
return
case "randomfox":
commands.RandomFox(target, nb)
return
case "randomxkcd":
commands.RandomXkcd(target, nb)
return
case "subage":
if msgLen < 3 {
nb.Send(target, "Usage: ()subage [user] [streamer]")
return
} else {
commands.Subage(target, cmdParams[1], cmdParams[2], nb)
return
}
case "thumb":
if msgLen == 1 {
nb.Send(target, "Usage: ()thumbnail [channel]")
return
} else {
commands.Thumbnail(target, cmdParams[1], nb)
return
}
case "thumbnail":
if msgLen == 1 {
nb.Send(target, "Usage: ()thumbnail [channel]")
return
} else {
commands.Thumbnail(target, cmdParams[1], nb)
return
}
case "title":
if msgLen == 1 {
commands.Title(target, target, nb)
return
} else {
commands.Title(target, cmdParams[1], nb)
return
}
case "uptime":
if msgLen == 1 {
commands.Uptime(target, target, nb)
return
} else {
commands.Uptime(target, cmdParams[1], nb)
return
}
case "uid":
if msgLen == 1 {
nb.Send(target, "Usage: ()uid [username]")
return
} else {
commands.Userid(target, cmdParams[1], nb)
return
}
case "userid":
if msgLen == 1 {
nb.Send(target, "Usage: ()userid [username]")
return
} else {
commands.Userid(target, cmdParams[1], nb)
return
}
case "weather":
if msgLen == 1 {
@ -131,6 +258,8 @@ func Command(message twitch.PrivateMessage, nb *bot.Bot) {
commands.Weather(target, message.Message[9:len(message.Message)], nb)
return
}
case "xd":
commands.Xd(target, nb)
case "xkcd":
commands.Xkcd(target, nb)
return