mirror of
https://github.com/fumiama/ReiBot.git
synced 2026-06-09 12:30:47 +08:00
fix cbq permission & add more cbq engine
This commit is contained in:
130
engine.go
130
engine.go
@@ -242,7 +242,7 @@ func OnMessageKeyword(keyword string, rules ...Rule) *Matcher {
|
|||||||
return defaultEngine.OnMessageKeyword(keyword, rules...)
|
return defaultEngine.OnMessageKeyword(keyword, rules...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// OnKeyword 关键词触发器
|
// OnMessageKeyword 关键词触发器
|
||||||
func (e *Engine) OnMessageKeyword(keyword string, rules ...Rule) *Matcher {
|
func (e *Engine) OnMessageKeyword(keyword string, rules ...Rule) *Matcher {
|
||||||
matcher := &Matcher{
|
matcher := &Matcher{
|
||||||
Type: "Message",
|
Type: "Message",
|
||||||
@@ -365,6 +365,43 @@ func (e *Engine) OnMessageShell(command string, model interface{}, rules ...Rule
|
|||||||
return StoreMatcher(matcher)
|
return StoreMatcher(matcher)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnCallbackQueryPrefix 前缀触发器
|
||||||
|
func OnCallbackQueryPrefix(prefix string, rules ...Rule) *Matcher {
|
||||||
|
return defaultEngine.OnCallbackQueryPrefix(prefix, rules...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnCallbackQueryPrefix 前缀触发器
|
||||||
|
func (e *Engine) OnCallbackQueryPrefix(prefix string, rules ...Rule) *Matcher {
|
||||||
|
matcher := &Matcher{
|
||||||
|
Type: "CallbackQuery",
|
||||||
|
Rules: append([]Rule{PrefixRule(prefix)}, rules...),
|
||||||
|
Engine: e,
|
||||||
|
}
|
||||||
|
e.matchers = append(e.matchers, matcher)
|
||||||
|
return StoreMatcher(matcher)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnCallbackQuerySuffix 后缀触发器
|
||||||
|
func OnCallbackQuerySuffix(suffix string, rules ...Rule) *Matcher {
|
||||||
|
return defaultEngine.OnCallbackQuerySuffix(suffix, rules...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnCallbackQuerySuffix 后缀触发器
|
||||||
|
func (e *Engine) OnCallbackQuerySuffix(suffix string, rules ...Rule) *Matcher {
|
||||||
|
matcher := &Matcher{
|
||||||
|
Type: "CallbackQuery",
|
||||||
|
Rules: append([]Rule{SuffixRule(suffix)}, rules...),
|
||||||
|
Engine: e,
|
||||||
|
}
|
||||||
|
e.matchers = append(e.matchers, matcher)
|
||||||
|
return StoreMatcher(matcher)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnCallbackQueryRegex 正则触发器
|
||||||
|
func OnCallbackQueryRegex(regexPattern string, rules ...Rule) *Matcher {
|
||||||
|
return defaultEngine.OnCallbackQueryRegex(regexPattern, rules...)
|
||||||
|
}
|
||||||
|
|
||||||
// OnCallbackQueryRegex 正则触发器
|
// OnCallbackQueryRegex 正则触发器
|
||||||
func (e *Engine) OnCallbackQueryRegex(regexPattern string, rules ...Rule) *Matcher {
|
func (e *Engine) OnCallbackQueryRegex(regexPattern string, rules ...Rule) *Matcher {
|
||||||
matcher := &Matcher{
|
matcher := &Matcher{
|
||||||
@@ -375,3 +412,94 @@ func (e *Engine) OnCallbackQueryRegex(regexPattern string, rules ...Rule) *Match
|
|||||||
e.matchers = append(e.matchers, matcher)
|
e.matchers = append(e.matchers, matcher)
|
||||||
return StoreMatcher(matcher)
|
return StoreMatcher(matcher)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnCallbackQueryKeyword 关键词触发器
|
||||||
|
func OnCallbackQueryKeyword(keyword string, rules ...Rule) *Matcher {
|
||||||
|
return defaultEngine.OnCallbackQueryKeyword(keyword, rules...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnCallbackQueryKeyword 关键词触发器
|
||||||
|
func (e *Engine) OnCallbackQueryKeyword(keyword string, rules ...Rule) *Matcher {
|
||||||
|
matcher := &Matcher{
|
||||||
|
Type: "CallbackQuery",
|
||||||
|
Rules: append([]Rule{KeywordRule(keyword)}, rules...),
|
||||||
|
Engine: e,
|
||||||
|
}
|
||||||
|
e.matchers = append(e.matchers, matcher)
|
||||||
|
return StoreMatcher(matcher)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnCallbackQueryFullMatch 完全匹配触发器
|
||||||
|
func OnCallbackQueryFullMatch(src string, rules ...Rule) *Matcher {
|
||||||
|
return defaultEngine.OnCallbackQueryFullMatch(src, rules...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnCallbackQueryFullMatch 完全匹配触发器
|
||||||
|
func (e *Engine) OnCallbackQueryFullMatch(src string, rules ...Rule) *Matcher {
|
||||||
|
matcher := &Matcher{
|
||||||
|
Type: "CallbackQuery",
|
||||||
|
Rules: append([]Rule{FullMatchRule(src)}, rules...),
|
||||||
|
Engine: e,
|
||||||
|
}
|
||||||
|
e.matchers = append(e.matchers, matcher)
|
||||||
|
return StoreMatcher(matcher)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnCallbackQueryFullMatchGroup 完全匹配触发器组
|
||||||
|
func OnCallbackQueryFullMatchGroup(src []string, rules ...Rule) *Matcher {
|
||||||
|
return defaultEngine.OnCallbackQueryFullMatchGroup(src, rules...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnCallbackQueryFullMatchGroup 完全匹配触发器组
|
||||||
|
func (e *Engine) OnCallbackQueryFullMatchGroup(src []string, rules ...Rule) *Matcher {
|
||||||
|
matcher := &Matcher{
|
||||||
|
Type: "CallbackQuery",
|
||||||
|
Rules: append([]Rule{FullMatchRule(src...)}, rules...),
|
||||||
|
Engine: e,
|
||||||
|
}
|
||||||
|
e.matchers = append(e.matchers, matcher)
|
||||||
|
return StoreMatcher(matcher)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnCallbackQueryKeywordGroup 关键词触发器组
|
||||||
|
func OnCallbackQueryKeywordGroup(keywords []string, rules ...Rule) *Matcher {
|
||||||
|
return defaultEngine.OnCallbackQueryKeywordGroup(keywords, rules...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnCallbackQueryKeywordGroup 关键词触发器组
|
||||||
|
func (e *Engine) OnCallbackQueryKeywordGroup(keywords []string, rules ...Rule) *Matcher {
|
||||||
|
matcher := &Matcher{
|
||||||
|
Type: "CallbackQuery",
|
||||||
|
Rules: append([]Rule{KeywordRule(keywords...)}, rules...),
|
||||||
|
Engine: e,
|
||||||
|
}
|
||||||
|
e.matchers = append(e.matchers, matcher)
|
||||||
|
return StoreMatcher(matcher)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnCallbackQueryPrefixGroup 前缀触发器组
|
||||||
|
func (e *Engine) OnCallbackQueryPrefixGroup(prefix []string, rules ...Rule) *Matcher {
|
||||||
|
matcher := &Matcher{
|
||||||
|
Type: "CallbackQuery",
|
||||||
|
Rules: append([]Rule{PrefixRule(prefix...)}, rules...),
|
||||||
|
Engine: e,
|
||||||
|
}
|
||||||
|
e.matchers = append(e.matchers, matcher)
|
||||||
|
return StoreMatcher(matcher)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnCallbackQuerySuffixGroup 后缀触发器组
|
||||||
|
func OnCallbackQuerySuffixGroup(suffix []string, rules ...Rule) *Matcher {
|
||||||
|
return defaultEngine.OnCallbackQuerySuffixGroup(suffix, rules...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OnCallbackQuerySuffixGroup 后缀触发器组
|
||||||
|
func (e *Engine) OnCallbackQuerySuffixGroup(suffix []string, rules ...Rule) *Matcher {
|
||||||
|
matcher := &Matcher{
|
||||||
|
Type: "CallbackQuery",
|
||||||
|
Rules: append([]Rule{SuffixRule(suffix...)}, rules...),
|
||||||
|
Engine: e,
|
||||||
|
}
|
||||||
|
e.matchers = append(e.matchers, matcher)
|
||||||
|
return StoreMatcher(matcher)
|
||||||
|
}
|
||||||
|
|||||||
4
event.go
4
event.go
@@ -73,7 +73,7 @@ func (tc *TelegramClient) processEvent(update tgba.Update) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func match(ctx *Ctx, matchers []*Matcher) {
|
func match(ctx *Ctx, matchers []*Matcher) {
|
||||||
if ctx.Message != nil {
|
if ctx.Message != nil && ctx.Event.Type == "Message" {
|
||||||
// Caption也当作消息处理
|
// Caption也当作消息处理
|
||||||
if ctx.Message.Text == "" && ctx.Message.Caption != "" {
|
if ctx.Message.Text == "" && ctx.Message.Caption != "" {
|
||||||
ctx.Message.Text = ctx.Message.Caption
|
ctx.Message.Text = ctx.Message.Caption
|
||||||
@@ -81,7 +81,7 @@ func match(ctx *Ctx, matchers []*Matcher) {
|
|||||||
log.Println("cpoy Message Caption to Text:", ctx.Message.Text)
|
log.Println("cpoy Message Caption to Text:", ctx.Message.Text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ctx.Message != nil && ctx.Message.Text != "" { // 确保无空
|
if ctx.Message != nil && ctx.Event.Type == "Message" && ctx.Message.Text != "" { // 确保无空
|
||||||
ctx.IsToMe = func(ctx *Ctx) bool {
|
ctx.IsToMe = func(ctx *Ctx) bool {
|
||||||
if ctx.Message.Chat.IsPrivate() {
|
if ctx.Message.Chat.IsPrivate() {
|
||||||
log.Debugln("[event] private event")
|
log.Debugln("[event] private event")
|
||||||
|
|||||||
@@ -65,22 +65,27 @@ func (m *Matcher) setPriority(priority int) *Matcher {
|
|||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
// firstPriority 设置当前 Matcher 优先级 - 0
|
// firstPriority 设置当前 Matcher 优先级 - 0
|
||||||
func (m *Matcher) firstPriority() *Matcher {
|
func (m *Matcher) firstPriority() *Matcher {
|
||||||
return m.setPriority(0)
|
return m.setPriority(0)
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// secondPriority 设置当前 Matcher 优先级 - 1
|
// secondPriority 设置当前 Matcher 优先级 - 1
|
||||||
func (m *Matcher) secondPriority() *Matcher {
|
func (m *Matcher) secondPriority() *Matcher {
|
||||||
return m.setPriority(1)
|
return m.setPriority(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
// thirdPriority 设置当前 Matcher 优先级 - 2
|
// thirdPriority 设置当前 Matcher 优先级 - 2
|
||||||
func (m *Matcher) thirdPriority() *Matcher {
|
func (m *Matcher) thirdPriority() *Matcher {
|
||||||
return m.setPriority(2)
|
return m.setPriority(2)
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// Limit 限速器
|
// Limit 限速器
|
||||||
|
//
|
||||||
// postfn 当请求被拒绝时的操作
|
// postfn 当请求被拒绝时的操作
|
||||||
func (m *Matcher) Limit(limiterfn func(*Ctx) *rate.Limiter, postfn ...func(*Ctx)) *Matcher {
|
func (m *Matcher) Limit(limiterfn func(*Ctx) *rate.Limiter, postfn ...func(*Ctx)) *Matcher {
|
||||||
m.Rules = append(m.Rules, func(ctx *Ctx) bool {
|
m.Rules = append(m.Rules, func(ctx *Ctx) bool {
|
||||||
|
|||||||
206
rules.go
206
rules.go
@@ -15,8 +15,9 @@ import (
|
|||||||
// 检查消息前缀
|
// 检查消息前缀
|
||||||
func PrefixRule(prefixes ...string) Rule {
|
func PrefixRule(prefixes ...string) Rule {
|
||||||
return func(ctx *Ctx) bool {
|
return func(ctx *Ctx) bool {
|
||||||
msg, ok := ctx.Value.(*tgba.Message)
|
switch msg := ctx.Value.(type) {
|
||||||
if !ok || msg.Text == "" { // 确保无空
|
case *tgba.Message:
|
||||||
|
if msg.Text == "" { // 确保无空
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for _, prefix := range prefixes {
|
for _, prefix := range prefixes {
|
||||||
@@ -28,6 +29,22 @@ func PrefixRule(prefixes ...string) Rule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
case *tgba.CallbackQuery:
|
||||||
|
if msg.Data == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, prefix := range prefixes {
|
||||||
|
if strings.HasPrefix(msg.Data, prefix) {
|
||||||
|
ctx.State["prefix"] = prefix
|
||||||
|
arg := strings.TrimLeft(msg.Data[len(prefix):], " ")
|
||||||
|
ctx.State["args"] = arg
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,8 +53,9 @@ func PrefixRule(prefixes ...string) Rule {
|
|||||||
// 检查消息后缀
|
// 检查消息后缀
|
||||||
func SuffixRule(suffixes ...string) Rule {
|
func SuffixRule(suffixes ...string) Rule {
|
||||||
return func(ctx *Ctx) bool {
|
return func(ctx *Ctx) bool {
|
||||||
msg, ok := ctx.Value.(*tgba.Message)
|
switch msg := ctx.Value.(type) {
|
||||||
if !ok || msg.Text == "" { // 确保无空
|
case *tgba.Message:
|
||||||
|
if msg.Text == "" { // 确保无空
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for _, suffix := range suffixes {
|
for _, suffix := range suffixes {
|
||||||
@@ -49,10 +67,28 @@ func SuffixRule(suffixes ...string) Rule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
case *tgba.CallbackQuery:
|
||||||
|
if msg.Data == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, suffix := range suffixes {
|
||||||
|
if strings.HasSuffix(msg.Data, suffix) {
|
||||||
|
ctx.State["suffix"] = suffix
|
||||||
|
arg := strings.TrimRight(msg.Data[:len(msg.Data)-len(suffix)], " ")
|
||||||
|
ctx.State["args"] = arg
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommandRule check if the message is a command and trim the command name
|
// CommandRule check if the message is a command and trim the command name
|
||||||
|
//
|
||||||
|
// this rule only supports tgba.Message
|
||||||
func CommandRule(commands ...string) Rule {
|
func CommandRule(commands ...string) Rule {
|
||||||
return func(ctx *Ctx) bool {
|
return func(ctx *Ctx) bool {
|
||||||
msg, ok := ctx.Value.(*tgba.Message)
|
msg, ok := ctx.Value.(*tgba.Message)
|
||||||
@@ -123,6 +159,8 @@ func RegexRule(regexPattern string) Rule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReplyRule check if the message is replying some message
|
// ReplyRule check if the message is replying some message
|
||||||
|
//
|
||||||
|
// this rule only supports tgba.Message
|
||||||
func ReplyRule(messageID int) Rule {
|
func ReplyRule(messageID int) Rule {
|
||||||
return func(ctx *Ctx) bool {
|
return func(ctx *Ctx) bool {
|
||||||
msg, ok := ctx.Value.(*tgba.Message)
|
msg, ok := ctx.Value.(*tgba.Message)
|
||||||
@@ -136,8 +174,9 @@ func ReplyRule(messageID int) Rule {
|
|||||||
// KeywordRule check if the message has a keyword or keywords
|
// KeywordRule check if the message has a keyword or keywords
|
||||||
func KeywordRule(src ...string) Rule {
|
func KeywordRule(src ...string) Rule {
|
||||||
return func(ctx *Ctx) bool {
|
return func(ctx *Ctx) bool {
|
||||||
msg, ok := ctx.Value.(*tgba.Message)
|
switch msg := ctx.Value.(type) {
|
||||||
if !ok || msg.Text == "" { // 确保无空
|
case *tgba.Message:
|
||||||
|
if msg.Text == "" { // 确保无空
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for _, str := range src {
|
for _, str := range src {
|
||||||
@@ -147,14 +186,29 @@ func KeywordRule(src ...string) Rule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
case *tgba.CallbackQuery:
|
||||||
|
if msg.Data == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, str := range src {
|
||||||
|
if strings.Contains(msg.Data, str) {
|
||||||
|
ctx.State["keyword"] = str
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FullMatchRule check if src has the same copy of the message
|
// FullMatchRule check if src has the same copy of the message
|
||||||
func FullMatchRule(src ...string) Rule {
|
func FullMatchRule(src ...string) Rule {
|
||||||
return func(ctx *Ctx) bool {
|
return func(ctx *Ctx) bool {
|
||||||
msg, ok := ctx.Value.(*tgba.Message)
|
switch msg := ctx.Value.(type) {
|
||||||
if !ok || msg.Text == "" { // 确保无空
|
case *tgba.Message:
|
||||||
|
if msg.Text == "" { // 确保无空
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for _, str := range src {
|
for _, str := range src {
|
||||||
@@ -164,10 +218,26 @@ func FullMatchRule(src ...string) Rule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
case *tgba.CallbackQuery:
|
||||||
|
if msg.Data == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, str := range src {
|
||||||
|
if str == msg.Data {
|
||||||
|
ctx.State["matched"] = msg.Data
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShellRule 定义shell-like规则
|
// ShellRule 定义shell-like规则
|
||||||
|
//
|
||||||
|
// this rule only supports tgba.Message
|
||||||
func ShellRule(cmd string, model interface{}) Rule {
|
func ShellRule(cmd string, model interface{}) Rule {
|
||||||
cmdRule := CommandRule(cmd)
|
cmdRule := CommandRule(cmd)
|
||||||
t := reflect.TypeOf(model)
|
t := reflect.TypeOf(model)
|
||||||
@@ -190,6 +260,8 @@ func ShellRule(cmd string, model interface{}) Rule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// OnlyToMe only triggered in conditions of @bot or begin with the nicknames
|
// OnlyToMe only triggered in conditions of @bot or begin with the nicknames
|
||||||
|
//
|
||||||
|
// this rule only supports tgba.Message
|
||||||
func OnlyToMe(ctx *Ctx) bool {
|
func OnlyToMe(ctx *Ctx) bool {
|
||||||
return ctx.IsToMe
|
return ctx.IsToMe
|
||||||
}
|
}
|
||||||
@@ -197,8 +269,9 @@ func OnlyToMe(ctx *Ctx) bool {
|
|||||||
// CheckUser only triggered by specific person
|
// CheckUser only triggered by specific person
|
||||||
func CheckUser(userId ...int64) Rule {
|
func CheckUser(userId ...int64) Rule {
|
||||||
return func(ctx *Ctx) bool {
|
return func(ctx *Ctx) bool {
|
||||||
msg, ok := ctx.Value.(*tgba.Message)
|
switch msg := ctx.Value.(type) {
|
||||||
if !ok || msg.From == nil { // 确保无空
|
case *tgba.Message:
|
||||||
|
if msg.From == nil { // 确保无空
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for _, uid := range userId {
|
for _, uid := range userId {
|
||||||
@@ -207,14 +280,28 @@ func CheckUser(userId ...int64) Rule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
case *tgba.CallbackQuery:
|
||||||
|
if msg.From == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, uid := range userId {
|
||||||
|
if msg.From.ID == uid {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckChat only triggered in specific chat
|
// CheckChat only triggered in specific chat
|
||||||
func CheckChat(chatId ...int64) Rule {
|
func CheckChat(chatId ...int64) Rule {
|
||||||
return func(ctx *Ctx) bool {
|
return func(ctx *Ctx) bool {
|
||||||
msg, ok := ctx.Value.(*tgba.Message)
|
switch msg := ctx.Value.(type) {
|
||||||
if !ok || msg.Chat == nil { // 确保无空
|
case *tgba.Message:
|
||||||
|
if msg.Chat == nil { // 确保无空
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for _, cid := range chatId {
|
for _, cid := range chatId {
|
||||||
@@ -223,6 +310,19 @@ func CheckChat(chatId ...int64) Rule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
case *tgba.CallbackQuery:
|
||||||
|
if msg.Message == nil || msg.Message.Chat == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, cid := range chatId {
|
||||||
|
if msg.Message.Chat.ID == cid {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,8 +373,9 @@ func OnlyChannel(ctx *Ctx) bool {
|
|||||||
|
|
||||||
// SuperUserPermission only triggered by the bot's owner
|
// SuperUserPermission only triggered by the bot's owner
|
||||||
func SuperUserPermission(ctx *Ctx) bool {
|
func SuperUserPermission(ctx *Ctx) bool {
|
||||||
msg, ok := ctx.Value.(*tgba.Message)
|
switch msg := ctx.Value.(type) {
|
||||||
if !ok || msg.From == nil { // 确保无空
|
case *tgba.Message:
|
||||||
|
if msg.From == nil { // 确保无空
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for _, su := range ctx.Caller.b.SuperUsers {
|
for _, su := range ctx.Caller.b.SuperUsers {
|
||||||
@@ -283,18 +384,30 @@ func SuperUserPermission(ctx *Ctx) bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
case *tgba.CallbackQuery:
|
||||||
|
if msg.From == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, su := range ctx.Caller.b.SuperUsers {
|
||||||
|
if su == msg.From.ID {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreaterPermission only triggered by the group creater or higher permission
|
// CreaterPermission only triggered by the group creater or higher permission
|
||||||
func CreaterPermission(ctx *Ctx) bool {
|
func CreaterPermission(ctx *Ctx) bool {
|
||||||
msg, ok := ctx.Value.(*tgba.Message)
|
if SuperUserPermission(ctx) {
|
||||||
if !ok || msg.From == nil || msg.Chat == nil { // 确保无空
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for _, su := range ctx.Caller.b.SuperUsers {
|
|
||||||
if su == msg.From.ID {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
switch msg := ctx.Value.(type) {
|
||||||
|
case *tgba.Message:
|
||||||
|
if msg.From == nil || msg.Chat == nil { // 确保无空
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
m, err := ctx.Caller.GetChatMember(
|
m, err := ctx.Caller.GetChatMember(
|
||||||
tgba.GetChatMemberConfig{
|
tgba.GetChatMemberConfig{
|
||||||
@@ -308,18 +421,36 @@ func CreaterPermission(ctx *Ctx) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return m.IsCreator()
|
return m.IsCreator()
|
||||||
|
case *tgba.CallbackQuery:
|
||||||
|
if msg.From == nil || msg.Message == nil || msg.Message.Chat == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
m, err := ctx.Caller.GetChatMember(
|
||||||
|
tgba.GetChatMemberConfig{
|
||||||
|
ChatConfigWithUser: tgba.ChatConfigWithUser{
|
||||||
|
ChatID: msg.Message.Chat.ID,
|
||||||
|
UserID: msg.From.ID,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return m.IsCreator()
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// AdminPermission only triggered by the group admins or higher permission
|
// AdminPermission only triggered by the group admins or higher permission
|
||||||
func AdminPermission(ctx *Ctx) bool {
|
func AdminPermission(ctx *Ctx) bool {
|
||||||
msg, ok := ctx.Value.(*tgba.Message)
|
if SuperUserPermission(ctx) {
|
||||||
if !ok || msg.From == nil || msg.Chat == nil { // 确保无空
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
for _, su := range ctx.Caller.b.SuperUsers {
|
|
||||||
if su == msg.From.ID {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
switch msg := ctx.Value.(type) {
|
||||||
|
case *tgba.Message:
|
||||||
|
if msg.From == nil || msg.Chat == nil { // 确保无空
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
m, err := ctx.Caller.GetChatMember(
|
m, err := ctx.Caller.GetChatMember(
|
||||||
tgba.GetChatMemberConfig{
|
tgba.GetChatMemberConfig{
|
||||||
@@ -333,6 +464,25 @@ func AdminPermission(ctx *Ctx) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return m.IsCreator() || m.IsAdministrator()
|
return m.IsCreator() || m.IsAdministrator()
|
||||||
|
case *tgba.CallbackQuery:
|
||||||
|
if msg.From == nil || msg.Message == nil || msg.Message.Chat == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
m, err := ctx.Caller.GetChatMember(
|
||||||
|
tgba.GetChatMemberConfig{
|
||||||
|
ChatConfigWithUser: tgba.ChatConfigWithUser{
|
||||||
|
ChatID: msg.Message.Chat.ID,
|
||||||
|
UserID: msg.From.ID,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return m.IsCreator() || m.IsAdministrator()
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserOrGrpAdmin 允许用户单独使用或群管使用
|
// UserOrGrpAdmin 允许用户单独使用或群管使用
|
||||||
@@ -344,6 +494,8 @@ func UserOrGrpAdmin(ctx *Ctx) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// IsPhoto 消息是图片返回 true
|
// IsPhoto 消息是图片返回 true
|
||||||
|
//
|
||||||
|
// this rule only supports tgba.Message
|
||||||
func IsPhoto(ctx *Ctx) bool {
|
func IsPhoto(ctx *Ctx) bool {
|
||||||
msg, ok := ctx.Value.(*tgba.Message)
|
msg, ok := ctx.Value.(*tgba.Message)
|
||||||
if !ok || len(msg.Photo) == 0 { // 确保无空
|
if !ok || len(msg.Photo) == 0 { // 确保无空
|
||||||
@@ -354,6 +506,8 @@ func IsPhoto(ctx *Ctx) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MustProvidePhoto 消息不存在图片阻塞120秒至有图片,超时返回 false
|
// MustProvidePhoto 消息不存在图片阻塞120秒至有图片,超时返回 false
|
||||||
|
//
|
||||||
|
// this rule only supports tgba.Message
|
||||||
func MustProvidePhoto(needphohint, failhint string) Rule {
|
func MustProvidePhoto(needphohint, failhint string) Rule {
|
||||||
return func(ctx *Ctx) bool {
|
return func(ctx *Ctx) bool {
|
||||||
msg, ok := ctx.Value.(*tgba.Message)
|
msg, ok := ctx.Value.(*tgba.Message)
|
||||||
|
|||||||
Reference in New Issue
Block a user