github.com/Mrs4s/MiraiGo@v0.0.0-20240226124653-54bdd873e3fe/client/notify.go (about)

     1  package client
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/Mrs4s/MiraiGo/utils"
    10  
    11  	"github.com/Mrs4s/MiraiGo/client/pb/notify"
    12  )
    13  
    14  type (
    15  	// GroupPokeNotifyEvent 群内戳一戳提示事件
    16  	GroupPokeNotifyEvent struct {
    17  		GroupCode int64
    18  		Sender    int64
    19  		Receiver  int64
    20  	}
    21  
    22  	// GroupRedBagLuckyKingNotifyEvent 群内抢红包运气王提示事件
    23  	GroupRedBagLuckyKingNotifyEvent struct {
    24  		GroupCode int64
    25  		Sender    int64
    26  		LuckyKing int64
    27  	}
    28  
    29  	// MemberHonorChangedNotifyEvent 群成员荣誉变更提示事件
    30  	MemberHonorChangedNotifyEvent struct {
    31  		GroupCode int64
    32  		Honor     HonorType
    33  		Uin       int64
    34  		Nick      string
    35  	}
    36  
    37  	// MemberSpecialTitleUpdatedEvent 群成员头衔更新事件
    38  	MemberSpecialTitleUpdatedEvent struct {
    39  		GroupCode int64
    40  		Uin       int64
    41  		NewTitle  string
    42  	}
    43  
    44  	// FriendPokeNotifyEvent 好友戳一戳提示事件
    45  	FriendPokeNotifyEvent struct {
    46  		Sender   int64
    47  		Receiver int64
    48  	}
    49  )
    50  
    51  // grayTipProcessor 提取出来专门用于处理群内 notify tips
    52  func (c *QQClient) grayTipProcessor(groupCode int64, tipInfo *notify.GeneralGrayTipInfo) {
    53  	if tipInfo.BusiType == 12 && tipInfo.BusiId == 1061 {
    54  		sender := int64(0)
    55  		receiver := c.Uin
    56  		for _, templ := range tipInfo.MsgTemplParam {
    57  			if templ.Name == "uin_str1" {
    58  				sender, _ = strconv.ParseInt(templ.Value, 10, 64)
    59  			}
    60  			if templ.Name == "uin_str2" {
    61  				receiver, _ = strconv.ParseInt(templ.Value, 10, 64)
    62  			}
    63  		}
    64  		if sender != 0 {
    65  			c.GroupNotifyEvent.dispatch(c, &GroupPokeNotifyEvent{
    66  				GroupCode: groupCode,
    67  				Sender:    sender,
    68  				Receiver:  receiver,
    69  			})
    70  		}
    71  	}
    72  	switch tipInfo.TemplId {
    73  	case 1052, 1053, 1054, 1067: // 群荣誉
    74  		var nick string
    75  		var uin int64
    76  		for _, templ := range tipInfo.MsgTemplParam {
    77  			if templ.Name == "nick" {
    78  				nick = templ.Value
    79  			}
    80  			if templ.Name == "uin" {
    81  				uin, _ = strconv.ParseInt(templ.Value, 10, 64)
    82  			}
    83  		}
    84  		c.GroupNotifyEvent.dispatch(c, &MemberHonorChangedNotifyEvent{
    85  			GroupCode: groupCode,
    86  			Honor: func() HonorType {
    87  				switch tipInfo.TemplId {
    88  				case 1052:
    89  					return Performer
    90  				case 1053, 1054:
    91  					return Talkative
    92  				case 1067:
    93  					return Emotion
    94  				default:
    95  					return 0
    96  				}
    97  			}(),
    98  			Uin:  uin,
    99  			Nick: nick,
   100  		})
   101  	}
   102  }
   103  
   104  // msgGrayTipProcessor 用于处理群内 aio notify tips
   105  func (c *QQClient) msgGrayTipProcessor(groupCode int64, tipInfo *notify.AIOGrayTipsInfo) {
   106  	if len(tipInfo.Content) == 0 {
   107  		return
   108  	}
   109  	type tipCommand struct {
   110  		Command int    `json:"cmd"`
   111  		Data    string `json:"data"`
   112  		Text    string `json:"text"`
   113  	}
   114  	content := utils.B2S(tipInfo.Content)
   115  	var tipCmds []*tipCommand
   116  	start := -1
   117  	for i := 0; i < len(content); i++ {
   118  		if content[i] == '<' && len(content) > i+1 && content[i+1] == '{' {
   119  			start = i + 1
   120  		}
   121  		if content[i] == '>' && content[i-1] == '}' && start != -1 {
   122  			tip := &tipCommand{}
   123  			if err := json.Unmarshal(utils.S2B(content[start:i]), tip); err == nil {
   124  				tipCmds = append(tipCmds, tip)
   125  			}
   126  			start = -1
   127  		}
   128  	}
   129  	// 好像只能这么判断
   130  	if strings.Contains(content, "头衔") {
   131  		event := &MemberSpecialTitleUpdatedEvent{GroupCode: groupCode}
   132  		for _, cmd := range tipCmds {
   133  			if cmd.Command == 5 {
   134  				event.Uin, _ = strconv.ParseInt(cmd.Data, 10, 64)
   135  			}
   136  			if cmd.Command == 1 {
   137  				event.NewTitle = cmd.Text
   138  			}
   139  		}
   140  		if event.Uin == 0 {
   141  			c.error("process special title updated tips error: missing cmd")
   142  			return
   143  		}
   144  		if mem := c.FindGroup(groupCode).FindMember(event.Uin); mem != nil {
   145  			mem.SpecialTitle = event.NewTitle
   146  		}
   147  		c.MemberSpecialTitleUpdatedEvent.dispatch(c, event)
   148  	}
   149  }
   150  
   151  func (e *GroupPokeNotifyEvent) From() int64 {
   152  	return e.GroupCode
   153  }
   154  
   155  func (e *GroupPokeNotifyEvent) Content() string {
   156  	return fmt.Sprintf("%d戳了戳%d", e.Sender, e.Receiver)
   157  }
   158  
   159  func (e *FriendPokeNotifyEvent) From() int64 {
   160  	return e.Sender
   161  }
   162  
   163  func (e *FriendPokeNotifyEvent) Content() string {
   164  	return fmt.Sprintf("%d戳了戳%d", e.Sender, e.Receiver)
   165  }
   166  
   167  func (e *GroupRedBagLuckyKingNotifyEvent) From() int64 {
   168  	return e.GroupCode
   169  }
   170  
   171  func (e *GroupRedBagLuckyKingNotifyEvent) Content() string {
   172  	return fmt.Sprintf("%d发的红包被领完, %d是运气王", e.Sender, e.LuckyKing)
   173  }
   174  
   175  func (e *MemberHonorChangedNotifyEvent) From() int64 {
   176  	return e.GroupCode
   177  }
   178  
   179  func (e *MemberHonorChangedNotifyEvent) Content() string {
   180  	switch e.Honor {
   181  	case Talkative:
   182  		return fmt.Sprintf("昨日 %s(%d) 在群 %d 内发言最积极, 获得 龙王 标识。", e.Nick, e.Uin, e.GroupCode)
   183  	case Performer:
   184  		return fmt.Sprintf("%s(%d) 在群 %d 里连续发消息超过7天, 获得 群聊之火 标识。", e.Nick, e.Uin, e.GroupCode)
   185  	case Emotion:
   186  		return fmt.Sprintf("%s(%d) 在群聊 %d 中连续发表情包超过3天,且累计数量超过20条,获得 快乐源泉 标识。", e.Nick, e.Uin, e.GroupCode)
   187  	default:
   188  		return "ERROR"
   189  	}
   190  }