User cmd.Args() for extra values on commands

This commit is contained in:
Michał Gdula 2025-04-24 21:19:14 +01:00
parent 29bdf1b0ae
commit 5d67b2e2b0
2 changed files with 12 additions and 22 deletions

View file

@ -71,7 +71,6 @@ func RegisterPorbCommands(a *app.App) {
func registerE621(a *app.App) app.Callback {
return func(h *app.Handler, args []string) app.Error {
var options struct {
Tags string
Order string
Rating string
}
@ -80,7 +79,6 @@ func registerE621(a *app.App) app.Callback {
cmd.StringVar(&options.Order, "order", "random", "Search order")
cmd.StringVar(&options.Rating, "rating", "e", "Search rating")
cmd.StringVar(&options.Tags, "tags", "", "Search tags")
cmd.Parse(args)
@ -90,7 +88,7 @@ func registerE621(a *app.App) app.Callback {
"https://e621.net/posts.json/?limit=1&tags=order:%s+rating:%s+%s",
options.Order,
options.Rating,
options.Tags,
strings.Join(cmd.Args(), "+"),
),
nil,
)

View file

@ -2,7 +2,6 @@ package tinyfox
import (
"errors"
"flag"
"fmt"
"net/http"
"slices"
@ -89,34 +88,27 @@ func RegisterTinyfoxCommands(a *app.App) {
func registerAnimal(a *app.App) app.Callback {
return func(h *app.Handler, args []string) app.Error {
var options struct {
Kind string
}
cmd := flag.NewFlagSet("", flag.ContinueOnError)
cmd.StringVar(&options.Kind, "kind", "", "Animal kind to search for")
cmd.Parse(args)
if options.Kind == "" {
if len(args) < 1 {
return app.Error{
Msg: "Animal name is required!",
Err: errors.New("animal name is required"),
}
}
if !slices.Contains(animals, options.Kind) {
alias, ok := animalAliases[options.Kind]
animal := args[0]
if !slices.Contains(animals, animal) {
alias, ok := animalAliases[animal]
if !ok {
return app.Error{
Msg: fmt.Sprintf("Animal \"%s\" is invalid. The following animals are supported:\n%s", options.Kind, strings.Join(animals, ", ")),
Msg: fmt.Sprintf("Animal \"%s\" is invalid. The following animals are supported:\n%s", animal, strings.Join(animals, ", ")),
Err: errors.New("entered invalid animal name"),
}
}
options.Kind = alias
animal = alias
}
req, err := http.NewRequest(http.MethodGet, "https://api.tinyfox.dev/img?animal="+options.Kind, nil)
req, err := http.NewRequest(http.MethodGet, "https://api.tinyfox.dev/img?animal="+animal, nil)
if err != nil {
return app.Error{
Msg: "Failed to make request",
@ -137,13 +129,13 @@ func registerAnimal(a *app.App) app.Callback {
Embed: &discordgo.MessageEmbed{
Title: "Animal",
Image: &discordgo.MessageEmbedImage{
URL: "attachment://image.png",
URL: "attachment://animal.png",
},
Color: utils.ColorFromRGB(255, 255, 255),
},
Files: []*discordgo.File{
{
Name: "image.png",
Name: "animal.png",
ContentType: "",
Reader: res.Body,
},