ZeroBot-Plugin/plugin_chat/chat.go
himawari 793cac09cb
新增统计睡眠时间和添加协程里的异常捕获 (#84)
* fix:捕获协程异常

* feat:添加睡眠时间

* feat:添加睡眠管理功能

* fix:修改对标时间

* fix:修改注释函数名,使用一个时间

* feat:让大家能够更加清楚的看到每一个功能

* feat:把群管放在control,增加退群信息

* fix:不会recover

* fix:原本的正则限制机器人回复回复的消息,故去掉正则

* fix:添加服务详情,整理manager

* feat:chat进control

Co-authored-by: Guohuiyuan <haibaraguo@yeahka.com>
2021-12-04 20:46:24 +08:00

105 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Package chat 对话插件
package chat
import (
"github.com/FloatTech/ZeroBot-Plugin/control"
"math/rand"
"strconv"
"time"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/extension/rate"
"github.com/wdvxdr1123/ZeroBot/message"
)
var poke = rate.NewManager(time.Minute*5, 8) // 戳一戳
var engine = control.Register("chat", &control.Options{
DisableOnDefault: false,
Help: "chat\n- [BOT名字]\n- [戳一戳BOT]\n- 空调开\n- 空调关\n- 群温度\n- 设置温度[正整数]",
})
func init() { // 插件主体
// 被喊名字
engine.OnFullMatch("", zero.OnlyToMe).SetBlock(true).FirstPriority().
Handle(func(ctx *zero.Ctx) {
var nickname = zero.BotConfig.NickName[0]
time.Sleep(time.Second * 1)
ctx.SendChain(message.Text(
[]string{
nickname + "在此,有何贵干~",
"(っ●ω●)っ在~",
"这里是" + nickname + "(っ●ω●)っ",
nickname + "不在呢~",
}[rand.Intn(4)],
))
})
// 戳一戳
engine.On("notice/notify/poke", zero.OnlyToMe).SetBlock(false).FirstPriority().
Handle(func(ctx *zero.Ctx) {
var nickname = zero.BotConfig.NickName[0]
switch {
case poke.Load(ctx.Event.UserID).AcquireN(3):
// 5分钟共8块命令牌 一次消耗3块命令牌
time.Sleep(time.Second * 1)
ctx.SendChain(message.Text("请不要戳", nickname, " >_<"))
case poke.Load(ctx.Event.UserID).Acquire():
// 5分钟共8块命令牌 一次消耗1块命令牌
time.Sleep(time.Second * 1)
ctx.SendChain(message.Text("喂(#`O) 戳", nickname, "干嘛!"))
default:
// 频繁触发,不回复
}
})
// 群空调
var AirConditTemp = map[int64]int{}
var AirConditSwitch = map[int64]bool{}
engine.OnFullMatch("空调开").SetBlock(true).FirstPriority().
Handle(func(ctx *zero.Ctx) {
AirConditSwitch[ctx.Event.GroupID] = true
ctx.SendChain(message.Text("❄️哔~"))
})
engine.OnFullMatch("空调关").SetBlock(true).FirstPriority().
Handle(func(ctx *zero.Ctx) {
AirConditSwitch[ctx.Event.GroupID] = false
delete(AirConditTemp, ctx.Event.GroupID)
ctx.SendChain(message.Text("💤哔~"))
})
engine.OnRegex(`设置温度(\d+)`).SetBlock(true).FirstPriority().
Handle(func(ctx *zero.Ctx) {
if _, exist := AirConditTemp[ctx.Event.GroupID]; !exist {
AirConditTemp[ctx.Event.GroupID] = 26
}
if AirConditSwitch[ctx.Event.GroupID] {
temp := ctx.State["regex_matched"].([]string)[1]
AirConditTemp[ctx.Event.GroupID], _ = strconv.Atoi(temp)
ctx.SendChain(message.Text(
"❄️风速中", "\n",
"群温度 ", AirConditTemp[ctx.Event.GroupID], "℃",
))
} else {
ctx.SendChain(message.Text(
"💤", "\n",
"群温度 ", AirConditTemp[ctx.Event.GroupID], "℃",
))
}
})
engine.OnFullMatch(`群温度`).SetBlock(true).FirstPriority().
Handle(func(ctx *zero.Ctx) {
if _, exist := AirConditTemp[ctx.Event.GroupID]; !exist {
AirConditTemp[ctx.Event.GroupID] = 26
}
if AirConditSwitch[ctx.Event.GroupID] {
ctx.SendChain(message.Text(
"❄️风速中", "\n",
"群温度 ", AirConditTemp[ctx.Event.GroupID], "℃",
))
} else {
ctx.SendChain(message.Text(
"💤", "\n",
"群温度 ", AirConditTemp[ctx.Event.GroupID], "℃",
))
}
})
}