feat:添加词库,骂人和笑话 (#90)

Co-authored-by: Guohuiyuan <haibaraguo@yeahka.com>
This commit is contained in:
himawari
2021-12-21 21:24:59 +08:00
committed by GitHub
parent 4fdedd0a7b
commit b953385bc5
10 changed files with 1985 additions and 13 deletions

17
plugin_funny/data.go Normal file
View File

@@ -0,0 +1,17 @@
package funny
import (
"os"
"github.com/FloatTech/ZeroBot-Plugin/utils/file"
"github.com/FloatTech/ZeroBot-Plugin/utils/process"
)
// 加载数据库
func init() {
go func() {
process.SleepAbout1sTo2s()
_ = os.MkdirAll(dbpath, 0755)
_, _ = file.GetLazyData(dbfile, false, true)
}()
}

63
plugin_funny/laugh.go Normal file
View File

@@ -0,0 +1,63 @@
package funny
import (
"io/ioutil"
"math/rand"
"regexp"
"strconv"
"strings"
"time"
"github.com/FloatTech/ZeroBot-Plugin/control"
log "github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/extension/rate"
"github.com/wdvxdr1123/ZeroBot/message"
"github.com/wdvxdr1123/ZeroBot/utils/helper"
)
const (
qqReg = `\d+`
dbpath = "data/funny/"
dbfile = dbpath + "laugh.txt"
)
var (
engine = control.Register("curse", &control.Options{
DisableOnDefault: false,
Help: "讲个笑话\n" +
"- 讲个笑话[@xxx]|讲个笑话[qq号]\n",
})
limit = rate.NewManager(time.Minute, 20)
)
func init() {
engine.OnPrefix("讲个笑话").SetBlock(true).FirstPriority().Handle(func(ctx *zero.Ctx) {
if !limit.Load(ctx.Event.GroupID).Acquire() {
ctx.SendChain(message.Text("请稍后重试0x0..."))
return
}
var uid int64
var text string
reg := regexp.MustCompile(qqReg)
if len(ctx.Event.Message) > 1 && ctx.Event.Message[1].Type == "at" {
uid, _ = strconv.ParseInt(ctx.Event.Message[1].Data["qq"], 10, 64)
} else if reg.MatchString(ctx.Event.RawMessage) {
result := reg.FindAllString(ctx.Event.RawMessage, -1)
uid, _ = strconv.ParseInt(result[0], 10, 64)
} else if uid == 0 {
uid = ctx.Event.UserID
}
si := ctx.GetStrangerInfo(uid, false)
nickname := si.Get("nickname").String()
laugh, err := ioutil.ReadFile(dbfile)
if err != nil {
log.Println("err:", err)
}
laughList := strings.Split(helper.BytesToString(laugh), "\n")
rand.Seed(time.Now().Unix())
text = laughList[rand.Intn(len(laughList))]
text = strings.Replace(text, "%name", nickname, -1)
ctx.SendChain(message.Text(text))
})
}