mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2026-02-12 02:00:24 +00:00
✨ 增加插件 reborn
This commit is contained in:
21
plugin_reborn/born.go
Normal file
21
plugin_reborn/born.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package reborn
|
||||
|
||||
import (
|
||||
wr "github.com/mroth/weightedrand"
|
||||
)
|
||||
|
||||
var (
|
||||
gender, _ = wr.NewChooser(
|
||||
wr.Choice{Item: "男孩子", Weight: 50707},
|
||||
wr.Choice{Item: "女孩子", Weight: 48292},
|
||||
wr.Choice{Item: "雌雄同体", Weight: 1001},
|
||||
)
|
||||
)
|
||||
|
||||
func randcoun() string {
|
||||
return areac.Pick().(string)
|
||||
}
|
||||
|
||||
func randgen() string {
|
||||
return gender.Pick().(string)
|
||||
}
|
||||
82
plugin_reborn/load.go
Normal file
82
plugin_reborn/load.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package reborn
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
wr "github.com/mroth/weightedrand"
|
||||
)
|
||||
|
||||
const (
|
||||
datapath = "data/Reborn"
|
||||
jsonfile = datapath + "/rate.json"
|
||||
pburl = "https://codechina.csdn.net/u011570312/ZeroBot-Plugin/-/raw/master/" + jsonfile
|
||||
)
|
||||
|
||||
type rate []struct {
|
||||
Name string `json:"name"`
|
||||
Weight float64 `json:"weight"`
|
||||
}
|
||||
|
||||
var (
|
||||
areac *wr.Chooser
|
||||
)
|
||||
|
||||
func init() {
|
||||
area := make(rate, 226)
|
||||
err := load(&area)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
choices := make([]wr.Choice, len(area))
|
||||
for i, a := range area {
|
||||
choices[i].Item = a.Name
|
||||
choices[i].Weight = uint(a.Weight * 1e9)
|
||||
}
|
||||
areac, err = wr.NewChooser(choices...)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// load 加载rate数据
|
||||
func load(area *rate) error {
|
||||
if _, err := os.Stat(jsonfile); err == nil || os.IsExist(err) {
|
||||
f, err := os.Open(jsonfile)
|
||||
if err == nil {
|
||||
defer f.Close()
|
||||
data, err1 := io.ReadAll(f)
|
||||
if err1 == nil {
|
||||
if len(data) > 0 {
|
||||
return json.Unmarshal(data, area)
|
||||
}
|
||||
}
|
||||
return err1
|
||||
}
|
||||
} else { // 如果没有小作文,则从 url 下载
|
||||
f, err := os.Create(jsonfile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
resp, err := http.Get(pburl)
|
||||
if err == nil {
|
||||
defer resp.Body.Close()
|
||||
if resp.ContentLength > 0 {
|
||||
log.Printf("[Reborn] 从镜像下载国家和地区%d字节...", resp.ContentLength)
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err == nil && len(data) > 0 {
|
||||
_, _ = f.Write(data)
|
||||
return json.Unmarshal(data, area)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
27
plugin_reborn/main.go
Normal file
27
plugin_reborn/main.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// Package reborn 投胎 来自 https://github.com/YukariChiba/tgbot/blob/main/modules/Reborn.py
|
||||
package reborn
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/FloatTech/ZeroBot-Plugin/control"
|
||||
zero "github.com/wdvxdr1123/ZeroBot"
|
||||
"github.com/wdvxdr1123/ZeroBot/message"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
control.Register("reborn", &control.Options{
|
||||
DisableOnDefault: false,
|
||||
Help: "投胎\n- reborn",
|
||||
}).OnFullMatch("reborn").SetBlock(true).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
if rand.Int31() > 1<<27 {
|
||||
ctx.SendChain(message.At(ctx.Event.UserID), message.Text(fmt.Sprintf("投胎成功!\n您出生在 %s, 是 %s。", randcoun(), randgen())))
|
||||
} else {
|
||||
ctx.SendChain(message.At(ctx.Event.UserID), message.Text("投胎失败!\n您没能活到出生,祝您下次好运!"))
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user