mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-05 03:23:49 +08:00
Closes #556
This commit is contained in:
parent
b781c3b43b
commit
626de1cb17
@ -438,12 +438,12 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupID int64, m gjson.Result) MSG {
|
|||||||
// CQSendPrivateMessage 发送私聊消息
|
// CQSendPrivateMessage 发送私聊消息
|
||||||
//
|
//
|
||||||
// https://git.io/Jtz1l
|
// https://git.io/Jtz1l
|
||||||
func (bot *CQBot) CQSendPrivateMessage(userID int64, i interface{}, autoEscape bool) MSG {
|
func (bot *CQBot) CQSendPrivateMessage(userID int64, groupId int64, i interface{}, autoEscape bool) MSG {
|
||||||
var str string
|
var str string
|
||||||
if m, ok := i.(gjson.Result); ok {
|
if m, ok := i.(gjson.Result); ok {
|
||||||
if m.Type == gjson.JSON {
|
if m.Type == gjson.JSON {
|
||||||
elem := bot.ConvertObjectMessage(m, false)
|
elem := bot.ConvertObjectMessage(m, false)
|
||||||
mid := bot.SendPrivateMessage(userID, &message.SendingMessage{Elements: elem})
|
mid := bot.SendPrivateMessage(userID, groupId, &message.SendingMessage{Elements: elem})
|
||||||
if mid == -1 {
|
if mid == -1 {
|
||||||
return Failed(100, "SEND_MSG_API_ERROR", "请参考输出")
|
return Failed(100, "SEND_MSG_API_ERROR", "请参考输出")
|
||||||
}
|
}
|
||||||
@ -468,7 +468,7 @@ func (bot *CQBot) CQSendPrivateMessage(userID int64, i interface{}, autoEscape b
|
|||||||
} else {
|
} else {
|
||||||
elem = bot.ConvertStringMessage(str, false)
|
elem = bot.ConvertStringMessage(str, false)
|
||||||
}
|
}
|
||||||
mid := bot.SendPrivateMessage(userID, &message.SendingMessage{Elements: elem})
|
mid := bot.SendPrivateMessage(userID, groupId, &message.SendingMessage{Elements: elem})
|
||||||
if mid == -1 {
|
if mid == -1 {
|
||||||
return Failed(100, "SEND_MSG_API_ERROR", "请参考输出")
|
return Failed(100, "SEND_MSG_API_ERROR", "请参考输出")
|
||||||
}
|
}
|
||||||
@ -874,7 +874,7 @@ func (bot *CQBot) CQHandleQuickOperation(context, operation gjson.Result) MSG {
|
|||||||
bot.CQSendGroupMessage(context.Get("group_id").Int(), reply, autoEscape)
|
bot.CQSendGroupMessage(context.Get("group_id").Int(), reply, autoEscape)
|
||||||
}
|
}
|
||||||
if msgType == "private" {
|
if msgType == "private" {
|
||||||
bot.CQSendPrivateMessage(context.Get("user_id").Int(), reply, autoEscape)
|
bot.CQSendPrivateMessage(context.Get("user_id").Int(), context.Get("group_id").Int(), reply, autoEscape)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if msgType == "group" {
|
if msgType == "group" {
|
||||||
|
23
coolq/bot.go
23
coolq/bot.go
@ -240,7 +240,7 @@ func (bot *CQBot) SendGroupMessage(groupID int64, m *message.SendingMessage) int
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SendPrivateMessage 发送私聊消息
|
// SendPrivateMessage 发送私聊消息
|
||||||
func (bot *CQBot) SendPrivateMessage(target int64, m *message.SendingMessage) int32 {
|
func (bot *CQBot) SendPrivateMessage(target int64, groupId int64, m *message.SendingMessage) int32 {
|
||||||
var newElem []message.IMessageElement
|
var newElem []message.IMessageElement
|
||||||
for _, elem := range m.Elements {
|
for _, elem := range m.Elements {
|
||||||
if i, ok := elem.(*LocalImageElement); ok {
|
if i, ok := elem.(*LocalImageElement); ok {
|
||||||
@ -292,16 +292,33 @@ func (bot *CQBot) SendPrivateMessage(target int64, m *message.SendingMessage) in
|
|||||||
if msg != nil {
|
if msg != nil {
|
||||||
id = bot.InsertPrivateMessage(msg)
|
id = bot.InsertPrivateMessage(msg)
|
||||||
}
|
}
|
||||||
} else if code, ok := bot.tempMsgCache.Load(target); ok { // 临时会话
|
} else if code, ok := bot.tempMsgCache.Load(target); ok || groupId != 0 { // 临时会话
|
||||||
msg := bot.Client.SendTempMessage(code.(int64), target, m)
|
if groupId != 0 && !bot.Client.FindGroup(groupId).AdministratorOrOwner() {
|
||||||
|
log.Errorf("错误: 机器人在群(%v) 为非管理员或群主, 无法主动发起临时会话", groupId)
|
||||||
|
id = -1
|
||||||
|
} else if groupId != 0 && bot.Client.FindGroup(groupId).FindMember(target) == nil {
|
||||||
|
log.Errorf("错误: 群员(%v) 不在 群(%v), 无法发起临时会话", target, groupId)
|
||||||
|
id = -1
|
||||||
|
} else {
|
||||||
|
if code != nil {
|
||||||
|
groupId = code.(int64)
|
||||||
|
}
|
||||||
|
msg := bot.Client.SendTempMessage(groupId, target, m)
|
||||||
if msg != nil {
|
if msg != nil {
|
||||||
id = bot.InsertTempMessage(target, msg)
|
id = bot.InsertTempMessage(target, msg)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else if _, ok := bot.oneWayMsgCache.Load(target); ok { // 单向好友
|
} else if _, ok := bot.oneWayMsgCache.Load(target); ok { // 单向好友
|
||||||
msg := bot.Client.SendPrivateMessage(target, m)
|
msg := bot.Client.SendPrivateMessage(target, m)
|
||||||
if msg != nil {
|
if msg != nil {
|
||||||
id = bot.InsertPrivateMessage(msg)
|
id = bot.InsertPrivateMessage(msg)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
nickname := "Unknown"
|
||||||
|
if summaryInfo, _ := bot.Client.GetSummaryInfo(target); summaryInfo != nil {
|
||||||
|
nickname = summaryInfo.Nickname
|
||||||
|
}
|
||||||
|
log.Errorf("错误: 请先添加 %v(%v) 为好友", nickname, target)
|
||||||
}
|
}
|
||||||
if id == -1 {
|
if id == -1 {
|
||||||
return -1
|
return -1
|
||||||
|
@ -45,17 +45,17 @@ func getGroupMemberInfo(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
|||||||
func sendMSG(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
func sendMSG(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||||
autoEscape := global.EnsureBool(p.Get("auto_escape"), false)
|
autoEscape := global.EnsureBool(p.Get("auto_escape"), false)
|
||||||
if p.Get("message_type").Str == "private" {
|
if p.Get("message_type").Str == "private" {
|
||||||
return bot.CQSendPrivateMessage(p.Get("user_id").Int(), p.Get("message"), autoEscape)
|
return bot.CQSendPrivateMessage(p.Get("user_id").Int(), p.Get("group_id").Int(), p.Get("message"), autoEscape)
|
||||||
}
|
}
|
||||||
if p.Get("message_type").Str == "group" {
|
if p.Get("message_type").Str == "group" {
|
||||||
return bot.CQSendGroupMessage(p.Get("group_id").Int(), p.Get("message"), autoEscape)
|
return bot.CQSendGroupMessage(p.Get("group_id").Int(), p.Get("message"), autoEscape)
|
||||||
}
|
}
|
||||||
|
if p.Get("user_id").Int() != 0 {
|
||||||
|
return bot.CQSendPrivateMessage(p.Get("user_id").Int(), p.Get("group_id").Int(), p.Get("message"), autoEscape)
|
||||||
|
}
|
||||||
if p.Get("group_id").Int() != 0 {
|
if p.Get("group_id").Int() != 0 {
|
||||||
return bot.CQSendGroupMessage(p.Get("group_id").Int(), p.Get("message"), autoEscape)
|
return bot.CQSendGroupMessage(p.Get("group_id").Int(), p.Get("message"), autoEscape)
|
||||||
}
|
}
|
||||||
if p.Get("user_id").Int() != 0 {
|
|
||||||
return bot.CQSendPrivateMessage(p.Get("user_id").Int(), p.Get("message"), autoEscape)
|
|
||||||
}
|
|
||||||
return coolq.MSG{}
|
return coolq.MSG{}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,7 +69,7 @@ func sendGroupForwardMSG(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sendPrivateMSG(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
func sendPrivateMSG(bot *coolq.CQBot, p resultGetter) coolq.MSG {
|
||||||
return bot.CQSendPrivateMessage(p.Get("user_id").Int(), p.Get("message"),
|
return bot.CQSendPrivateMessage(p.Get("user_id").Int(), p.Get("group_id").Int(), p.Get("message"),
|
||||||
global.EnsureBool(p.Get("auto_escape"), false))
|
global.EnsureBool(p.Get("auto_escape"), false))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user