add help command that returns a help text about a command

This commit is contained in:
lyx0 2023-03-04 03:14:14 +00:00
parent 890a060c1c
commit b2c8c4ab61
3 changed files with 87 additions and 6 deletions

View file

@ -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) {

View file

@ -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 <commandname>", app.TwitchClient)
if msgLen == 1 {
common.Send(target, "Provides information for a given command. Usage: ()help <commandname>", 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 <emote name>",
"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 <emote name>",
"followage": "Returns how long a given user has been following a channel. Example usage: ()followage <channel> <username>",
"firstline": "Returns the first message a user has sent in a given channel. Aliases: firstline, fl. Example usage: ()firstline <channel> <username>",
"fl": "Returns the first message a user has sent in a given channel. Aliases: firstline, fl. Example usage: ()fl <channel> <username>",
"help": "Returns more information about a command and its usage. 4Head Example usage: ()help <command name>",
"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 <channel>",
"thumbnail": "Returns a link to an (almost) live screenshot of a live channel. Alias: preview, thumbnail. Example usage: ()thumbnail <channel>",
"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
}

View file

@ -1,3 +0,0 @@
package commands