From d5d0f7ba46b14db5965f4d1125e1835e9275457c Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Tue, 10 Oct 2023 13:16:30 +0200 Subject: [PATCH] add help command --- cmd/nourybot/commands.go | 53 ++++++++++++++++++++++++++++++++++++++++ internal/data/user.go | 6 ++--- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/cmd/nourybot/commands.go b/cmd/nourybot/commands.go index 89b38b1..7a5f290 100644 --- a/cmd/nourybot/commands.go +++ b/cmd/nourybot/commands.go @@ -109,6 +109,11 @@ func (app *application) handleCommand(message twitch.PrivateMessage) { reply = commands.LastFmUserRecent(target, cmdParams[1]) } + case "help": + if msgLen > 1 { + app.commandHelp(target, cmdParams[1], message.User.Name) + } + case "nourybot": reply = "Lidl Twitch bot made by @nourylul. Prefix: ()" @@ -210,3 +215,51 @@ func (app *application) handleCommand(message twitch.PrivateMessage) { return } } + +// Map of known commands with their help texts. +var helpText = map[string]string{ + "bttv": "Returns the search URL for a given BTTV emote. Example usage: ()bttv ", + "coin": "Flips a coin! Aliases: coinflip, coin, cf", + "cf": "Flips a coin! Aliases: coinflip, coin, cf", + "coinflip": "Flips a coin! Aliases: coinflip, coin, cf", + "currency": "Returns the exchange rate for two currencies. Only three letter abbreviations are supported ( List of supported currencies: https://decapi.me/misc/currency?list ). Example usage: ()currency 10 USD to EUR", + "ffz": "Returns the search URL for a given FFZ emote. Example usage: ()ffz ", + "followage": "Returns how long a given user has been following a channel. Example usage: ()followage ", + "firstline": "Returns the first message a user has sent in a given channel. Aliases: firstline, fl. Example usage: ()firstline ", + "fl": "Returns the first message a user has sent in a given channel. Aliases: firstline, fl. Example usage: ()fl ", + "help": "Returns more information about a command and its usage. 4Head Example usage: ()help ", + "ping": "Hopefully returns a Pong! monkaS", + "preview": "Returns a link to an (almost) live screenshot of a live channel. Alias: preview, thumbnail. Example usage: ()preview ", + "phonetic": "Translates the input to the text equivalent on a phonetic russian keyboard layout. Layout and general functionality is the same as https://russian.typeit.org/", + "ph": "Translates the input to the text equivalent on a phonetic russian keyboard layout. Layout and general functionality is the same as https://russian.typeit.org/", + "thumbnail": "Returns a link to an (almost) live screenshot of a live channel. Alias: preview, thumbnail. Example usage: ()thumbnail ", + "tweet": "Returns the latest tweet for a provided user. Example usage: ()tweet ", + "seventv": "Returns the search URL for a given SevenTV emote. Aliases: seventv, 7tv. Example usage: ()seventv FeelsDankMan", + "7tv": "Returns the search URL for a given SevenTV emote. Aliases: seventv, 7tv. Example usage: ()7tv FeelsDankMan", + "weather": "Returns the weather for a given location. Example usage: ()weather Vilnius", + "randomxkcd": "Returns a link to a random xkcd comic. Alises: randomxkcd, rxkcd. Example usage: ()randomxkcd", + "rxkcd": "Returns a link to a random xkcd comic. Alises: randomxkcd, rxkcd. Example usage: ()rxkcd", + "xkcd": "Returns a link to the latest xkcd comic. Example usage: ()xkcd", +} + +// Help checks if a help text for a given command exists and replies with it. +func (app *application) commandHelp(target, name, username string) { + // Check if the `helpText` map has an entry for `name`. If it does return it's value entry + // and send that as a reply. + i, ok := helpText[name] + if !ok { + // If it doesn't check the database for a command with that `name`. If there is one + // reply with that commands `help` entry. + c, err := app.GetCommandHelp(name, username) + if err != nil { + app.Log.Infow("commandHelp: no such command found", + "err", err) + return + } + + app.Send(target, c) + return + } + + app.Send(target, i) +} diff --git a/internal/data/user.go b/internal/data/user.go index bc90e46..a75cae6 100644 --- a/internal/data/user.go +++ b/internal/data/user.go @@ -23,14 +23,14 @@ type UserModel struct { // Insert inserts a user model into the database. func (u UserModel) Insert(login, twitchId string) error { query := ` - INSERT INTO users(login, twitchid, level) - VALUES ($1, $2, $3) + INSERT INTO users(login, twitchid, level, location, lastfm_username) + VALUES ($1, $2, $3, $4, $5) ON CONFLICT (login) DO NOTHING RETURNING id, added_at; ` - args := []interface{}{login, twitchId, "0"} + args := []interface{}{login, twitchId, "0", "", ""} // Execute the query returning the number of affected rows. result, err := u.DB.Exec(query, args...)