diff --git a/event.go b/event.go index 5e73137..89564ab 100644 --- a/event.go +++ b/event.go @@ -53,7 +53,7 @@ var ( // sessionHandle /* - @Description: + @Description: 遍历所有的session发现是否需要将消息传到对应的handle里面 @param event Event */ func sessionHandle(event Event) { @@ -78,113 +78,9 @@ type ( } ) -// AddMessageHandle -/* - @Description: - @param messageType string - @param rules []Rule - @param handles ...func(event Event, bot *Bot) -*/ -//func AddMessageHandle(messageType string, rules []Rule, handles ...func(event Event, bot *Bot)) { -// for _, handle := range handles { -// MessageHandles = append(MessageHandles, &messageHandle{ -// handle: handle, -// messageType: messageType, -// rules: rules, -// }) -// } -// -//} -// -//func AddPretreatmentHandle(rules []Rule, weight int, handles ...func(event Event, bot *Bot) bool) { -// for _, handle := range handles { -// PretreatmentHandles = append(PretreatmentHandles, &PretreatmentHandle{ -// handle: handle, -// rules: rules, -// weight: weight, -// }) -// } -//} -// -//// AddNoticeHandle -///* -// @Description: -// @param noticeType string -// @param rules []Rule -// @param weight int -// @param handles ...func(event Event, bot *Bot) -//*/ -//func AddNoticeHandle(noticeType string, rules []Rule, weight int, handles ...func(event Event, bot *Bot)) { -// for _, handle := range handles { -// NoticeHandles = append(NoticeHandles, ¬iceHandle{ -// handle: handle, -// noticeType: noticeType, -// rules: rules, -// weight: weight, -// }) -// } -//} -// -//// AddRequestHandle -///* -// @Description: -// @param requestType string -// @param rules []Rule -// @param weight int -// @param handles ...func(event Event, bot *Bot) -//*/ -//func AddRequestHandle(requestType string, rules []Rule, weight int, handles ...func(event Event, bot *Bot)) { -// for _, handle := range handles { -// RequestHandles = append(RequestHandles, &requestHandle{ -// handle: handle, -// requestType: requestType, -// rules: rules, -// weight: weight, -// }) -// } -//} -// -//// AddMetaHandles -///* -// @Description: -// @param rules []Rule -// @param weight int -// @param handles ...func(event Event, bot *Bot) -//*/ -//func AddMetaHandles(rules []Rule, weight int, handles ...func(event Event, bot *Bot)) { -// for _, handle := range handles { -// MetaHandles = append(MetaHandles, &metaHandle{ -// handle: handle, -// rules: rules, -// weight: weight, -// }) -// } -//} -// -//// AddCommandHandle -///* -// @Description: -// @param handle func(event Event, bot *Bot, args []string) -// @param command string -// @param allies []string -// @param rules []Rule -// @param weight int -// @param block bool -//*/ -//func AddCommandHandle(handle func(event Event, bot *Bot, args []string), command string, allies []string, rules []Rule, weight int, block bool) { -// CommandHandles = append(CommandHandles, &commandHandle{ -// handle: handle, -// command: command, -// allies: allies, -// rules: rules, -// weight: weight, -// block: block, -// }) -//} - // eventMain /* - @Description: + @Description: 事件总处理器,所有的事件都从这里开始处理 */ func eventMain() { sort.Sort(&MessageHandles) @@ -230,7 +126,7 @@ func eventMain() { // GetOneEvent /* - @Description: + @Description: 向session队列里面添加一个对象,等待用户的响应,设置超时时间 @receiver b @param rules ...Rule @return Event Event @@ -255,11 +151,11 @@ func (b *Bot) GetOneEvent(rules ...Rule) (Event, error) { // GetMoreEvent /* - @Description: + @Description: 获取一个通道不断从用户获取消息 @receiver b @param rules ...Rule - @return int int - @return chan Event + @return int int 对应session在队列中的编号,后面关闭需要该编号 + @return chan Event 事件通道 */ func (b *Bot) GetMoreEvent(rules ...Rule) (int, chan Event) { s := session{ @@ -273,7 +169,7 @@ func (b *Bot) GetMoreEvent(rules ...Rule) (int, chan Event) { // CloseMessageChan /* - @Description: + @Description: 关闭session,即从等待队列中删除 @receiver b @param id int */ @@ -434,8 +330,10 @@ func processMessageHandle() { event := <-c a := 0 log.Debugln(len(CommandHandles)) + // 遍历所有的command对象 for _, handle := range CommandHandles { + // 判断该cmd在该群是否被禁用 disable := true for _, group := range handle.disableGroup { if event.GroupId == group { @@ -446,10 +344,12 @@ func processMessageHandle() { continue } + // 检查rules rule := checkRule(event, handle.rules) if handle.rules == nil { rule = true } + // 判断rules和插件是否被禁用 if !rule || !handle.Enable { continue } diff --git a/init.go b/init.go index 1a6a00f..c764f89 100644 --- a/init.go +++ b/init.go @@ -157,6 +157,7 @@ func initConfig(path string, fileType string) error { hook.AddLevel(utils.GetLogLevel(DefaultConfig.LogLevel)...) log.Infoln("已加载配置:" + string(data)) + log.SetLevel(log.DebugLevel) return err } diff --git a/server.go b/server.go index aeec1dc..3381234 100644 --- a/server.go +++ b/server.go @@ -11,6 +11,7 @@ import ( //nolint:gci ) +// 一个ws连接对象 type connection struct { SelfID int wsSocket *websocket.Conn // 底层websocket @@ -22,6 +23,7 @@ type connection struct { closeChan chan byte // 关闭通知 } +// ws协议 var upgrade = websocket.Upgrader{ CheckOrigin: func(r *http.Request) bool { return true @@ -29,7 +31,7 @@ var upgrade = websocket.Upgrader{ // readData /* - @Description: + @Description: ws连接读循环 @receiver con */ func (con *connection) readData() { @@ -52,7 +54,7 @@ func (con *connection) readData() { // FilterEventOrResponse /* - @Description: + @Description: 从ws的读队列分别过滤是事件还是api响应 @receiver con */ func (con *connection) FilterEventOrResponse() { @@ -71,7 +73,7 @@ func (con *connection) FilterEventOrResponse() { // writeData /* - @Description: + @Description: 从ws写队列死循环写入数据 @receiver con */ func (con *connection) writeData() { @@ -93,7 +95,7 @@ func (con *connection) writeData() { // wsClose /* - @Description: + @Description: 关闭ws连接 @receiver con */ func (con *connection) wsClose() { @@ -113,7 +115,7 @@ func (con *connection) wsClose() { // EventHandle /* - @Description: + @Description: ws的httphandle @param w http.ResponseWriter @param r *http.Request */ @@ -153,7 +155,7 @@ func EventHandle(w http.ResponseWriter, r *http.Request) { // getResponse /* - @Description: + @Description: 死循环根据echo获取api响应 @param r *response @param echo string @return []byte diff --git a/utils/Log_hook.go b/utils/Log_hook.go index 7f79d73..c5a3c69 100644 --- a/utils/Log_hook.go +++ b/utils/Log_hook.go @@ -74,6 +74,7 @@ func (l *LogHook) AddWriter(writer ...io.Writer) { func (l *LogHook) AddLevel(level ...log.Level) { l.levels = append(l.levels, level...) + } func NewLogHook(formatter log.Formatter, levels []log.Level, writers ...io.Writer) *LogHook {