mirror of
https://github.com/fumiama/NanoBot.git
synced 2026-06-05 18:50:24 +08:00
feat: GetTriggeredMessages
This commit is contained in:
51
context.go
51
context.go
@@ -77,7 +77,7 @@ func (ctx *Ctx) CheckSession() Rule {
|
||||
}
|
||||
|
||||
// Send 发送消息到对方
|
||||
func (ctx *Ctx) Send(replytosender bool, post *MessagePost) (*Message, error) {
|
||||
func (ctx *Ctx) Send(replytosender bool, post *MessagePost) (reply *Message, err error) {
|
||||
msg, ok := ctx.Value.(*Message)
|
||||
if ok && msg != nil {
|
||||
post.ReplyMessageID = msg.ID
|
||||
@@ -91,46 +91,27 @@ func (ctx *Ctx) Send(replytosender bool, post *MessagePost) (*Message, error) {
|
||||
}
|
||||
|
||||
if msg.SrcGuildID != "" { // dms
|
||||
return ctx.PostMessageToUser(msg.GuildID, post)
|
||||
reply, err = ctx.PostMessageToUser(msg.GuildID, post)
|
||||
} else {
|
||||
reply, err = ctx.PostMessageToChannel(msg.ChannelID, post)
|
||||
}
|
||||
return ctx.PostMessageToChannel(msg.ChannelID, post)
|
||||
if ok && msg != nil && reply != nil && reply.ID != "" {
|
||||
logtriggeredmessages(msg.ID, reply.ID)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// SendPlainMessage 发送纯文本消息到对方
|
||||
func (ctx *Ctx) SendPlainMessage(replytosender bool, printable ...any) (*Message, error) {
|
||||
msg, ok := ctx.Value.(*Message)
|
||||
post := &MessagePost{}
|
||||
if ok && msg != nil {
|
||||
post.ReplyMessageID = msg.ID
|
||||
if replytosender {
|
||||
post.MessageReference = &MessageReference{
|
||||
MessageID: msg.ID,
|
||||
}
|
||||
}
|
||||
} else {
|
||||
post.ReplyMessageID = "MESSAGE_CREATE"
|
||||
}
|
||||
|
||||
post.Content = HideURL(fmt.Sprint(printable...))
|
||||
if msg.SrcGuildID != "" { // dms
|
||||
return ctx.PostMessageToUser(msg.GuildID, post)
|
||||
}
|
||||
return ctx.PostMessageToChannel(msg.ChannelID, post)
|
||||
return ctx.Send(replytosender, &MessagePost{
|
||||
Content: HideURL(fmt.Sprint(printable...)),
|
||||
})
|
||||
}
|
||||
|
||||
// SendImage 发送带图片消息到对方
|
||||
func (ctx *Ctx) SendImage(file string, replytosender bool, caption ...any) (*Message, error) {
|
||||
msg, ok := ctx.Value.(*Message)
|
||||
post := &MessagePost{}
|
||||
if ok && msg != nil {
|
||||
post.ReplyMessageID = msg.ID
|
||||
if replytosender {
|
||||
post.MessageReference = &MessageReference{
|
||||
MessageID: msg.ID,
|
||||
}
|
||||
}
|
||||
} else {
|
||||
post.ReplyMessageID = "MESSAGE_CREATE"
|
||||
post := &MessagePost{
|
||||
Content: HideURL(fmt.Sprint(caption...)),
|
||||
}
|
||||
|
||||
if strings.HasPrefix(file, "http") {
|
||||
@@ -138,12 +119,8 @@ func (ctx *Ctx) SendImage(file string, replytosender bool, caption ...any) (*Mes
|
||||
} else {
|
||||
post.ImageFile = file
|
||||
}
|
||||
post.Content = HideURL(fmt.Sprint(caption...))
|
||||
|
||||
if msg.SrcGuildID != "" { // dms
|
||||
return ctx.PostMessageToUser(msg.GuildID, post)
|
||||
}
|
||||
return ctx.PostMessageToChannel(msg.ChannelID, post)
|
||||
return ctx.Send(replytosender, post)
|
||||
}
|
||||
|
||||
// Block 匹配成功后阻止后续触发
|
||||
|
||||
2
go.mod
2
go.mod
@@ -4,6 +4,7 @@ go 1.20
|
||||
|
||||
require (
|
||||
github.com/FloatTech/floatbox v0.0.0-20231017134949-ae5059ebace7
|
||||
github.com/FloatTech/ttl v0.0.0-20230307105452-d6f7b2b647d1
|
||||
github.com/FloatTech/zbpctrl v1.6.0
|
||||
github.com/RomiChan/syncx v0.0.0-20221202055724-5f842c53020e
|
||||
github.com/RomiChan/websocket v1.4.3-0.20220227141055-9b2c6168c9c5
|
||||
@@ -17,7 +18,6 @@ require (
|
||||
|
||||
require (
|
||||
github.com/FloatTech/sqlite v1.6.3 // indirect
|
||||
github.com/FloatTech/ttl v0.0.0-20220715042055-15612be72f5b // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/fumiama/cron v1.3.0 // indirect
|
||||
github.com/fumiama/go-registry v0.2.6 // indirect
|
||||
|
||||
4
go.sum
4
go.sum
@@ -2,8 +2,8 @@ github.com/FloatTech/floatbox v0.0.0-20231017134949-ae5059ebace7 h1:5GQYI8JfYhbw
|
||||
github.com/FloatTech/floatbox v0.0.0-20231017134949-ae5059ebace7/go.mod h1:TeTlp+hTxpJti4JSdmUqzxGEr4wUBOVct9YWBepilpc=
|
||||
github.com/FloatTech/sqlite v1.6.3 h1:MQkqBNlkPuCoKQQgoNLuTL/2Ci3tBTFAnVYBdD0Wy4M=
|
||||
github.com/FloatTech/sqlite v1.6.3/go.mod h1:zFbHzRfB+CJ+VidfjuVbrcin3DAz283F7hF1hIeHzpY=
|
||||
github.com/FloatTech/ttl v0.0.0-20220715042055-15612be72f5b h1:tvciXWq2nuvTbFeJGLDNIdRX3BI546D3O7k7vrVueZw=
|
||||
github.com/FloatTech/ttl v0.0.0-20220715042055-15612be72f5b/go.mod h1:fHZFWGquNXuHttu9dUYoKuNbm3dzLETnIOnm1muSfDs=
|
||||
github.com/FloatTech/ttl v0.0.0-20230307105452-d6f7b2b647d1 h1:g4pTnDJUW4VbJ9NvoRfUvdjDrHz/6QhfN/LoIIpICbo=
|
||||
github.com/FloatTech/ttl v0.0.0-20230307105452-d6f7b2b647d1/go.mod h1:fHZFWGquNXuHttu9dUYoKuNbm3dzLETnIOnm1muSfDs=
|
||||
github.com/FloatTech/zbpctrl v1.6.0 h1:BWg9aRR4bUCmNNKj6GPH0TmzFRWYImIi6rQcQTTYRs4=
|
||||
github.com/FloatTech/zbpctrl v1.6.0/go.mod h1:i3GGM5K4HiDsXzvmXQSYoH1QT3tsSaAHjRzHwKGsHG0=
|
||||
github.com/RomiChan/syncx v0.0.0-20221202055724-5f842c53020e h1:wR3MXQ3VbUlPKOOUwLOYgh/QaJThBTYtsl673O3lqSA=
|
||||
|
||||
26
message.go
Normal file
26
message.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package nano
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/FloatTech/ttl"
|
||||
)
|
||||
|
||||
var (
|
||||
triggeredMessages = ttl.NewCache[string, []string](time.Minute * 5)
|
||||
triggeredMessagesMu = sync.Mutex{}
|
||||
)
|
||||
|
||||
func logtriggeredmessages(id, reply string) {
|
||||
triggeredMessagesMu.Lock()
|
||||
defer triggeredMessagesMu.Unlock()
|
||||
triggeredMessages.Set(id, append(triggeredMessages.Get(id), reply))
|
||||
}
|
||||
|
||||
// GetTriggeredMessages 获取被 id 消息触发的回复消息 id
|
||||
func GetTriggeredMessages(id string) []string {
|
||||
triggeredMessagesMu.Lock()
|
||||
defer triggeredMessagesMu.Unlock()
|
||||
return triggeredMessages.Get(id)
|
||||
}
|
||||
Reference in New Issue
Block a user