Skip to content

Commit

Permalink
enhance something of message's chain (#18)
Browse files Browse the repository at this point in the history
* enhance priority's setting

* add chain-message's sender

* add a key&val appender in MessageSegment

* enhance MessageSegment Text builder
  • Loading branch information
kanrichan committed Apr 8, 2021
1 parent 7cb1b23 commit 1273221
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
9 changes: 9 additions & 0 deletions contex.go
Expand Up @@ -6,6 +6,7 @@ import (
"unsafe"

"github.com/modern-go/reflect2"
"github.com/wdvxdr1123/ZeroBot/message"
)

// Ctx represents the Context which hold the event.
Expand Down Expand Up @@ -90,6 +91,14 @@ func (ctx *Ctx) Send(message interface{}) int64 {
return ctx.SendPrivateMessage(ctx.Event.UserID, message)
}

// SendChain 快捷发送消息-消息链
func (ctx *Ctx) SendChain(message ...message.MessageSegment) int64 {
if ctx.Event.GroupID != 0 {
return ctx.SendGroupMessage(ctx.Event.GroupID, message)
}
return ctx.SendPrivateMessage(ctx.Event.UserID, message)
}

// FutureEvent ...
func (ctx *Ctx) FutureEvent(Type string, rule ...Rule) *FutureEvent {
return ctx.ma.FutureEvent(Type, rule...)
Expand Down
15 changes: 15 additions & 0 deletions matcher.go
Expand Up @@ -63,6 +63,21 @@ func (m *Matcher) SetPriority(priority int) *Matcher {
return m
}

// FirstPriority 设置当前 Matcher 优先级 - 0
func (m *Matcher) FirstPriority() *Matcher {
return m.SetPriority(0)
}

// SecondPriority 设置当前 Matcher 优先级 - 1
func (m *Matcher) SecondPriority() *Matcher {
return m.SetPriority(1)
}

// ThirdPriority 设置当前 Matcher 优先级 - 2
func (m *Matcher) ThirdPriority() *Matcher {
return m.SetPriority(2)
}

// StoreMatcher store a matcher to matcher list.
func StoreMatcher(m *Matcher) *Matcher {
matcherLock.Lock()
Expand Down
22 changes: 14 additions & 8 deletions message/message.go
@@ -1,6 +1,7 @@
package message

import (
"fmt"
"strconv"
"strings"

Expand Down Expand Up @@ -75,11 +76,11 @@ func (m MessageSegment) CQCode() string {

// Text 纯文本
// https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#%E7%BA%AF%E6%96%87%E6%9C%AC
func Text(text string) MessageSegment {
func Text(text ...interface{}) MessageSegment {
return MessageSegment{
Type: "text",
Data: map[string]string{
"text": text,
"text": fmt.Sprint(text...),
},
}
}
Expand Down Expand Up @@ -142,15 +143,14 @@ func Music(mType string, id int64) MessageSegment {

// CustomMusic 音乐自定义分享
// https://github.com/howmanybots/onebot/blob/master/v11/specs/message/segment.md#%E9%9F%B3%E4%B9%90%E8%87%AA%E5%AE%9A%E4%B9%89%E5%88%86%E4%BA%AB-
func CustomMusic(subType, url, audio, title string) MessageSegment {
func CustomMusic(url, audio, title string) MessageSegment {
return MessageSegment{
Type: "music",
Data: map[string]string{
"type": "custom",
"sub_type": subType,
"url": url,
"audio": audio,
"title": title,
"type": "custom",
"url": url,
"audio": audio,
"title": title,
},
}
}
Expand Down Expand Up @@ -267,6 +267,12 @@ func TTS(text string) MessageSegment {
}
}

// Add 为 MessageSegment 的 Data 增加一个字段
func (m MessageSegment) Add(key string, val interface{}) MessageSegment {
m.Data[key] = fmt.Sprint(val)
return m
}

// ReplyWithMessage returns a reply message
func ReplyWithMessage(messageID int64, m ...MessageSegment) Message {
return append(Message{Reply(messageID)}, m...)
Expand Down

0 comments on commit 1273221

Please sign in to comment.