1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-04 19:17:37 +08:00

internal/onebot: new package

This commit is contained in:
wdvxdr 2023-02-15 14:24:57 +08:00
parent 6b3aabd9af
commit cf717ad762
8 changed files with 313 additions and 15 deletions

View File

@ -15,6 +15,11 @@ import (
"strings" "strings"
) )
var supported = flag.Bool("supported", false, "genRouter supported.go")
var output = flag.String("o", "", "output file")
var pkg = flag.String("pkg", "", "package name")
var src = flag.String("path", "", "source file")
type Param struct { type Param struct {
Name string Name string
Type string Type string
@ -44,7 +49,16 @@ func (g *generator) WriteString(s string) {
io.WriteString(g.out, s) io.WriteString(g.out, s)
} }
func (g *generator) generate(routers []Router) { func (g *generator) writef(format string, a ...any) {
fmt.Fprintf(g.out, format, a...)
}
func (g *generator) header() {
g.WriteString("// Code generated by cmd/api-generator. DO NOT EDIT.\n\n")
g.writef("package %s\n\n", *pkg)
}
func (g *generator) genRouter(routers []Router) {
var actions []string // for onebot v12 get_supported_actions var actions []string // for onebot v12 get_supported_actions
for _, router := range routers { for _, router := range routers {
if len(router.PathV12) > 0 { if len(router.PathV12) > 0 {
@ -59,21 +73,17 @@ func (g *generator) generate(routers []Router) {
} }
// TODO: v12 和 all 的 switch-case 由常量改为数组寻址, 以利用 get_supported_actions // TODO: v12 和 all 的 switch-case 由常量改为数组寻址, 以利用 get_supported_actions
g.WriteString("// Code generated by cmd/api-generator. DO NOT EDIT.\n\n") g.WriteString("import (\n\n")
g.WriteString("package api\n\nimport (\n\n")
g.WriteString("\"github.com/Mrs4s/go-cqhttp/coolq\"\n") g.WriteString("\"github.com/Mrs4s/go-cqhttp/coolq\"\n")
g.WriteString("\"github.com/Mrs4s/go-cqhttp/global\"\n") g.WriteString("\"github.com/Mrs4s/go-cqhttp/global\"\n")
g.WriteString(")\n\n") g.WriteString(")\n\n")
g.WriteString(fmt.Sprintf(`func (c *Caller) call(action string, version uint16, p Getter) global.MSG { g.WriteString(`func (c *Caller) call(action string, version uint16, p Getter) global.MSG {
var converter coolq.IDConverter = func(id any) any { var converter coolq.IDConverter = func(id any) any {
return coolq.ConvertIDWithVersion(id,version) return coolq.ConvertIDWithVersion(id,version)
} }
if version == 12 { if version == 12 {
if action == "get_supported_actions" {
return coolq.OK([]string{%v})
}
switch action { switch action {
`, strings.Join(actions, ","))) `)
for _, router := range routers { for _, router := range routers {
g.router(router, PathV12) g.router(router, PathV12)
} }
@ -194,7 +204,6 @@ func conv(v, t string) string {
func main() { func main() {
var routers []Router var routers []Router
src := flag.String("path", "", "source file")
flag.Parse() flag.Parse()
fset := token.NewFileSet() fset := token.NewFileSet()
for _, s := range strings.Split(*src, ",") { for _, s := range strings.Split(*src, ",") {
@ -304,12 +313,17 @@ func main() {
out := new(bytes.Buffer) out := new(bytes.Buffer)
g := &generator{out: out} g := &generator{out: out}
g.generate(routers) g.header()
if *supported {
g.genSupported(routers)
} else {
g.genRouter(routers)
}
source, err := format.Source(out.Bytes()) source, err := format.Source(out.Bytes())
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = os.WriteFile("api.go", source, 0o644) err = os.WriteFile(*output, source, 0o644)
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -0,0 +1,44 @@
package main
import "html/template"
func (g *generator) genSupported(routers []Router) {
var v11, v12 []string // for onebot v12 get_supported_actions
for _, router := range routers {
if len(router.PathV11) > 0 {
v11 = append(v11, router.PathV11...)
}
if len(router.PathV11) > 0 {
v12 = append(v12, router.PathV12...)
}
if len(router.Path) > 0 {
v11 = append(v11, router.Path...)
v12 = append(v12, router.Path...)
}
}
type S struct {
V11 []string
V12 []string
}
tmpl, err := template.New("").Parse(supportedTemplete)
if err != nil {
panic(err)
}
err = tmpl.Execute(g.out, &S{V11: v11, V12: v12})
if err != nil {
panic(err)
}
}
const supportedTemplete = `
var supportedV11 = []string{
{{range .V11}} "{{.}}",
{{end}}
}
var supportedV12 = []string{
{{range .V12}} "{{.}}",
{{end}}
}`

View File

@ -27,6 +27,7 @@ import (
"github.com/Mrs4s/go-cqhttp/internal/base" "github.com/Mrs4s/go-cqhttp/internal/base"
"github.com/Mrs4s/go-cqhttp/internal/cache" "github.com/Mrs4s/go-cqhttp/internal/cache"
"github.com/Mrs4s/go-cqhttp/internal/download" "github.com/Mrs4s/go-cqhttp/internal/download"
"github.com/Mrs4s/go-cqhttp/internal/onebot"
"github.com/Mrs4s/go-cqhttp/internal/param" "github.com/Mrs4s/go-cqhttp/internal/param"
"github.com/Mrs4s/go-cqhttp/modules/filter" "github.com/Mrs4s/go-cqhttp/modules/filter"
) )
@ -2105,6 +2106,14 @@ func (bot *CQBot) CQReloadEventFilter(file string) global.MSG {
return OK(nil) return OK(nil)
} }
// CQGetSupportedActions 获取支持的动作列表
//
// @route12(get_supported_actions)
func (bot *CQBot) CQGetSupportedActions() global.MSG {
// TODO: fix v11
return OK(onebot.V12.SupportedActions)
}
// OK 生成成功返回值 // OK 生成成功返回值
func OK(data any) global.MSG { func OK(data any) global.MSG {
return global.MSG{"data": data, "retcode": 0, "status": "ok", "message": ""} return global.MSG{"data": data, "retcode": 0, "status": "ok", "message": ""}

41
internal/onebot/onebot.go Normal file
View File

@ -0,0 +1,41 @@
package onebot
// Self 机器人自身标识
//
// https://12.onebot.dev/connect/data-protocol/basic-types/#_10
type Self struct {
Platform string `json:"platform"`
UserID string `json:"user_id"`
}
// Request 动作请求是应用端为了主动向 OneBot 实现请求服务而发送的数据
//
// https://12.onebot.dev/connect/data-protocol/action-request/
type Request struct {
Action string // 动作名称
Params any // 动作参数
Echo any // 每次请求的唯一标识
}
// Response 动作响应是 OneBot 实现收到应用端的动作请求并处理完毕后,发回应用端的数据
//
// https://12.onebot.dev/connect/data-protocol/action-response/
type Response struct {
Status string `json:"status"` // 执行状态,必须是 ok、failed 中的一个
Code int64 `json:"retcode"` // 返回码
Data any `json:"data"` // 响应数据
Message string `json:"message"` // 错误信息
Echo any `json:"echo"` // 动作请求中的 echo 字段值
}
// Event 事件
//
// https://12.onebot.dev/connect/data-protocol/event/
type Event struct {
ID string
Time int64
Type string
DetailType string
SubType string
Self *Self
}

23
internal/onebot/spec.go Normal file
View File

@ -0,0 +1,23 @@
// Package onebot defines onebot protocol struct and some spec info.
package onebot
//go:generate go run github.com/Mrs4s/go-cqhttp/cmd/api-generator -pkg onebot -path=./../../coolq/api.go,./../../coolq/api_v12.go -supported -o supported.go
// Spec OneBot Specification
type Spec struct {
Version int // must be 11 or 12
SupportedActions []string
}
/* // TODO: Use this variable
var V11 = &Spec{
Version: 11,
SupportedActions: supportedV11,
}
*/
// V12 OneBot V12
var V12 = &Spec{
Version: 12,
SupportedActions: supportedV12,
}

View File

@ -0,0 +1,168 @@
// Code generated by cmd/api-generator. DO NOT EDIT.
package onebot
var supportedV11 = []string{
".get_word_slices",
".handle_quick_operation",
".ocr_image",
"ocr_image",
"_del_group_notice",
"_get_group_notice",
"_get_model_show",
"_send_group_notice",
"_set_model_show",
"can_send_image",
"can_send_record",
"check_url_safely",
"create_group_file_folder",
"create_guild_role",
"delete_essence_msg",
"delete_friend",
"delete_group_file",
"delete_group_folder",
"delete_guild_role",
"delete_msg",
"delete_unidirectional_friend",
"download_file",
"get_essence_msg_list",
"get_forward_msg",
"get_friend_list",
"get_group_at_all_remain",
"get_group_file_system_info",
"get_group_file_url",
"get_group_files_by_folder",
"get_group_honor_info",
"get_group_info",
"get_group_list",
"get_group_member_info",
"get_group_member_list",
"get_group_msg_history",
"get_group_root_files",
"get_group_system_msg",
"get_guild_channel_list",
"get_guild_list",
"get_guild_member_list",
"get_guild_member_profile",
"get_guild_meta_by_guest",
"get_guild_msg",
"get_guild_roles",
"get_guild_service_profile",
"get_image",
"get_login_info",
"get_msg",
"get_online_clients",
"get_status",
"get_stranger_info",
"get_topic_channel_feeds",
"get_unidirectional_friend_list",
"get_version_info",
"mark_msg_as_read",
"qidian_get_account_info",
"reload_event_filter",
"send_forward_msg",
"send_group_forward_msg",
"send_group_msg",
"send_group_sign",
"send_guild_channel_msg",
"send_msg",
"send_private_forward_msg",
"send_private_msg",
"set_essence_msg",
"set_friend_add_request",
"set_group_add_request",
"set_group_admin",
"set_group_anonymous",
"set_group_anonymous_ban",
"set_group_ban",
"set_group_card",
"set_group_kick",
"set_group_leave",
"set_group_name",
"set_group_portrait",
"set_group_special_title",
"set_group_whole_ban",
"set_guild_member_role",
"set_qq_profile",
"update_guild_role",
"upload_group_file",
"upload_private_file",
}
var supportedV12 = []string{
".get_word_slices",
".handle_quick_operation",
".ocr_image",
"ocr_image",
"_del_group_notice",
"_get_group_notice",
"_get_model_show",
"_send_group_notice",
"_set_model_show",
"check_url_safely",
"create_group_file_folder",
"create_guild_role",
"delete_essence_msg",
"delete_friend",
"delete_group_file",
"delete_group_folder",
"delete_guild_role",
"delete_msg",
"delete_unidirectional_friend",
"download_file",
"get_essence_msg_list",
"get_forward_msg",
"get_friend_list",
"get_group_at_all_remain",
"get_group_file_system_info",
"get_group_file_url",
"get_group_files_by_folder",
"get_group_honor_info",
"get_group_info",
"get_group_list",
"get_group_member_info",
"get_group_member_list",
"get_group_msg_history",
"get_group_root_files",
"get_group_system_msg",
"get_guild_channel_list",
"get_guild_list",
"get_guild_member_list",
"get_guild_member_profile",
"get_guild_meta_by_guest",
"get_guild_msg",
"get_guild_roles",
"get_guild_service_profile",
"get_image",
"get_self_info",
"get_msg",
"get_online_clients",
"get_status",
"get_user_info",
"get_topic_channel_feeds",
"get_unidirectional_friend_list",
"mark_msg_as_read",
"qidian_get_account_info",
"reload_event_filter",
"send_group_sign",
"send_guild_channel_msg",
"set_essence_msg",
"set_friend_add_request",
"set_group_add_request",
"set_group_admin",
"set_group_anonymous",
"set_group_anonymous_ban",
"set_group_ban",
"set_group_card",
"set_group_kick",
"set_group_leave",
"set_group_name",
"set_group_portrait",
"set_group_special_title",
"set_group_whole_ban",
"set_guild_member_role",
"set_qq_profile",
"update_guild_role",
"upload_group_file",
"upload_private_file",
}

View File

@ -12,15 +12,14 @@ func (c *Caller) call(action string, version uint16, p Getter) global.MSG {
return coolq.ConvertIDWithVersion(id, version) return coolq.ConvertIDWithVersion(id, version)
} }
if version == 12 { if version == 12 {
if action == "get_supported_actions" {
return coolq.OK([]string{".get_word_slices", ".handle_quick_operation", ".ocr_image", "ocr_image", "_get_group_notice", "_get_model_show", "_send_group_notice", "_set_model_show", "check_url_safely", "create_group_file_folder", "create_guild_role", "delete_essence_msg", "delete_friend", "delete_group_file", "delete_group_folder", "delete_guild_role", "delete_msg", "delete_unidirectional_friend", "download_file", "get_essence_msg_list", "get_forward_msg", "get_friend_list", "get_group_at_all_remain", "get_group_file_system_info", "get_group_file_url", "get_group_files_by_folder", "get_group_honor_info", "get_group_info", "get_group_list", "get_group_member_info", "get_group_member_list", "get_group_msg_history", "get_group_root_files", "get_group_system_msg", "get_guild_channel_list", "get_guild_list", "get_guild_member_list", "get_guild_member_profile", "get_guild_meta_by_guest", "get_guild_msg", "get_guild_roles", "get_guild_service_profile", "get_image", "get_self_info", "get_msg", "get_online_clients", "get_status", "get_user_info", "get_topic_channel_feeds", "get_unidirectional_friend_list", "get_version", "mark_msg_as_read", "qidian_get_account_info", "reload_event_filter", "send_group_sign", "send_guild_channel_msg", "send_message", "set_essence_msg", "set_friend_add_request", "set_group_add_request", "set_group_admin", "set_group_anonymous_ban", "set_group_ban", "set_group_card", "set_group_kick", "set_group_leave", "set_group_name", "set_group_portrait", "set_group_special_title", "set_group_whole_ban", "set_guild_member_role", "set_qq_profile", "update_guild_role", "upload_group_file"})
}
switch action { switch action {
case "get_self_info": case "get_self_info":
return c.bot.CQGetLoginInfo() return c.bot.CQGetLoginInfo()
case "get_user_info": case "get_user_info":
p0 := p.Get("user_id").Int() p0 := p.Get("user_id").Int()
return c.bot.CQGetStrangerInfo(p0) return c.bot.CQGetStrangerInfo(p0)
case "get_supported_actions":
return c.bot.CQGetSupportedActions()
case "get_version": case "get_version":
return c.bot.CQGetVersion() return c.bot.CQGetVersion()
case "send_message": case "send_message":

View File

@ -8,7 +8,7 @@ import (
"github.com/Mrs4s/go-cqhttp/global" "github.com/Mrs4s/go-cqhttp/global"
) )
//go:generate go run github.com/Mrs4s/go-cqhttp/cmd/api-generator -path=./../../coolq/api.go,./../../coolq/api_v12.go //go:generate go run github.com/Mrs4s/go-cqhttp/cmd/api-generator -pkg api -path=./../../coolq/api.go,./../../coolq/api_v12.go -o api.go
// Getter 参数获取 // Getter 参数获取
type Getter interface { type Getter interface {