forked from fumiama/NanoBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
129 lines (110 loc) · 2.92 KB
/
message.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package nano
import (
"fmt"
"strconv"
"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)
}
type MessageType int
const (
MessageTypeText MessageType = iota
MessageTypeImage
MessageTypeImageBytes
MessageTypeReply
)
// Message impl the array form of message
type Messages []MessageSegment
// MessageSegment impl the single message
// MessageSegment 消息数组
type MessageSegment struct {
Type MessageType
Data string
}
// String impls the interface fmt.Stringer
func (m MessageSegment) String() string {
return m.Data
}
// Text 纯文本
func Text(text ...interface{}) MessageSegment {
return MessageSegment{
Type: MessageTypeText,
Data: HideURL(MessageEscape(fmt.Sprint(text...))),
}
}
// Face QQ表情
// https://bot.q.qq.com/wiki/develop/api/openapi/message/message_format.html#%E6%94%AF%E6%8C%81%E7%9A%84%E6%A0%BC%E5%BC%8F
func Face(id int) MessageSegment {
return MessageSegment{
Type: MessageTypeText,
Data: "<emoji:" + strconv.Itoa(id) + ">",
}
}
// Image 普通图片
func Image(file string) MessageSegment {
return MessageSegment{
Type: MessageTypeImage,
Data: file,
}
}
// ImageBytes 普通图片
func ImageBytes(data []byte) MessageSegment {
return MessageSegment{
Type: MessageTypeImageBytes,
Data: BytesToString(data),
}
}
// At @某人
// https://bot.q.qq.com/wiki/develop/api/openapi/message/message_format.html#%E6%94%AF%E6%8C%81%E7%9A%84%E6%A0%BC%E5%BC%8F
func At(id string) MessageSegment {
if id == "all" {
return AtAll()
}
return MessageSegment{
Type: MessageTypeText,
Data: "<@!" + id + ">",
}
}
// AtAll @全体成员
// https://bot.q.qq.com/wiki/develop/api/openapi/message/message_format.html#%E6%94%AF%E6%8C%81%E7%9A%84%E6%A0%BC%E5%BC%8F
func AtAll() MessageSegment {
return MessageSegment{
Type: MessageTypeText,
Data: "@everyone",
}
}
// AtChannel #频道
// https://bot.q.qq.com/wiki/develop/api/openapi/message/message_format.html#%E6%94%AF%E6%8C%81%E7%9A%84%E6%A0%BC%E5%BC%8F
func AtChannel(id string) MessageSegment {
return MessageSegment{
Type: MessageTypeText,
Data: "<#channel_id>",
}
}
// Reply 回复
// https://github.com/botuniverse/onebot-11/tree/master/message/segment.md#%E5%9B%9E%E5%A4%8D
func ReplyTo(id string) MessageSegment {
return MessageSegment{
Type: MessageTypeReply,
Data: id,
}
}
// ReplyWithMessage returns a reply message
func ReplyWithMessage(messageID string, m ...MessageSegment) Messages {
return append(Messages{ReplyTo(messageID)}, m...)
}