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

optimize: メッセージ送信機能

This commit is contained in:
源文雨
2023-10-17 23:34:07 +09:00
parent d82564e798
commit 9c5a9796e1
4 changed files with 50 additions and 23 deletions

2
bot.go
View File

@@ -153,7 +153,7 @@ func (bot *Bot) Authorization() string {
return "Bot " + bot.AppID + "." + bot.Token
}
// AtMe 返回 "<@!"+bot.ready.User.ID+">"
// AtMe 返回 <@!bot.ready.User.ID>
func (bot *Bot) AtMe() string {
return "<@!" + bot.ready.User.ID + ">"
}

View File

@@ -75,13 +75,18 @@ func (ctx *Ctx) CheckSession() Rule {
// Send 发送消息到对方
func (ctx *Ctx) Send(replytosender bool, post *MessagePost) (*Message, error) {
msg := ctx.Value.(*Message)
post.ReplyMessageID = msg.ID
if replytosender {
post.MessageReference = &MessageReference{
MessageID: msg.ID,
msg, ok := ctx.Value.(*Message)
if ok && msg != nil {
post.ReplyMessageID = msg.ID
if replytosender {
post.MessageReference = &MessageReference{
MessageID: msg.ID,
}
}
} else {
post.ReplyMessageID = "MESSAGE_CREATE"
}
if msg.SrcGuildID != "" { // dms
return ctx.Caller.PostMessageToUser(msg.GuildID, post)
}
@@ -90,16 +95,20 @@ func (ctx *Ctx) Send(replytosender bool, post *MessagePost) (*Message, error) {
// SendPlainMessage 发送纯文本消息到对方
func (ctx *Ctx) SendPlainMessage(replytosender bool, printable ...any) (*Message, error) {
msg := ctx.Value.(*Message)
post := &MessagePost{
ReplyMessageID: msg.ID,
}
if replytosender {
post.MessageReference = &MessageReference{
MessageID: msg.ID,
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 = fmt.Sprint(printable...)
post.Content = HideURL(fmt.Sprint(printable...))
if msg.SrcGuildID != "" { // dms
return ctx.Caller.PostMessageToUser(msg.GuildID, post)
}
@@ -108,21 +117,26 @@ func (ctx *Ctx) SendPlainMessage(replytosender bool, printable ...any) (*Message
// SendImage 发送带图片消息到对方
func (ctx *Ctx) SendImage(file string, replytosender bool, caption ...any) (*Message, error) {
msg := ctx.Value.(*Message)
post := &MessagePost{
ReplyMessageID: msg.ID,
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"
}
if strings.HasPrefix(file, "http") {
post.Image = file
} else {
post.ImageFile = file
}
if replytosender {
post.MessageReference = &MessageReference{
MessageID: msg.ID,
}
}
post.Content = fmt.Sprint(caption...)
post.Content = HideURL(fmt.Sprint(caption...))
if msg.SrcGuildID != "" { // dms
return ctx.Caller.PostMessageToUser(msg.GuildID, post)
}

View File

@@ -63,6 +63,14 @@ func MessageUnescape(text string) string {
return text
}
// HideURL 转义 URL 以避免审核
func HideURL(s string) string {
s = strings.ReplaceAll(s, ".", "…")
s = strings.ReplaceAll(s, "http://", "🔗📄:")
s = strings.ReplaceAll(s, "https://", "🔗🔒:")
return s
}
// UnderlineToCamel convert abc_def to AbcDef
func UnderlineToCamel(s string) string {
sb := strings.Builder{}

View File

@@ -12,6 +12,11 @@ type User struct {
UnionUserAccount string `json:"union_user_account"`
}
// At 返回 <@!u.ID>
func (u *User) At() string {
return "<@!" + u.ID + ">"
}
// GetMyInfo 获取当前用户(机器人)详情
//
// https://bot.q.qq.com/wiki/develop/api/openapi/user/me.html