mirror of
https://github.com/tiennm99/tiennm99bot.git
synced 2026-10-11 12:28:54 +00:00
The rarity tag now only styles the wish animation and the result text, so /gacha picks like /random.
115 lines
3.6 KiB
Go
115 lines
3.6 KiB
Go
package misc
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"math/rand/v2"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/go-telegram/bot"
|
|
"github.com/go-telegram/bot/models"
|
|
|
|
"github.com/tiennm99/miti99bot/internal/log"
|
|
"github.com/tiennm99/miti99bot/internal/modules"
|
|
"github.com/tiennm99/miti99bot/internal/modules/util/chathelper"
|
|
)
|
|
|
|
const (
|
|
gachaUsage = "Usage: /gacha <option,...>\nTag rarity with *4 or *5, e.g. /gacha Pizza*5, Pho*4, Rice"
|
|
gachaPlaceholder = "Wishing..."
|
|
gachaFilename = "gacha.mp4"
|
|
|
|
gachaMinRarity = 3
|
|
gachaMaxRarity = 5
|
|
gachaDefaultRarity = gachaMinRarity
|
|
)
|
|
|
|
// gachaRarityTag matches a trailing "*3", "*4", or "*5" rarity tag.
|
|
var gachaRarityTag = regexp.MustCompile(`\s*\*\s*([345])$`)
|
|
|
|
type gachaOption struct {
|
|
Label string
|
|
Rarity int
|
|
}
|
|
|
|
// parseGachaOptions splits comma-separated options and strips each one's
|
|
// rarity tag. Untagged options are 3★; options left blank after the tag is
|
|
// removed are dropped.
|
|
func parseGachaOptions(arg string) []gachaOption {
|
|
parts := splitWheelOptions(arg)
|
|
out := make([]gachaOption, 0, len(parts))
|
|
for _, part := range parts {
|
|
option := gachaOption{Label: part, Rarity: gachaDefaultRarity}
|
|
if m := gachaRarityTag.FindStringSubmatchIndex(part); m != nil {
|
|
option.Label = strings.TrimSpace(part[:m[0]])
|
|
option.Rarity = int(part[m[2]] - '0')
|
|
}
|
|
if option.Label != "" {
|
|
out = append(out, option)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// gachaResultText renders an option as its stars followed by its label.
|
|
func gachaResultText(option gachaOption) string {
|
|
return strings.Repeat("★", option.Rarity) + " " + option.Label
|
|
}
|
|
|
|
func gachaCommand() modules.Command {
|
|
return modules.Command{
|
|
Name: "gacha",
|
|
Visibility: modules.VisibilityPublic,
|
|
Description: "Wish for one comma-separated option, Genshin style; tag *4 or *5 for rarity",
|
|
Parameters: "<option,...>",
|
|
Handler: func(ctx context.Context, b *bot.Bot, update *models.Update) error {
|
|
if update.Message == nil {
|
|
return nil
|
|
}
|
|
options := parseGachaOptions(chathelper.ArgAfterCommand(update.Message.Text))
|
|
if len(options) == 0 {
|
|
return chathelper.Reply(ctx, b, update.Message, gachaUsage)
|
|
}
|
|
// Every option is equally likely; the rarity tag only styles the
|
|
// wish animation and the result text.
|
|
winner := rand.IntN(len(options))
|
|
results := make([]string, len(options))
|
|
for i, option := range options {
|
|
results[i] = gachaResultText(option)
|
|
}
|
|
|
|
placeholder := sendRenderPlaceholder(ctx, b, update.Message, gachaPlaceholder)
|
|
animation, err := renderGachaAnimation(ctx, options[winner].Label, options[winner].Rarity)
|
|
if err != nil {
|
|
if !errors.Is(err, errWheelAPINotConfigured) {
|
|
log.Warn("gacha remote render failed", "err", err)
|
|
}
|
|
return replaceWheelPlaceholder(ctx, b, update.Message, placeholder, results[winner])
|
|
}
|
|
_, err = b.SendAnimation(ctx, &bot.SendAnimationParams{
|
|
ChatID: update.Message.Chat.ID,
|
|
MessageThreadID: update.Message.MessageThreadID,
|
|
Animation: &models.InputFileUpload{
|
|
Filename: gachaFilename,
|
|
Data: bytes.NewReader(animation.Data),
|
|
},
|
|
Duration: animation.Duration,
|
|
Width: animation.Width,
|
|
Height: animation.Height,
|
|
Caption: wheelResultCaption(results, winner),
|
|
ParseMode: models.ParseModeHTML,
|
|
})
|
|
if err != nil {
|
|
log.Warn("gacha send animation failed", "chat", update.Message.Chat.ID, "err", err)
|
|
return replaceWheelPlaceholder(ctx, b, update.Message, placeholder, results[winner])
|
|
}
|
|
if err := chathelper.DeleteMessage(ctx, b, update.Message, placeholder); err != nil {
|
|
log.Warn("gacha placeholder delete failed", "chat", update.Message.Chat.ID, "err", err)
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
}
|