prun-trade-bot-go/lib/commands/price.go

70 lines
2.0 KiB
Go
Raw Normal View History

2024-07-17 14:56:53 +02:00
package commands
import (
"code.lowsec.club/okawari/go-discord-bot/lib/helpers"
"context"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/rs/zerolog/log"
"strings"
)
type PriceCommand struct {
}
func (p PriceCommand) CreateCommand() *discordgo.ApplicationCommand {
return &discordgo.ApplicationCommand{
ID: "price",
Name: "price",
Description: "Change stock of traded commodity",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "commodity",
Description: "Ticker of wanted commodity",
Required: true,
MinLength: helpers.Pointer(1),
MaxLength: 3,
},
{
Type: discordgo.ApplicationCommandOptionString,
Name: "price",
Description: "Price of commodity in stock",
Required: true,
MinValue: helpers.Pointer(0.0),
},
},
}
}
func (p PriceCommand) Handle(ctx context.Context, session *discordgo.Session, command *discordgo.InteractionCreate) {
logger := log.Ctx(ctx)
db := helpers.DB(ctx)
commodityString := strings.ToUpper(command.ApplicationCommandData().Options[0].StringValue())
price, err := helpers.ParseNumericString(command.ApplicationCommandData().Options[1].StringValue())
if err != nil {
helpers.InteractionErrorResponse(session, command, logger, err)
return
}
logger.Info().Str("commodity", commodityString).Float64("price", price).Msg("Got price change request")
_, exists, err := helpers.FindCommodity(db, commodityString)
if err != nil {
helpers.InteractionErrorResponse(session, command, logger, err)
return
}
if !exists {
helpers.InteractionErrorResponse(session, command, logger, fmt.Errorf("Can't find any commodities by the name `%s`", commodityString))
return
}
err = session.InteractionRespond(command.Interaction, helpers.EphemeralResponse("%s", "test"))
if err != nil {
logger.Error().Str("user", command.User.Username).Err(err).Msg("Unable to respond to request from user")
}
}