From b2c8c4ab6184239f1a6768c12a03ff46d54d94eb Mon Sep 17 00:00:00 2001 From: lyx0 <66651385+lyx0@users.noreply.github.com> Date: Sat, 4 Mar 2023 03:14:14 +0000 Subject: [PATCH] add `help` command that returns a help text about a command --- cmd/bot/command.go | 35 +++++++++++++++++++++++++ cmd/bot/commands.go | 55 ++++++++++++++++++++++++++++++++++++--- internal/commands/help.go | 3 --- 3 files changed, 87 insertions(+), 6 deletions(-) delete mode 100644 internal/commands/help.go diff --git a/cmd/bot/command.go b/cmd/bot/command.go index 9d8434b..b694ae5 100644 --- a/cmd/bot/command.go +++ b/cmd/bot/command.go @@ -83,6 +83,41 @@ func (app *Application) GetCommand(name, username string) (string, error) { return "", ErrUserInsufficientLevel } +// GetCommand queries the database for a name. If an entry exists it checks +// if the Command.Level is 0, if it is the command.Text value is returned. +// +// If the Command.Level is not 0 it queries the database for the level of the +// user who sent the message. If the users level is equal or higher +// the command.Text field is returned. +func (app *Application) GetCommandHelp(name, username string) (string, error) { + // Fetch the command from the database if it exists. + command, err := app.Models.Commands.Get(name) + if err != nil { + // It probably did not exist + return "", err + } + + // If the command has no level set just return the text. + // Otherwise check if the level is high enough. + if command.Level == 0 { + return command.Help, nil + } else { + // Get the user from the database to check if the userlevel is equal + // or higher than the command.Level. + user, err := app.Models.Users.Get(username) + if err != nil { + return "", err + } + if user.Level >= command.Level { + // Userlevel is sufficient so return the command.Text + return command.Help, nil + } + } + + // Userlevel was not enough so return an empty string and error. + return "", ErrUserInsufficientLevel +} + // EditCommandLevel takes in a name and level string and updates the entry with name // to the supplied level value. func (app *Application) EditCommandLevel(name, lvl string, message twitch.PrivateMessage) { diff --git a/cmd/bot/commands.go b/cmd/bot/commands.go index 4d9446e..1db24d7 100644 --- a/cmd/bot/commands.go +++ b/cmd/bot/commands.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "strings" "github.com/gempir/go-twitch-irc/v3" @@ -149,11 +150,11 @@ func (app *Application) handleCommand(message twitch.PrivateMessage) { } case "help": - if msgLen < 2 { - common.Send(target, "Not enough arguments provided. Usage: ()help ", app.TwitchClient) + if msgLen == 1 { + common.Send(target, "Provides information for a given command. Usage: ()help ", app.TwitchClient) return } else { - commands.Preview(target, cmdParams[1], app.TwitchClient) + app.commandHelp(target, cmdParams[1], message.User.Name) return } @@ -445,3 +446,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 ", + "thumbnail": "Returns a link to an (almost) live screenshot of a live channel. Alias: preview, thumbnail. Example usage: ()thumbnail ", + "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.Logger.Infow("commandHelp: no such command found", + "err", err) + return + } + + reply := fmt.Sprintf(c) + common.Send(target, reply, app.TwitchClient) + return + } + + reply := fmt.Sprintf("%s", i) + common.Send(target, reply, app.TwitchClient) + return +} diff --git a/internal/commands/help.go b/internal/commands/help.go deleted file mode 100644 index 7f891a9..0000000 --- a/internal/commands/help.go +++ /dev/null @@ -1,3 +0,0 @@ -package commands - -