From 97b66acf1da2a9199174f8985b14f77047d9e70f Mon Sep 17 00:00:00 2001 From: DawnNight <65600313+DawnNights@users.noreply.github.com> Date: Tue, 13 Apr 2021 11:39:59 +0800 Subject: [PATCH] Update chat.go --- chat/chat.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/chat/chat.go b/chat/chat.go index 59d5a871..2a964ec4 100644 --- a/chat/chat.go +++ b/chat/chat.go @@ -1,9 +1,12 @@ package chat import ( + "gopkg.in/yaml.v2" + "io/ioutil" "math/rand" + "os" + "strings" "time" - zero "github.com/wdvxdr1123/ZeroBot" "github.com/wdvxdr1123/ZeroBot/extension/rate" "github.com/wdvxdr1123/ZeroBot/message" @@ -12,6 +15,54 @@ import ( var poke = rate.NewManager(time.Minute*5, 8) // 戳一戳 func init() { // 插件主体 + myData := map[string][]string{} + yaml.Unmarshal(FileRead("chat\\myData.yaml"),&myData) + + zero.OnRegex("^(.+?)$").Handle(func(ctx *zero.Ctx) { + var nickname string = zero.BotConfig.NickName[0] + text := ctx.State["regex_matched"].([]string)[1] + + if strings.Index(text,nickname+"跟我学 ")==0{ + kv := strings.Split(text[len(nickname+"跟我学 "):len(text)]," ") + if len(kv)>0 && kv[1]!=""{ + myData[kv[0]] = kv[1:len(kv)] + content,_ := yaml.Marshal(myData) + FileWrite("chat\\myData.yaml",content) + ctx.Send(nickname + "学会了有关["+kv[0]+"]的新知识呢:\n- "+strings.Join(kv[1:len(kv)],"\n- ")) + }else { + ctx.Send("你想让"+nickname+"学些什么呀?") + } + return + } + + if strings.Index(text,nickname+"请忘掉 ")==0{ + keys := strings.Split(text[len(nickname+"请忘掉 "):len(text)]," ") + if len(keys)>0 && keys[0]!=""{ + for _, key := range keys { + if _,ok := myData[key];ok{ + delete(myData,key) + ctx.Send("["+key+"]是什么呀?"+nickname+"已经忘光光了哦~") + }else { + ctx.Send(nickname+"的脑袋里可没有与["+key+"]有关的内容呢~") + } + content,_ := yaml.Marshal(myData) + FileWrite("chat\\myData.yaml",content) + } + }else { + ctx.Send("你想让"+nickname+"忘掉什么呀?") + } + return + } + + for k,vs := range myData{ + if strings.Index(text,k) != -1{ + rand.Seed(time.Now().Unix()) + ctx.Send(vs[rand.Intn(len(vs))]) + return + } + } + }) + // 被喊名字 zero.OnFullMatch("", zero.OnlyToMe).SetBlock(false).FirstPriority(). Handle(func(ctx *zero.Ctx) { @@ -45,3 +96,17 @@ func init() { // 插件主体 return }) } + +func FileRead(path string) []byte { + //读取文件数据 + file,_ := os.Open(path) + defer file.Close() + data,_ := ioutil.ReadAll(file) + return data +} + +func FileWrite(path string,content []byte) int { + //写入文件数据 + ioutil.WriteFile(path,content,0644) + return len(content) +}