mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-20 06:20:08 +08:00
优化代码结构
This commit is contained in:
parent
b834307178
commit
0860a105ed
45
plugin/event/data.go
Normal file
45
plugin/event/data.go
Normal file
@ -0,0 +1,45 @@
|
||||
package event
|
||||
|
||||
type storage int64
|
||||
|
||||
// 申请
|
||||
func (s *storage) setapply(on bool) {
|
||||
if on {
|
||||
*s |= 0b001
|
||||
} else {
|
||||
*s &= 0b110
|
||||
}
|
||||
}
|
||||
|
||||
// 邀请
|
||||
func (s *storage) setinvite(on bool) {
|
||||
if on {
|
||||
*s |= 0b010
|
||||
} else {
|
||||
*s &= 0b101
|
||||
}
|
||||
}
|
||||
|
||||
// 主人
|
||||
func (s *storage) setmaster(on bool) {
|
||||
if on {
|
||||
*s |= 0b100
|
||||
} else {
|
||||
*s &= 0b011
|
||||
}
|
||||
}
|
||||
|
||||
// 申请
|
||||
func (s *storage) isapplyon() bool {
|
||||
return *s&0b001 > 0
|
||||
}
|
||||
|
||||
// 邀请
|
||||
func (s *storage) isinviteon() bool {
|
||||
return *s&0b010 > 0
|
||||
}
|
||||
|
||||
// 主人
|
||||
func (s *storage) ismasteron() bool {
|
||||
return *s&0b100 > 0
|
||||
}
|
||||
@ -2,6 +2,7 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@ -17,27 +18,33 @@ func init() {
|
||||
engine := control.Register("event", &ctrl.Options[*zero.Ctx]{
|
||||
DisableOnDefault: false,
|
||||
Help: "好友申请以及群聊邀请事件处理,默认发送给主人列表第一位\n" +
|
||||
" - [开启|关闭]自动同意[申请|邀请|主人]\n" +
|
||||
" - [同意|拒绝][申请|邀请][flag]\n" +
|
||||
"- [开启|关闭]自动同意[申请|邀请|主人]\n" +
|
||||
"- [同意|拒绝][申请|邀请][flag]\n" +
|
||||
"flag跟随事件一起发送, 默认同意主人的事件",
|
||||
})
|
||||
engine.On("request/group/invite").SetBlock(true).
|
||||
engine.On("request/group/invite").SetBlock(false).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
c, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
|
||||
if ok {
|
||||
su := zero.BotConfig.SuperUsers[0]
|
||||
now := time.Unix(ctx.Event.Time, 0).Format("2006-01-02 15:04:05")
|
||||
flag := ctx.Event.Flag
|
||||
es := base14.EncodeString(flag)
|
||||
flag, err := strconv.ParseInt(ctx.Event.Flag, 10, 64)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
return
|
||||
}
|
||||
var buf [8]byte
|
||||
binary.BigEndian.PutUint64(buf[:], uint64(flag))
|
||||
es := base14.EncodeToString(buf[1:])
|
||||
comment := ctx.Event.Comment
|
||||
userid := ctx.Event.UserID
|
||||
username := ctx.CardOrNickName(userid)
|
||||
data := c.GetData(-su)
|
||||
data := (storage)(c.GetData(-su))
|
||||
groupid := ctx.Event.GroupID
|
||||
groupname := ctx.GetGroupInfo(groupid, true).Name
|
||||
logrus.Info("[event]收到来自[", username, "](", userid, ")的群聊邀请,群:[", groupname, "](", groupid, ")")
|
||||
if data&2 == 2 || data&1 != 1 && zero.SuperUserPermission(ctx) {
|
||||
ctx.SetGroupAddRequest(flag, "invite", true, "")
|
||||
if data.isinviteon() || (data.ismasteron() && zero.SuperUserPermission(ctx)) {
|
||||
ctx.SetGroupAddRequest(ctx.Event.Flag, "invite", true, "")
|
||||
ctx.SendPrivateForwardMessage(su, message.Message{message.CustomNode(username, userid,
|
||||
"已自动同意在"+now+"收到来自"+
|
||||
"\n用户:["+username+"]("+strconv.FormatInt(userid, 10)+")的群聊邀请"+
|
||||
@ -57,21 +64,27 @@ func init() {
|
||||
message.CustomNode(username, userid, es)})
|
||||
}
|
||||
})
|
||||
engine.On("request/friend").SetBlock(true).
|
||||
engine.On("request/friend").SetBlock(false).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
c, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
|
||||
if ok {
|
||||
su := zero.BotConfig.SuperUsers[0]
|
||||
now := time.Unix(ctx.Event.Time, 0).Format("2006-01-02 15:04:05")
|
||||
flag := ctx.Event.Flag
|
||||
es := base14.EncodeString(flag)
|
||||
flag, err := strconv.ParseInt(ctx.Event.Flag, 10, 64)
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
return
|
||||
}
|
||||
var buf [8]byte
|
||||
binary.BigEndian.PutUint64(buf[:], uint64(flag))
|
||||
es := base14.EncodeToString(buf[1:])
|
||||
comment := ctx.Event.Comment
|
||||
userid := ctx.Event.UserID
|
||||
username := ctx.CardOrNickName(userid)
|
||||
data := c.GetData(-su)
|
||||
data := (storage)(c.GetData(-su))
|
||||
logrus.Info("[event]收到来自[", username, "](", userid, ")的好友申请")
|
||||
if data&4 == 4 || data&1 != 1 && zero.SuperUserPermission(ctx) {
|
||||
ctx.SetFriendAddRequest(flag, true, "")
|
||||
if data.isapplyon() || (data.ismasteron() && zero.SuperUserPermission(ctx)) {
|
||||
ctx.SetFriendAddRequest(ctx.Event.Flag, true, "")
|
||||
ctx.SendPrivateForwardMessage(su, message.Message{message.CustomNode(username, userid,
|
||||
"已自动同意在"+now+"收到来自"+
|
||||
"\n用户:["+username+"]("+strconv.FormatInt(userid, 10)+")"+
|
||||
@ -89,34 +102,25 @@ func init() {
|
||||
message.CustomNode(username, userid, es)})
|
||||
}
|
||||
})
|
||||
engine.OnRegex(`^(同意|拒绝)(申请|邀请)\s*([一-踀]+[㴁-㴆]?)\s*(.*)$`, zero.SuperUserPermission, zero.OnlyPrivate).SetBlock(true).
|
||||
engine.OnRegex(`^(同意|拒绝)(申请|邀请)\s*([一-踀]{4})\s*(.*)$`, zero.SuperUserPermission, zero.OnlyPrivate).SetBlock(true).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
su := zero.BotConfig.SuperUsers[0]
|
||||
cmd := ctx.State["regex_matched"].([]string)[1]
|
||||
org := ctx.State["regex_matched"].([]string)[2]
|
||||
flag := ctx.State["regex_matched"].([]string)[3]
|
||||
es := ctx.State["regex_matched"].([]string)[3]
|
||||
other := ctx.State["regex_matched"].([]string)[4]
|
||||
es := base14.DecodeString(flag)
|
||||
switch cmd {
|
||||
case "同意":
|
||||
var buf [8]byte
|
||||
copy(buf[1:], base14.DecodeFromString(es))
|
||||
flag := strconv.FormatInt(int64(binary.BigEndian.Uint64(buf[:])), 10)
|
||||
ok := cmd == "同意"
|
||||
switch org {
|
||||
case "申请":
|
||||
ctx.SetFriendAddRequest(es, true, other)
|
||||
ctx.SetFriendAddRequest(flag, ok, other)
|
||||
ctx.SendPrivateMessage(su, message.Text("已", cmd, org))
|
||||
case "邀请":
|
||||
ctx.SetGroupAddRequest(es, "invite", true, "")
|
||||
ctx.SetGroupAddRequest(flag, "invite", ok, other)
|
||||
ctx.SendPrivateMessage(su, message.Text("已", cmd, org))
|
||||
}
|
||||
case "拒绝":
|
||||
switch org {
|
||||
case "申请":
|
||||
ctx.SetFriendAddRequest(es, false, "")
|
||||
ctx.SendPrivateMessage(su, message.Text("已", cmd, org))
|
||||
case "邀请":
|
||||
ctx.SetGroupAddRequest(es, "invite", false, other)
|
||||
ctx.SendPrivateMessage(su, message.Text("已", cmd, org))
|
||||
}
|
||||
}
|
||||
})
|
||||
engine.OnRegex(`^(开启|关闭)自动同意(申请|邀请|主人)$`, zero.SuperUserPermission, zero.OnlyPrivate).SetBlock(true).
|
||||
Handle(func(ctx *zero.Ctx) {
|
||||
@ -124,30 +128,18 @@ func init() {
|
||||
su := zero.BotConfig.SuperUsers[0]
|
||||
option := ctx.State["regex_matched"].([]string)[1]
|
||||
from := ctx.State["regex_matched"].([]string)[2]
|
||||
data := c.GetData(-su)
|
||||
switch option {
|
||||
case "开启":
|
||||
data := (storage)(c.GetData(-su))
|
||||
switch from {
|
||||
case "申请":
|
||||
data |= 4
|
||||
data.setapply(option == "开启")
|
||||
case "邀请":
|
||||
data |= 2
|
||||
data.setinvite(option == "开启")
|
||||
case "主人":
|
||||
data &= 7
|
||||
data.setmaster(option == "开启")
|
||||
}
|
||||
case "关闭":
|
||||
switch from {
|
||||
case "申请":
|
||||
data &= 3
|
||||
case "邀请":
|
||||
data &= 5
|
||||
case "主人":
|
||||
data |= 1
|
||||
}
|
||||
}
|
||||
err := c.SetData(-su, data)
|
||||
err := c.SetData(-su, int64(data))
|
||||
if err != nil {
|
||||
ctx.SendChain(message.Text("ERROR:", err))
|
||||
ctx.SendChain(message.Text("ERROR: ", err))
|
||||
return
|
||||
}
|
||||
ctx.SendChain(message.Text("已设置自动同意" + from + "为" + option))
|
||||
|
||||
@ -553,12 +553,9 @@ func init() { // 插件主体
|
||||
// 根据 gist 自动同意加群
|
||||
// 加群请在github新建一个gist,其文件名为本群群号的字符串的md5(小写),内容为一行,是当前unix时间戳(10分钟内有效)。
|
||||
// 然后请将您的用户名和gist哈希(小写)按照username/gisthash的格式填写到回答即可。
|
||||
engine.OnRequest().SetBlock(false).Handle(func(ctx *zero.Ctx) {
|
||||
/*if ctx.Event.RequestType == "friend" {
|
||||
ctx.SetFriendAddRequest(ctx.Event.Flag, true, "")
|
||||
}*/
|
||||
engine.On("request/group/add").SetBlock(false).Handle(func(ctx *zero.Ctx) {
|
||||
c, ok := ctx.State["manager"].(*ctrl.Control[*zero.Ctx])
|
||||
if ok && c.GetData(ctx.Event.GroupID)&0x10 == 0x10 && ctx.Event.RequestType == "group" && ctx.Event.SubType == "add" {
|
||||
if ok && c.GetData(ctx.Event.GroupID)&0x10 == 0x10 {
|
||||
// gist 文件名是群号的 ascii 编码的 md5
|
||||
// gist 内容是当前 uinx 时间戳,在 10 分钟内视为有效
|
||||
ans := ctx.Event.Comment[strings.Index(ctx.Event.Comment, "答案:")+len("答案:"):]
|
||||
|
||||
Loading…
Reference in New Issue
Block a user