1
0
mirror of https://github.com/fumiama/NanoBot.git synced 2026-06-05 10:40:24 +08:00

feat: more zb- like apis

This commit is contained in:
源文雨
2023-10-18 14:12:51 +09:00
parent 58c5f9bd78
commit dde5d2df54
2 changed files with 68 additions and 0 deletions

34
bot.go
View File

@@ -394,3 +394,37 @@ func (bot *Bot) Listen() {
}
}
}
// GetBot 获取指定的bot (Ctx)实例
func GetBot(id string) *Ctx {
caller, ok := clients.Load(id)
if !ok {
return nil
}
return &Ctx{caller: caller}
}
// RangeBot 遍历所有bot (Ctx)实例
//
// 单次操作返回 true 则继续遍历,否则退出
func RangeBot(iter func(id string, ctx *Ctx) bool) {
clients.Range(func(key string, value *Bot) bool {
return iter(key, &Ctx{caller: value})
})
}
// GetFirstSuperUser 在 ids 中获得 SuperUsers 列表的首个 qq
//
// 找不到返回 nil
func (bot *Bot) GetFirstSuperUser(ids ...string) string {
m := make(map[string]struct{}, len(ids)*4)
for _, qq := range ids {
m[qq] = struct{}{}
}
for _, qq := range bot.SuperUsers {
if _, ok := m[qq]; ok {
return qq
}
}
return ""
}

View File

@@ -123,6 +123,40 @@ func (ctx *Ctx) SendImage(file string, replytosender bool, caption ...any) (*Mes
return ctx.Send(replytosender, post)
}
// Echo 向自身分发虚拟事件
func (ctx *Ctx) Echo(payload *WebsocketPayload) {
ctx.caller.processEvent(payload)
}
// FutureEvent ...
func (ctx *Ctx) FutureEvent(Type string, rule ...Rule) *FutureEvent {
return ctx.ma.FutureEvent(Type, rule...)
}
// Get 从 promt 获得回复
func (ctx *Ctx) Get(prompt string) string {
if prompt != "" {
_, _ = ctx.SendPlainMessage(false, prompt)
}
return (<-ctx.FutureEvent("Message", ctx.CheckSession()).Next()).Event.Value.(*Message).Content
}
// ExtractPlainText 提取消息中的纯文本
func (ctx *Ctx) ExtractPlainText() string {
if ctx == nil || ctx.Value == nil {
return ""
}
if msg, ok := ctx.Value.(*Message); ok {
return msg.Content
}
return ""
}
// MessageString 字符串消息便于Regex
func (ctx *Ctx) MessageString() string {
return ctx.ExtractPlainText()
}
// Block 匹配成功后阻止后续触发
func (ctx *Ctx) Block() {
ctx.ma.SetBlock(true)