remake_bot/handlers.go

156 lines
3.4 KiB
Go
Raw Normal View History

2024-02-14 22:32:38 +08:00
package main
import (
"encoding/json"
"fmt"
2024-05-12 10:24:02 +08:00
"github.com/purofle/remake_bot/quotely"
2024-02-14 22:32:38 +08:00
tele "gopkg.in/telebot.v3"
"math/rand"
2024-05-12 10:24:02 +08:00
crand "math/rand"
2024-02-14 22:32:38 +08:00
"os"
2024-05-12 10:24:02 +08:00
"strings"
2024-02-14 22:32:38 +08:00
"sync"
2024-05-12 10:24:02 +08:00
"time"
2024-02-14 22:32:38 +08:00
)
type Country struct {
CountryName string `json:"country"`
Population int64 `json:"population"`
}
var (
countryList []Country
2024-05-12 10:24:02 +08:00
userList []string
2024-02-14 22:32:38 +08:00
totalPopulation int64
mutex sync.Mutex
)
2024-05-12 10:24:02 +08:00
func initList() error {
2024-02-14 22:32:38 +08:00
rawJson, err := os.ReadFile("countries.json")
if err != nil {
return err
}
if err = json.Unmarshal(rawJson, &countryList); err != nil {
return err
}
totalPopulation = int64(0)
for _, country := range countryList {
totalPopulation += country.Population
}
2024-05-12 10:24:02 +08:00
rawJson, err = os.ReadFile("user_list.json")
if err != nil {
return err
}
if err = json.Unmarshal(rawJson, &userList); err != nil {
return err
}
2024-02-14 22:32:38 +08:00
return nil
}
func getRandomCountry() Country {
// 生成随机数
randomNum := rand.Int63n(totalPopulation)
// 根据随机数获取对应的国家
index := 0
for i, country := range countryList {
if randomNum < country.Population {
index = i
break
}
randomNum -= country.Population
}
return countryList[index]
}
func CommandRemake(c tele.Context) error {
remakeData := []string{"男孩子", "女孩子", "MtF", "FtM", "MtC", "萝莉", "正太", "武装直升机", "沃尔玛购物袋", "星巴克", "太监", "无性别", "扶她", "死胎"}
remakeResult := rand.Intn(len(remakeData))
randomCountry := getRandomCountry()
mutex.Lock()
_, hasKey := remakeCount[c.Sender().ID]
if !hasKey {
remakeCount[c.Sender().ID] = new(RemakeData)
}
oldGender := remakeCount[c.Sender().ID].count
remakeCount[c.Sender().ID] = &RemakeData{
country: randomCountry.CountryName,
gender: remakeData[remakeResult],
count: oldGender + 1,
}
mutex.Unlock()
text := fmt.Sprintf("转生成功!您现在是 %s 的 %s 了。", randomCountry.CountryName, remakeData[remakeResult])
return c.Reply(text)
}
func CommandRemakeData(c tele.Context) error {
var text string
userData, hasKey := remakeCount[c.Sender().ID]
if hasKey {
text = fmt.Sprintf("您现在是 %s 的 %s共 remake 了 %d 次", userData.country, userData.gender, userData.count)
} else {
text = "您还没有 remake 过呢,快 /remake 吧"
}
return c.Reply(text)
}
2024-05-12 10:24:02 +08:00
func CommandEat(c tele.Context) error {
2024-05-12 13:11:37 +08:00
if !(c.Chat().Type == tele.ChatPrivate || c.Chat().ID == -1001965344356) {
fmt.Println(c.Chat().ID)
return nil
}
2024-05-12 10:24:02 +08:00
method := []string{"炒", "蒸", "煮"}
// 获取时间段
hour := time.Now().Hour()
var hourText string
switch {
case hour > 6 && hour <= 10:
hourText = "早上"
case hour > 10 && hour <= 14:
hourText = "中午"
case hour > 14 && hour <= 17:
hourText = "下午"
case hour > 18 && hour <= 21:
hourText = "晚上"
default:
hourText = "宵夜"
}
var name string
if strings.Contains(c.Sender().FirstName, " | ") {
name = strings.Split(c.Sender().FirstName, " | ")[0]
} else {
name = c.Sender().FirstName
}
result := fmt.Sprintf("今天%s吃 %s %s %s", hourText, name, method[rand.Intn(len(method))], userList[crand.Intn(len(userList))])
return c.Reply(result)
}
func CommandOnText(c tele.Context) error {
2024-05-12 13:11:37 +08:00
if c.Chat().ID != -1001965344356 {
return nil
}
2024-05-12 10:24:02 +08:00
if c.Message().ReplyTo != nil {
text := quotely.QuoteReply(c.Bot(), c.Message())
if text != "" {
return c.Reply(text, tele.ModeMarkdownV2)
}
}
return nil
}