kook.go

command module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 15, 2026 License: MIT Imports: 4 Imported by: 0

README

KOOK Go SDK

一个用于调用 KOOK API v3 的 Go SDK,封装了常用 HTTP 接口、WebSocket 事件连接和 Webhook 处理。

相关文档

安装

go get github.com/OggEu127/kook.go

快速开始

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/OggEu127/kook.go/kook"
)

func main() {
	client := kook.NewClient("你的机器人Token")

	me, err := client.User.GetMe(context.Background())
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("当前机器人: %s#%s\n", me.Username, me.IdentifyNum)
}

发送消息

msg, err := client.Message.SendMessage(context.Background(), kook.SendMessageParams{
	TargetID: "频道ID",
	Content:  "你好,KOOK!",
})
if err != nil {
	log.Fatal(err)
}

fmt.Println("消息ID:", msg.ID)

获取服务器和频道

guilds, err := client.Guild.GetGuildList(context.Background(), 1, 20, "")
if err != nil {
	log.Fatal(err)
}

channels, err := client.Channel.GetChannelList(context.Background(), "服务器ID", 1, 50, "")
if err != nil {
	log.Fatal(err)
}

fmt.Println("服务器数量:", len(guilds.Items))
fmt.Println("频道数量:", len(channels.Items))

上传文件

asset, err := client.Asset.UploadFile(context.Background(), "./image.png")
if err != nil {
	log.Fatal(err)
}

fmt.Println("资源地址:", asset.URL)

WebSocket 事件

ws := kook.NewWebSocketClient(client, false)

ws.OnEvent(kook.EventTypeTextMessage, func(event *kook.Event) {
	fmt.Println("收到消息:", event.Content)
})

if err := ws.Connect(); err != nil {
	log.Fatal(err)
}

select {}

错误处理

_, err := client.User.GetMe(context.Background())
if err != nil {
	if apiErr, ok := kook.IsKOOKError(err); ok {
		fmt.Printf("KOOK API 错误: code=%d message=%s\n", apiErr.Code, apiErr.Message)
		return
	}

	fmt.Println("请求失败:", err)
}

支持的主要功能

  • 用户信息
  • 服务器和成员管理
  • 频道管理
  • 频道权限覆写
  • 频道消息和私聊消息
  • 私信会话
  • 好友接口
  • 角色权限
  • 邀请、黑名单、表情、徽章
  • 媒体资源上传
  • 亲密度、用户动态、帖子
  • Gateway、WebSocket、Webhook

本地测试

go test ./...
go build ./...

运行根目录示例:

export KOOK_BOT_TOKEN="你的机器人Token"
go run .

运行 examples:

export KOOK_TOKEN="你的机器人Token"
go run examples/simple_bot/main.go

项目结构

kook.go/
├── kook/       # SDK 核心代码
├── examples/   # 示例程序
├── go.mod
└── README.md

要求

  • Go 1.21 或更高版本
  • KOOK 机器人 Token

许可证

本项目使用 MIT License,详见 LICENSE

说明

这是 KOOK API 的非官方 Go SDK,与 KOOK 官方没有从属关系。

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
examples
advanced_bot command
api_usage command
simple_bot command
webhook_bot command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL