diff --git a/cmd/bot/command.go b/cmd/bot/command.go index a11d5d8..c527ea8 100644 --- a/cmd/bot/command.go +++ b/cmd/bot/command.go @@ -94,6 +94,7 @@ func handleCommand(message twitch.PrivateMessage, tc *twitch.Client) { commands.Echo(target, message.Message[7:len(message.Message)], tc) return } + case "ffz": if msgLen < 2 { common.Send(target, "Not enough arguments provided. Usage: ()ffz ", tc) @@ -109,6 +110,44 @@ func handleCommand(message twitch.PrivateMessage, tc *twitch.Client) { } else { return } + + // Followage + // ()followage channel username + case "followage": + if msgLen == 1 { // ()followage + commands.Followage(target, target, message.User.Name, tc) + return + } else if msgLen == 2 { // ()followage forsen + commands.Followage(target, target, cmdParams[1], tc) + return + } else { // ()followage forsen pajlada + commands.Followage(target, cmdParams[1], cmdParams[2], tc) + return + } + // First Line + case "firstline": + if msgLen == 1 { + common.Send(target, "Usage: ()firstline ", tc) + return + } else if msgLen == 2 { + commands.FirstLine(target, target, cmdParams[1], tc) + return + } else { + commands.FirstLine(target, cmdParams[1], cmdParams[2], tc) + return + } + case "fl": + if msgLen == 1 { + common.Send(target, "Usage: ()firstline ", tc) + return + } else if msgLen == 2 { + commands.FirstLine(target, target, cmdParams[1], tc) + return + } else { + commands.FirstLine(target, cmdParams[1], cmdParams[2], tc) + return + } + case "ping": commands.Ping(target, tc) return @@ -120,6 +159,17 @@ func handleCommand(message twitch.PrivateMessage, tc *twitch.Client) { commands.Preview(target, cmdParams[1], tc) return } + + case "thumbnail": + if msgLen < 2 { + common.Send(target, "Not enough arguments provided. Usage: ()thumbnail ", tc) + return + } else { + commands.Preview(target, cmdParams[1], tc) + return + } + + // SevenTV case "seventv": if msgLen < 2 { common.Send(target, "Not enough arguments provided. Usage: ()seventv ", tc) @@ -136,14 +186,6 @@ func handleCommand(message twitch.PrivateMessage, tc *twitch.Client) { commands.Seventv(target, cmdParams[1], tc) return } - case "thumbnail": - if msgLen < 2 { - common.Send(target, "Not enough arguments provided. Usage: ()thumbnail ", tc) - return - } else { - commands.Preview(target, cmdParams[1], tc) - return - } case "tweet": if msgLen < 2 { common.Send(target, "Not enough arguments provided. Usage: ()tweet ", tc) diff --git a/pkg/commands/decapi/followage.go b/pkg/commands/decapi/followage.go new file mode 100644 index 0000000..4578e47 --- /dev/null +++ b/pkg/commands/decapi/followage.go @@ -0,0 +1,46 @@ +package decapi + +import ( + "fmt" + "io" + "net/http" + + "go.uber.org/zap" +) + +func Followage(channel, username string) (string, error) { + var basePath = "https://decapi.me/twitch/followage/" + + sugar := zap.NewExample().Sugar() + defer sugar.Sync() + + // ?precision is how precise the timestamp should be. + // precision 4 means: 1 2 3 4 + // pajlada has been following forsen for 7 years, 4 months, 4 weeks, 1 day + resp, err := http.Get(fmt.Sprint(basePath + channel + "/" + username + "?precision=4")) + if err != nil { + sugar.Error(err) + return "Something went wrong FeelsBadMan", err + } + + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + sugar.Error(err) + return "Something went wrong FeelsBadMan", err + } + + // User tries to look up how long he follows himself. + if string(body) == followageUserCannotFollowOwn { + return "You cannot follow yourself.", nil + + // Username is not following the requested channel. + } else if string(body) == fmt.Sprintf("%s does not follow %s", username, channel) { + return string(body), nil + } else { + reply := fmt.Sprintf("%s has been following %s for %s", username, channel, string(body)) + return reply, nil + } + +} diff --git a/pkg/commands/firstline.go b/pkg/commands/firstline.go new file mode 100644 index 0000000..7898dbc --- /dev/null +++ b/pkg/commands/firstline.go @@ -0,0 +1,20 @@ +package commands + +import ( + "fmt" + + "github.com/gempir/go-twitch-irc/v3" + "github.com/lyx0/nourybot/pkg/commands/ivr" + "github.com/lyx0/nourybot/pkg/common" +) + +func FirstLine(target, channel, username string, tc *twitch.Client) { + ivrResponse, err := ivr.FirstLine(channel, username) + + if err != nil { + common.Send(channel, fmt.Sprint(err), tc) + return + } + + common.Send(target, ivrResponse, tc) +} diff --git a/pkg/commands/followage.go b/pkg/commands/followage.go new file mode 100644 index 0000000..694fcdf --- /dev/null +++ b/pkg/commands/followage.go @@ -0,0 +1,21 @@ +package commands + +import ( + "github.com/gempir/go-twitch-irc/v3" + "github.com/lyx0/nourybot/pkg/commands/decapi" + "github.com/lyx0/nourybot/pkg/common" + "go.uber.org/zap" +) + +// ()currency 10 USD to EUR +func Followage(target, channel, username string, tc *twitch.Client) { + sugar := zap.NewExample().Sugar() + defer sugar.Sync() + + resp, err := decapi.Followage(channel, username) + if err != nil { + sugar.Error(err) + } + + common.Send(target, resp, tc) +} diff --git a/pkg/commands/ivr/firstline.go b/pkg/commands/ivr/firstline.go new file mode 100644 index 0000000..2d72f6d --- /dev/null +++ b/pkg/commands/ivr/firstline.go @@ -0,0 +1,50 @@ +package ivr + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + + "go.uber.org/zap" +) + +type firstLineApiResponse struct { + User string `json:"user"` + Message string `json:"message"` + Time string `json:"time"` + Error string `json:"error"` +} + +// FirstLine returns the first line a given user has sent in a +// given channel. +func FirstLine(channel, username string) (string, error) { + sugar := zap.NewExample().Sugar() + defer sugar.Sync() + + baseUrl := "https://api.ivr.fi/logs/firstmessage" + + resp, err := http.Get(fmt.Sprintf("%s/%s/%s", baseUrl, channel, username)) + if err != nil { + sugar.Error(err) + return "Something went wrong FeelsBadMan", err + } + + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + sugar.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 + } + +}