1
0
mirror of https://github.com/fumiama/ReiBot.git synced 2026-06-05 09:00:24 +08:00
This commit is contained in:
源文雨
2022-06-04 13:28:29 +08:00
parent e31ccdbd69
commit e3c0d5efd6
3 changed files with 61 additions and 1 deletions

18
bot.go
View File

@@ -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})
})
}

View File

@@ -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()
}

View File

@@ -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)