1
0
mirror of https://github.com/fumiama/ReiBot.git synced 2026-06-05 00:50:25 +08:00

优化代码结构

This commit is contained in:
源文雨
2022-05-31 23:12:43 +08:00
parent e37701840d
commit b2d1f0c249
3 changed files with 22 additions and 7 deletions

View File

@@ -2,7 +2,9 @@ package rei
import (
"log"
"reflect"
"time"
"unsafe"
tgba "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
@@ -16,6 +18,19 @@ type TelegramClient struct {
// NewTelegramClient ...
func NewTelegramClient(c *Bot) (tc TelegramClient) {
tc.b = *c
h := reflect.ValueOf(&tc.b.Handler).Elem()
t := h.Type()
tc.b.handlers = make(map[string]GeneralHandleType, 16)
for i := 0; i < h.NumField(); i++ {
f := h.Field(i)
if f.IsZero() {
continue
}
tp := t.Field(i).Name[2:]
log.Println("[INFO] register handler", tp)
handler := f.Interface()
tc.b.handlers[tp] = *(*GeneralHandleType)(unsafe.Add(unsafe.Pointer(&handler), unsafe.Sizeof(uintptr(0))))
}
return
}