diff --git a/bot.go b/bot.go index 64c9b4a..1d9e6da 100644 --- a/bot.go +++ b/bot.go @@ -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 + ">" } diff --git a/context.go b/context.go index cf6e90a..e3c43d3 100644 --- a/context.go +++ b/context.go @@ -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) } diff --git a/helper.go b/helper.go index 8127ded..e0e7d7b 100644 --- a/helper.go +++ b/helper.go @@ -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{} diff --git a/openapi_user.go b/openapi_user.go index 47a9c05..25b5d51 100644 --- a/openapi_user.go +++ b/openapi_user.go @@ -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