mirror of
https://github.com/fumiama/ReiBot.git
synced 2026-06-05 09:00:24 +08:00
add more
This commit is contained in:
18
bot.go
18
bot.go
@@ -54,3 +54,21 @@ func Run(bots ...Bot) {
|
||||
tc.Connect()
|
||||
tc.Listen()
|
||||
}
|
||||
|
||||
// GetBot 获取指定的bot (Ctx) 实例
|
||||
func GetBot(id int64) *Ctx {
|
||||
caller, ok := clients.Load(id)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return &Ctx{Caller: caller}
|
||||
}
|
||||
|
||||
// RangeBot 遍历所有bot (Ctx)实例
|
||||
//
|
||||
// 单次操作返回 true 则继续遍历,否则退出
|
||||
func RangeBot(iter func(id int64, ctx *Ctx) bool) {
|
||||
clients.Range(func(key int64, value *TelegramClient) bool {
|
||||
return iter(key, &Ctx{Caller: value})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"github.com/RomiChan/syncx"
|
||||
tgba "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -17,7 +18,10 @@ type TelegramClient struct {
|
||||
b Bot
|
||||
}
|
||||
|
||||
var logsetter = &sync.Once{}
|
||||
var (
|
||||
logsetter = &sync.Once{}
|
||||
clients = syncx.Map[int64, *TelegramClient]{}
|
||||
)
|
||||
|
||||
func init() {
|
||||
logsetter.Do(func() {
|
||||
@@ -61,6 +65,7 @@ func (tc *TelegramClient) Connect() {
|
||||
tc.Buffer = tc.b.Buffer
|
||||
break
|
||||
}
|
||||
clients.Store(tc.Self.ID, tc)
|
||||
log.Println("[INFO] 连接到Telegram服务器成功, token:", tc.b.Token)
|
||||
}
|
||||
|
||||
@@ -73,6 +78,7 @@ func (tc *TelegramClient) Listen() {
|
||||
tc.processEvent(update)
|
||||
}
|
||||
log.Println("[WARN] Telegram服务器连接断开...")
|
||||
clients.Delete(tc.Self.ID)
|
||||
time.Sleep(time.Millisecond * time.Duration(3))
|
||||
tc.Connect()
|
||||
}
|
||||
|
||||
36
rules.go
36
rules.go
@@ -208,6 +208,42 @@ func OnlyPrivate(ctx *Ctx) bool {
|
||||
return msg.Chat.Type == "private"
|
||||
}
|
||||
|
||||
// OnlyGroup requires that the ctx.Event is group message
|
||||
func OnlyGroup(ctx *Ctx) bool {
|
||||
msg, ok := ctx.Value.(*tgba.Message)
|
||||
if !ok || msg.Chat == nil { // 确保无空
|
||||
return false
|
||||
}
|
||||
return msg.Chat.Type == "group"
|
||||
}
|
||||
|
||||
// OnlySuperGroup requires that the ctx.Event is supergroup message
|
||||
func OnlySuperGroup(ctx *Ctx) bool {
|
||||
msg, ok := ctx.Value.(*tgba.Message)
|
||||
if !ok || msg.Chat == nil { // 确保无空
|
||||
return false
|
||||
}
|
||||
return msg.Chat.Type == "supergroup"
|
||||
}
|
||||
|
||||
// OnlyPublic requires that the ctx.Event is group or supergroup message
|
||||
func OnlyPublic(ctx *Ctx) bool {
|
||||
msg, ok := ctx.Value.(*tgba.Message)
|
||||
if !ok || msg.Chat == nil { // 确保无空
|
||||
return false
|
||||
}
|
||||
return msg.Chat.Type == "supergroup" || msg.Chat.Type == "group"
|
||||
}
|
||||
|
||||
// OnlyChannel requires that the ctx.Event is channel message
|
||||
func OnlyChannel(ctx *Ctx) bool {
|
||||
msg, ok := ctx.Value.(*tgba.Message)
|
||||
if !ok || msg.Chat == nil { // 确保无空
|
||||
return false
|
||||
}
|
||||
return msg.Chat.Type == "channel"
|
||||
}
|
||||
|
||||
// SuperUserPermission only triggered by the bot's owner
|
||||
func SuperUserPermission(ctx *Ctx) bool {
|
||||
msg, ok := ctx.Value.(*tgba.Message)
|
||||
|
||||
Reference in New Issue
Block a user