From 40f743cf46ea0bc921fb9b04ddaa4fb1cc9b3408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Fri, 13 Oct 2023 00:36:03 +0900 Subject: [PATCH] add part of handler --- bot.go | 17 +++++++++++++---- define.go | 33 ++++++++++++++++++++++++++++++++- handler.go | 31 +++++++++++++++++++++++++++++++ openapi_channel.go | 1 + openapi_guild.go | 1 + openapi_member.go | 1 + 6 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 handler.go diff --git a/bot.go b/bot.go index 1c0d942..4384717 100644 --- a/bot.go +++ b/bot.go @@ -4,10 +4,19 @@ import "time" // Bot 一个机器人实例的配置 type Bot struct { - AppID string // AppID is BotAppID(开发者ID) - Token string // Token is 机器人令牌 - Secret string // Secret is 机器人密钥 - Timeout time.Duration // Timeout is API 调用超时 + AppID string // AppID is BotAppID(开发者ID) + Token string // Token is 机器人令牌 + Secret string // Secret is 机器人密钥 + SuperUsers []string // SuperUsers 超级用户 + Timeout time.Duration // Timeout is API 调用超时 + Handler *Handler // Handler 注册对各种事件的处理 + + handlers map[string]GeneralHandleType // handlers 方便调用的 handler +} + +// Init 初始化, 只需执行一次 +func (b *Bot) Init() { + } // Authorization 返回 Authorization Header value diff --git a/define.go b/define.go index 7537fa8..d1c930e 100644 --- a/define.go +++ b/define.go @@ -1,5 +1,7 @@ package nano +import "encoding/json" + const ( // StandardAPI 正式环境接口域名 StandardAPI = `https://api.sgroup.qq.com` @@ -8,7 +10,7 @@ const ( ) var ( - OpenAPI = StandardAPI // OpenAPI 实际使用的 API, 可自行配置 + OpenAPI = StandardAPI // OpenAPI 实际使用的 API, 默认 StandardAPI, 可自行赋值配置 ) // CodeMessageBase 各种消息都有的 code + message 基类 @@ -16,3 +18,32 @@ type CodeMessageBase struct { C int `json:"code"` M string `json:"message"` } + +// OpCode https://bot.q.qq.com/wiki/develop/api/gateway/opcode.html +type OpCode int + +const ( + OpCodeDispatch OpCode = iota // Receive + OpCodeHeartbeat // Send/Receive + OpCodeIdentify // Send + OpCodeEmpty1 + OpCodeEmpty2 + OpCodeEmpty3 + OpCodeResume // Send + OpCodeReconnect // Receive + OpCodeEmpty4 + OpCodeInvalidSession // Receive + OpCodeHello // Receive + OpCodeHeartbeatACK // Receive/Reply + OpCodeHTTPCallbackACK // Reply +) + +// WebsocketPayload payload 指的是在 websocket 连接上传输的数据,网关的上下行消息采用的都是同一个结构 +// +// https://bot.q.qq.com/wiki/develop/api/gateway/reference.html +type WebsocketPayload struct { + Op OpCode `json:"op"` + D json.RawMessage `json:"d"` + S int `json:"s"` + T string `json:"t"` +} diff --git a/handler.go b/handler.go new file mode 100644 index 0000000..e084533 --- /dev/null +++ b/handler.go @@ -0,0 +1,31 @@ +package nano + +import "unsafe" + +// GeneralHandleType 作为通用的 handler 函数调用约定使用 +type GeneralHandleType func(int, *Bot, unsafe.Pointer) + +// Handler 事件订阅 +// +// https://bot.q.qq.com/wiki/develop/api/gateway/intents.html +type Handler struct { + // GUILDS (1 << 0) + + OnGuildCreate func(s int, bot *Bot, d *Guild) + OnGuildUpdate func(s int, bot *Bot, d *Guild) + OnGuildDelete func(s int, bot *Bot, d *Guild) + OnChannelCreate func(s int, bot *Bot, d *Channel) + OnChannelUpdate func(s int, bot *Bot, d *Channel) + OnChannelDelete func(s int, bot *Bot, d *Channel) + // GUILD_MEMBERS (1 << 1) + + OnGuildMemberAdd func(s int, bot *Bot, d *Member) + OnGuildMemberUpdate func(s int, bot *Bot, d *Member) + OnGuildMemberRemove func(s int, bot *Bot, d *Member) + // GUILD_MESSAGES (1 << 9) // 消息事件,仅 *私域* 机器人能够设置此 intents。 + + OnMessageCreate func(s int, bot *Bot, d *Message) + OnMessageDelete func(s int, bot *Bot, d *Message) + // GUILD_MESSAGE_REACTIONS (1 << 10) + +} diff --git a/openapi_channel.go b/openapi_channel.go index 3891f95..282f50d 100644 --- a/openapi_channel.go +++ b/openapi_channel.go @@ -58,6 +58,7 @@ type Channel struct { SpeakPermission SpeakPermission `json:"speak_permission"` ApplicationID string `json:"application_id"` // ApplicationID see https://bot.q.qq.com/wiki/develop/api/openapi/channel/model.html#%E5%BA%94%E7%94%A8%E5%AD%90%E9%A2%91%E9%81%93%E7%9A%84%E5%BA%94%E7%94%A8%E7%B1%BB%E5%9E%8B Permissions string `json:"permissions"` + OpUserID string `json:"op_user_id"` // https://bot.q.qq.com/wiki/develop/api/gateway/channel.html#%E5%86%85%E5%AE%B9 } // GetChannelsOfGuild 获取 guild_id 指定的频道下的子频道列表 diff --git a/openapi_guild.go b/openapi_guild.go index 39afd83..eb04dad 100644 --- a/openapi_guild.go +++ b/openapi_guild.go @@ -15,6 +15,7 @@ type Guild struct { MemberCount int `json:"member_count"` MaxMembers int `json:"max_members"` Description string `json:"description"` + OpUserID string `json:"op_user_id"` // https://bot.q.qq.com/wiki/develop/api/gateway/guild.html#%E4%BA%8B%E4%BB%B6%E5%86%85%E5%AE%B9 } // GetGuildByID 获取 guild_id 指定的频道的详情 diff --git a/openapi_member.go b/openapi_member.go index 92b7928..b21598a 100644 --- a/openapi_member.go +++ b/openapi_member.go @@ -14,6 +14,7 @@ type Member struct { Deaf bool `json:"deaf"` Mute bool `json:"mute"` Pending bool `json:"pending"` + OpUserID string `json:"op_user_id"` // https://bot.q.qq.com/wiki/develop/api/gateway/guild_member.html#%E5%86%85%E5%AE%B9 } // GetGuildMembersIn 获取 guild_id 指定的频道中所有成员的详情列表,支持分页