mirror of
https://github.com/Mrs4s/MiraiGo.git
synced 2025-05-04 11:07:40 +08:00
feat: subscribable event handler
This commit is contained in:
parent
a4cf95f793
commit
d2fe00e2bc
@ -2,6 +2,7 @@ package client
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"reflect"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@ -38,6 +39,21 @@ func (handle *EventHandle[T]) dispatch(client *QQClient, event T) {
|
|||||||
for _, handler := range handle.handlers {
|
for _, handler := range handle.handlers {
|
||||||
handler(client, event)
|
handler(client, event)
|
||||||
}
|
}
|
||||||
|
if len(client.eventHandlers.subscribedEventHandlers) > 0 {
|
||||||
|
for _, h := range client.eventHandlers.subscribedEventHandlers {
|
||||||
|
ht := reflect.TypeOf(h)
|
||||||
|
for i := 0; i < ht.NumMethod(); i++ {
|
||||||
|
method := ht.Method(i)
|
||||||
|
if method.Type.NumIn() != 3 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if method.Type.In(1) != reflect.TypeOf(client) || method.Type.In(2) != reflect.TypeOf(event) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
method.Func.Call([]reflect.Value{reflect.ValueOf(h), reflect.ValueOf(client), reflect.ValueOf(event)})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type eventHandlers struct {
|
type eventHandlers struct {
|
||||||
@ -51,9 +67,14 @@ type eventHandlers struct {
|
|||||||
memberJoinedGuildHandlers []func(*QQClient, *MemberJoinGuildEvent)
|
memberJoinedGuildHandlers []func(*QQClient, *MemberJoinGuildEvent)
|
||||||
|
|
||||||
serverUpdatedHandlers []func(*QQClient, *ServerUpdatedEvent) bool
|
serverUpdatedHandlers []func(*QQClient, *ServerUpdatedEvent) bool
|
||||||
|
subscribedEventHandlers []any
|
||||||
groupMessageReceiptHandlers sync.Map
|
groupMessageReceiptHandlers sync.Map
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *QQClient) SubscribeEventHandler(handler any) {
|
||||||
|
c.eventHandlers.subscribedEventHandlers = append(c.eventHandlers.subscribedEventHandlers, handler)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *GuildService) OnGuildChannelMessage(f func(*QQClient, *message.GuildChannelMessage)) {
|
func (s *GuildService) OnGuildChannelMessage(f func(*QQClient, *message.GuildChannelMessage)) {
|
||||||
s.c.eventHandlers.guildChannelMessageHandlers = append(s.c.eventHandlers.guildChannelMessageHandlers, f)
|
s.c.eventHandlers.guildChannelMessageHandlers = append(s.c.eventHandlers.guildChannelMessageHandlers, f)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user