add title command, use helix api for twitch lookups

This commit is contained in:
lyx0 2024-02-29 19:32:43 +01:00
parent e993de4eb1
commit f45b141cc3
3 changed files with 135 additions and 32 deletions

View file

@ -8,7 +8,6 @@ import (
"github.com/gempir/go-twitch-irc/v4" "github.com/gempir/go-twitch-irc/v4"
"github.com/lyx0/nourybot/pkg/commands" "github.com/lyx0/nourybot/pkg/commands"
"github.com/lyx0/nourybot/pkg/common" "github.com/lyx0/nourybot/pkg/common"
"github.com/lyx0/nourybot/pkg/ivr"
"github.com/lyx0/nourybot/pkg/lastfm" "github.com/lyx0/nourybot/pkg/lastfm"
"github.com/lyx0/nourybot/pkg/owm" "github.com/lyx0/nourybot/pkg/owm"
) )
@ -89,7 +88,6 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
reply = commands.Bttv(cmdParams[1]) reply = commands.Bttv(cmdParams[1])
} }
// Coinflip
case "coin": case "coin":
reply = commands.Coinflip() reply = commands.Coinflip()
@ -103,7 +101,6 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
if msgLen < 4 { if msgLen < 4 {
reply = "Not enough arguments provided. Usage: ()currency 10 USD to EUR" reply = "Not enough arguments provided. Usage: ()currency 10 USD to EUR"
} else { } else {
// ()currency <amount> <input currency> to <output currency>
reply, _ = commands.Currency(cmdParams[1], cmdParams[2], cmdParams[4]) reply, _ = commands.Currency(cmdParams[1], cmdParams[2], cmdParams[4])
} }
@ -122,30 +119,6 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
case "frankerfacez": case "frankerfacez":
reply = commands.Ffz(cmdParams[1]) reply = commands.Ffz(cmdParams[1])
case "notify":
switch cmdParams[1] {
case "live":
if userLevel >= 100 {
reply = app.createLiveSubscription(target, cmdParams[2])
}
case "offline":
if userLevel >= 100 {
reply = app.createOfflineSubscription(target, cmdParams[2])
}
}
case "unnotify":
switch cmdParams[1] {
case "live":
if userLevel >= 100 {
reply = app.deleteLiveSubscription(target, cmdParams[2])
}
case "offline":
if userLevel >= 100 {
reply = app.deleteOfflineSubscription(target, cmdParams[2])
}
}
case "ddg": case "ddg":
reply = commands.DuckDuckGo(message.Message[6:len(message.Message)]) reply = commands.DuckDuckGo(message.Message[6:len(message.Message)])
@ -214,10 +187,10 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
reply, _ = commands.Xkcd() reply, _ = commands.Xkcd()
case "uid": case "uid":
reply = ivr.IDByUsernameReply(cmdParams[1]) reply, _ = app.getChannelID(cmdParams[1])
case "userid": case "userid":
reply = ivr.IDByUsernameReply(cmdParams[1]) reply, _ = app.getChannelID(cmdParams[1])
case "commands": case "commands":
reply = app.ListChannelCommands(message.Channel) reply = app.ListChannelCommands(message.Channel)
@ -225,6 +198,15 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
case "timers": case "timers":
reply = fmt.Sprintf("https://bot.noury.li/timer/%s", message.Channel) reply = fmt.Sprintf("https://bot.noury.li/timer/%s", message.Channel)
case "title":
if msgLen == 1 {
reply = app.getChannelTitleByUsername(target)
} else if msgLen == 2 {
reply = app.getChannelTitleByUsername(cmdParams[1])
} else {
return
}
case "conv": case "conv":
if userLevel >= 100 { if userLevel >= 100 {
app.ConvertToMP4(cmdParams[1], message) app.ConvertToMP4(cmdParams[1], message)
@ -297,6 +279,30 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
go app.NewDownload("kappa", target, cmdParams[1], message) go app.NewDownload("kappa", target, cmdParams[1], message)
} }
case "notify":
switch cmdParams[1] {
case "live":
if userLevel >= 420 {
reply = app.createLiveSubscription(target, cmdParams[2])
}
case "offline":
if userLevel >= 420 {
reply = app.createOfflineSubscription(target, cmdParams[2])
}
}
case "unnotify":
switch cmdParams[1] {
case "live":
if userLevel >= 420 {
reply = app.deleteLiveSubscription(target, cmdParams[2])
}
case "offline":
if userLevel >= 420 {
reply = app.deleteOfflineSubscription(target, cmdParams[2])
}
}
case "yaf": case "yaf":
if userLevel >= 420 { if userLevel >= 420 {
go app.NewDownload("yaf", target, cmdParams[1], message) go app.NewDownload("yaf", target, cmdParams[1], message)
@ -568,6 +574,20 @@ var helpText = map[string]command{
Level: "0", Level: "0",
Usage: "()lastfm [username]", Usage: "()lastfm [username]",
}, },
"notify live": {
Name: "notify live",
Alias: nil,
Description: `Sends a notification when the specified Twitch channel goes online.`,
Level: "420",
Usage: "()notify live <username>",
},
"notify offline": {
Name: "notify offline",
Alias: nil,
Description: `Sends a notification when the specified Twitch channel goes offline.`,
Level: "420",
Usage: "()notify offline <username>",
},
"osrs": { "osrs": {
Name: "osrs", Name: "osrs",
Alias: nil, Alias: nil,
@ -659,6 +679,27 @@ var helpText = map[string]command{
Level: "0", Level: "0",
Usage: "()timers", Usage: "()timers",
}, },
"title": {
Name: "title",
Alias: nil,
Description: "Returns the title of a Twitch channel.",
Level: "0",
Usage: "()title [name]",
},
"unnotify live": {
Name: "unnotify live",
Alias: nil,
Description: `Removes a notification for a channel going online.`,
Level: "420",
Usage: "()unnotify live <username>",
},
"unnotify offline": {
Name: "unnotify offline",
Alias: nil,
Description: `Removes a notification for a channel going offline.`,
Level: "420",
Usage: "()unnotify offline <username>",
},
"user edit level": { "user edit level": {
Name: "user edit level", Name: "user edit level",
Alias: []string{"user set level"}, Alias: []string{"user set level"},

63
cmd/nourybot/helix.go Normal file
View file

@ -0,0 +1,63 @@
package main
import (
"github.com/nicklaw5/helix/v2"
)
func (app *application) getChannelID(username string) (string, error) {
resp, err := app.HelixClient.GetUsers(&helix.UsersParams{
Logins: []string{username},
})
if err != nil {
app.Log.Error(err)
return "", err
}
return resp.Data.Users[0].ID, nil
}
func (app *application) getChannelTitle(channelID string) string {
resp, err := app.HelixClient.GetChannelInformation(&helix.GetChannelInformationParams{
BroadcasterID: channelID,
})
if err != nil {
app.Log.Error(err)
return "Something went wrong FeelsBadMan"
}
return resp.Data.Channels[0].Title
}
func (app *application) getChannelTitleByUsername(username string) string {
channelID, err := app.getChannelID(username)
if err != nil {
app.Log.Error(err)
return "Something went wrong FeelsBadMan"
}
reply := app.getChannelTitle(channelID)
return reply
}
func (app *application) getChannelGame(channelId string) string {
resp, err := app.HelixClient.GetChannelInformation(&helix.GetChannelInformationParams{
BroadcasterID: channelId,
})
if err != nil {
app.Log.Error(err)
return "Something went wrong FeelsBadMan"
}
return resp.Data.Channels[0].GameName
}
func (app *application) getChannelGameByUsername(username string) string {
channelID, err := app.getChannelID(username)
if err != nil {
app.Log.Error(err)
return "Something went wrong FeelsBadMan"
}
reply := app.getChannelGame(channelID)
return reply
}

View file

@ -14,7 +14,6 @@ import (
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"github.com/lyx0/nourybot/internal/data" "github.com/lyx0/nourybot/internal/data"
"github.com/lyx0/nourybot/pkg/common" "github.com/lyx0/nourybot/pkg/common"
"github.com/lyx0/nourybot/pkg/ivr"
"github.com/nicklaw5/helix/v2" "github.com/nicklaw5/helix/v2"
) )
@ -95,8 +94,8 @@ func (app *application) eventsubFollow(w http.ResponseWriter, r *http.Request, p
w.WriteHeader(200) w.WriteHeader(200)
w.Write([]byte("ok")) w.Write([]byte("ok"))
game := ivr.GameByUsername(liveEvent.BroadcasterUserLogin) game := app.getChannelGame(liveEvent.BroadcasterUserID)
title := ivr.TitleByUsername(liveEvent.BroadcasterUserLogin) title := app.getChannelTitle(liveEvent.BroadcasterUserID)
app.SendNoBanphrase(channel, fmt.Sprintf("%s went live FeelsGoodMan Game: %s; Title: %s; https://twitch.tv/%s", liveEvent.BroadcasterUserName, game, title, liveEvent.BroadcasterUserLogin)) app.SendNoBanphrase(channel, fmt.Sprintf("%s went live FeelsGoodMan Game: %s; Title: %s; https://twitch.tv/%s", liveEvent.BroadcasterUserName, game, title, liveEvent.BroadcasterUserLogin))