Skip to content

Commit

Permalink
add plugin: poke
Browse files Browse the repository at this point in the history
  • Loading branch information
RicheyJang committed Jul 5, 2022
1 parent 2865818 commit 5896685
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 5 deletions.
5 changes: 0 additions & 5 deletions basic/ban/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"time"

"github.com/RicheyJang/PaimengBot/manager"
"github.com/RicheyJang/PaimengBot/utils"

zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
)
Expand Down Expand Up @@ -56,9 +54,6 @@ const AllPluginKey = "all"
const TipContent = "该功能已被禁用"

func checkPluginStatus(condition *manager.PluginCondition, ctx *zero.Ctx) error {
if !utils.IsMessage(ctx) { //仅处理消息类型事件
return nil
}
// 群ban
if ctx.Event.GroupID != 0 && !GetGroupPluginStatus(ctx.Event.GroupID, condition) {
if proxy.GetConfigBool("tip") {
Expand Down
7 changes: 7 additions & 0 deletions plugins/pixiv/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ func dealWithdraw(msgID int64) {
})
}

// GetRandomPictures 获取至少least张随机涩图,但也可能为空
func GetRandomPictures(tags []string, least int, r18 bool) []PictureInfo {
d := newDownloader(tags, least, r18)
d.get()
return d.pics
}

// CheckNoSESE 不可以涩涩检查,为true时才可以进行下一步
func (pic PictureInfo) CheckNoSESE() bool {
return proxy.GetConfigBool("r18") || !utils.StringSliceContain(pic.Tags, "R-18")
Expand Down
114 changes: 114 additions & 0 deletions plugins/poke/poke.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package poke

import (
"math/rand"
"regexp"
"strconv"
"strings"
"time"

"github.com/RicheyJang/PaimengBot/basic/ban"
"github.com/RicheyJang/PaimengBot/manager"
"github.com/RicheyJang/PaimengBot/plugins/pixiv"
"github.com/RicheyJang/PaimengBot/utils"
"github.com/RicheyJang/PaimengBot/utils/consts"
log "github.com/sirupsen/logrus"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
)

var proxy *manager.PluginProxy
var info = manager.PluginInfo{
Name: "戳一戳",
Usage: `戳一戳\拍一拍时随机回复`,
SuperUsage: `需要把go-cqhttp的设备类型修改为非手表
config-plugin配置项:
poke.replies: 回复内容列表,会从中随机选取,支持CQ码
另外,回复内容里还可以配置上某种动作,例如:
不准戳我![mute] :这将回复一句"不准戳我",并将其禁言2分钟
目前支持的动作:
[mute]:禁言两分钟,前提是将Bot设为群管理员
[poke]:戳回去
[ban]:封禁两分钟
[pixiv]:随机一张好康的图片
另另外,动作内还可以附带一个0~1的实数,代表该动作的发生概率,例如:
不准戳我![mute 0.5] :当随机选取到这一回复时,会有50%的概率将其禁言2分钟`,
}

func init() {
proxy = manager.RegisterPlugin(info)
if proxy == nil {
return
}
proxy.OnNotice(func(ctx *zero.Ctx) bool {
return ctx.Event.NoticeType == "notify" && ctx.Event.SubType == "poke" && ctx.Event.TargetID == ctx.Event.SelfID
}).SetBlock(true).ThirdPriority().Handle(pokeHandler)
proxy.AddConfig(consts.PluginConfigCDKey, "3s")
proxy.AddConfig("replies", []string{"?", "hentai!", "( >﹏<。)", "好气喔,我要给你起个难听的绰号", "那...那里...那里不能戳...", "喏[pixiv]", "不准戳我啦[mute 0.1]", "[poke]"})
}

var actionRegex = regexp.MustCompile(`\[([a-z]{2,10})\s*([01]\.\d+)?]`)

func pokeHandler(ctx *zero.Ctx) {
// 初始化
if proxy.LockUser(ctx.Event.UserID) {
return
}
defer proxy.UnlockUser(ctx.Event.UserID)
replies := proxy.GetConfigStrings("replies")
if len(replies) == 0 {
return
}
reply := replies[rand.Intn(len(replies))]
log.Info("即将回复:", reply)
// 发送回复内容
str := strings.TrimSpace(actionRegex.ReplaceAllString(reply, ""))
if len(str) > 0 { // 内容非空
ctx.Send(str)
}
// 解析、执行动作字符串
actions := actionRegex.FindAllStringSubmatch(reply, -1)
for _, action := range actions {
if len(actions) <= 2 {
continue
}
rate, _ := strconv.ParseFloat(action[2], 32)
dealActions(ctx, action[1], rate)
}
}

// 处理动作
func dealActions(ctx *zero.Ctx, action string, rate float64) {
if rate != 0 && rand.Float64() > rate { // 不满足概率,不触发
return
}
if ctx.Event.UserID == 0 {
return
}
// 执行各类动作
switch action {
case "mute": // 禁言
if ctx.Event.GroupID == 0 || utils.IsSuperUser(ctx.Event.UserID) {
break
}
ctx.SetGroupBan(ctx.Event.GroupID, ctx.Event.UserID, 120)
case "poke": // 戳一戳
if ctx.Event.GroupID == 0 {
break
}
ctx.Send(message.Poke(ctx.Event.UserID))
case "ban": // 封禁
if utils.IsSuperUser(ctx.Event.UserID) {
break
}
_ = ban.SetUserPluginStatus(false, ctx.Event.UserID, nil, 2*time.Minute)
case "pixiv": // 好康的
pics := pixiv.GetRandomPictures(nil, 1, false)
for _, pic := range pics {
if msg, err := pic.GenSinglePicMsg(); err == nil {
ctx.Send(msg)
break
}
}
}
}

0 comments on commit 5896685

Please sign in to comment.