From 4bfc29c1b61606a55e0ea5ac92f96ebd1a0e215b Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Thu, 29 Feb 2024 19:56:36 +0100 Subject: [PATCH] add game command that returns current game for a provided twitch channel --- cmd/nourybot/commands.go | 35 ++++++++++++++++++++++++++++++++--- cmd/nourybot/helix.go | 18 +++++++++++++++--- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/cmd/nourybot/commands.go b/cmd/nourybot/commands.go index 423fb02..dced15e 100644 --- a/cmd/nourybot/commands.go +++ b/cmd/nourybot/commands.go @@ -119,6 +119,15 @@ func (app *application) handleCommand(message twitch.PrivateMessage) { case "frankerfacez": reply = commands.Ffz(cmdParams[1]) + case "game": + if msgLen == 1 { + reply = app.getChannelGameByUsername(target) + } else if msgLen == 2 { + reply = app.getChannelGameByUsername(cmdParams[1]) + } else { + return + } + case "ddg": reply = commands.DuckDuckGo(message.Message[6:len(message.Message)]) @@ -187,10 +196,16 @@ func (app *application) handleCommand(message twitch.PrivateMessage) { reply, _ = commands.Xkcd() case "uid": - reply, _ = app.getChannelID(cmdParams[1]) + reply, _ = app.getUserID(cmdParams[1]) case "userid": - reply, _ = app.getChannelID(cmdParams[1]) + reply, _ = app.getUserID(cmdParams[1]) + + case "uname": + reply, _ = app.getChannelUsername(cmdParams[1]) + + case "username": + reply, _ = app.getChannelUsername(cmdParams[1]) case "commands": reply = app.ListChannelCommands(message.Channel) @@ -525,6 +540,13 @@ var helpText = map[string]command{ Level: "0", Usage: "()followage ", }, + "game": { + Name: "game", + Alias: nil, + Description: "Returns the currenct game Twitch channel. If no channel is provided will default to the channel the message was sent from.", + Level: "0", + Usage: "()game [channel]", + }, "godocs": { Name: "godocs", Alias: nil, @@ -682,7 +704,7 @@ var helpText = map[string]command{ "title": { Name: "title", Alias: nil, - Description: "Returns the title of a Twitch channel.", + Description: "Returns the title of a Twitch channel. If no channel is provided will default to the channel the message was sent from.", Level: "0", Usage: "()title [name]", }, @@ -714,6 +736,13 @@ var helpText = map[string]command{ Level: "0", Usage: "()uid ", }, + "username": { + Name: "username", + Alias: []string{"username", "uname"}, + Description: "Returns the Twitch login name for a given Twitch user ID.", + Level: "0", + Usage: "()username ", + }, "wa": { Name: "wa", Alias: []string{"wa", "query"}, diff --git a/cmd/nourybot/helix.go b/cmd/nourybot/helix.go index 2889840..3eb2a66 100644 --- a/cmd/nourybot/helix.go +++ b/cmd/nourybot/helix.go @@ -4,7 +4,7 @@ import ( "github.com/nicklaw5/helix/v2" ) -func (app *application) getChannelID(username string) (string, error) { +func (app *application) getUserID(username string) (string, error) { resp, err := app.HelixClient.GetUsers(&helix.UsersParams{ Logins: []string{username}, }) @@ -16,6 +16,18 @@ func (app *application) getChannelID(username string) (string, error) { return resp.Data.Users[0].ID, nil } +func (app *application) getChannelUsername(channelID string) (string, error) { + resp, err := app.HelixClient.GetUsers(&helix.UsersParams{ + IDs: []string{channelID}, + }) + if err != nil { + app.Log.Error(err) + return "", err + } + + return resp.Data.Users[0].Login, nil +} + func (app *application) getChannelTitle(channelID string) string { resp, err := app.HelixClient.GetChannelInformation(&helix.GetChannelInformationParams{ BroadcasterID: channelID, @@ -29,7 +41,7 @@ func (app *application) getChannelTitle(channelID string) string { } func (app *application) getChannelTitleByUsername(username string) string { - channelID, err := app.getChannelID(username) + channelID, err := app.getUserID(username) if err != nil { app.Log.Error(err) return "Something went wrong FeelsBadMan" @@ -52,7 +64,7 @@ func (app *application) getChannelGame(channelId string) string { } func (app *application) getChannelGameByUsername(username string) string { - channelID, err := app.getChannelID(username) + channelID, err := app.getUserID(username) if err != nil { app.Log.Error(err) return "Something went wrong FeelsBadMan"