lucky-tgbot/plugin/reborn/main.go

74 lines
1.5 KiB
Go
Raw Permalink Normal View History

2025-01-22 17:26:32 +08:00
package reborn
import (
"encoding/json"
"fmt"
"math/rand"
"os"
tele "gopkg.in/telebot.v3"
)
type Country struct {
Name string `json:"country"`
Pop int64 `json:"population"`
}
type RebornData struct {
List []Country
TotalPop int64
}
func InitRebornList(filePath string) (*RebornData, error) {
rawJson, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}
var countries []Country
if err = json.Unmarshal(rawJson, &countries); err != nil {
return nil, fmt.Errorf("failed to parse JSON: %w", err)
}
totalPop := int64(0)
for _, country := range countries {
totalPop += country.Pop
}
return &RebornData{
List: countries,
TotalPop: totalPop,
}, nil
}
func (data *RebornData) randCountry(r *rand.Rand) (Country, error) {
randNum := r.Int63n(data.TotalPop)
for _, country := range data.List {
if randNum < country.Pop {
return country, nil
}
randNum -= country.Pop
}
return Country{}, fmt.Errorf("failed to select a country")
}
func randGender(r *rand.Rand) string {
genders := []string{"男孩子", "女孩子", " MtF", " FtM", "萝莉", "正太", "武装直升机", "沃尔玛购物袋", "狗狗", "猫猫"}
return genders[r.Intn(len(genders))]
}
func Execute(c tele.Context, r *rand.Rand, data *RebornData) error {
2025-01-22 17:26:32 +08:00
country, err := data.randCountry(r)
if err != nil {
return nil
}
gender := randGender(r)
outputText := fmt.Sprintf("投胎成功!\n你出生在%s是%s。", country.Name, gender)
return c.Reply(outputText)
}