Skip to content

Commit

Permalink
添加部分代码注释
Browse files Browse the repository at this point in the history
  • Loading branch information
huoxue1 committed Jun 24, 2021
1 parent 9cba41c commit d6fa787
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 117 deletions.
122 changes: 11 additions & 111 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var (

// sessionHandle
/*
@Description:
@Description: 遍历所有的session发现是否需要将消息传到对应的handle里面
@param event Event
*/
func sessionHandle(event Event) {
Expand All @@ -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, &noticeHandle{
// 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)
Expand Down Expand Up @@ -230,7 +126,7 @@ func eventMain() {

// GetOneEvent
/*
@Description:
@Description: 向session队列里面添加一个对象,等待用户的响应,设置超时时间
@receiver b
@param rules ...Rule
@return Event Event
Expand All @@ -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{
Expand All @@ -273,7 +169,7 @@ func (b *Bot) GetMoreEvent(rules ...Rule) (int, chan Event) {

// CloseMessageChan
/*
@Description:
@Description: 关闭session,即从等待队列中删除
@receiver b
@param id int
*/
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
14 changes: 8 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
//nolint:gci
)

// 一个ws连接对象
type connection struct {
SelfID int
wsSocket *websocket.Conn // 底层websocket
Expand All @@ -22,14 +23,15 @@ type connection struct {
closeChan chan byte // 关闭通知
}

// ws协议
var upgrade = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
}}

// readData
/*
@Description:
@Description: ws连接读循环
@receiver con
*/
func (con *connection) readData() {
Expand All @@ -52,7 +54,7 @@ func (con *connection) readData() {

// FilterEventOrResponse
/*
@Description:
@Description: 从ws的读队列分别过滤是事件还是api响应
@receiver con
*/
func (con *connection) FilterEventOrResponse() {
Expand All @@ -71,7 +73,7 @@ func (con *connection) FilterEventOrResponse() {

// writeData
/*
@Description:
@Description: 从ws写队列死循环写入数据
@receiver con
*/
func (con *connection) writeData() {
Expand All @@ -93,7 +95,7 @@ func (con *connection) writeData() {

// wsClose
/*
@Description:
@Description: 关闭ws连接
@receiver con
*/
func (con *connection) wsClose() {
Expand All @@ -113,7 +115,7 @@ func (con *connection) wsClose() {

// EventHandle
/*
@Description:
@Description: ws的httphandle
@param w http.ResponseWriter
@param r *http.Request
*/
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions utils/Log_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d6fa787

Please sign in to comment.