-
Notifications
You must be signed in to change notification settings - Fork 39
/
bot.ts
193 lines (164 loc) · 6.71 KB
/
bot.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { Bot, Context, Fragment, h, Quester, Schema, Universal } from '@satorijs/satori'
import { adaptGroup, adaptMessage, adaptUser, decodeGuildMember, decodeRole, encodeRole } from './utils'
import * as Kook from './types'
import FormData from 'form-data'
import { WsClient } from './ws'
import { HttpServer } from './http'
import { isDirectChannel, KookMessageEncoder } from './message'
export class KookBot<C extends Context = Context, T extends KookBot.Config = KookBot.Config> extends Bot<C, T> {
static MessageEncoder = KookMessageEncoder
static inject = ['http']
http: Quester
internal: Kook.Internal
constructor(ctx: C, config: T) {
super(ctx, config, 'kook')
this.http = ctx.http.extend({
headers: {
'Authorization': `Bot ${config.token}`,
'Content-Type': 'application/json',
},
}).extend(config)
this.internal = new Kook.Internal(this.http)
if (config.protocol === 'http') {
ctx.plugin(HttpServer, this)
} else if (config.protocol === 'ws') {
ctx.plugin(WsClient, this as any)
}
}
async request<T = any>(method: Quester.Method, path: string, data = {}, headers: any = {}): Promise<T> {
if (method === 'GET') {
return (await this.http.get(path, { params: data, headers })).data
} else {
data = data instanceof FormData ? data : JSON.stringify(data)
return (await this.http(method, path, { data, headers })).data
}
}
async deleteMessage(channelId: string, msg_id: string) {
if (isDirectChannel(channelId)) {
await this.request('POST', '/user-chat/delete-msg', { msg_id })
} else {
await this.request('POST', '/message/delete', { msg_id })
}
}
async editMessage(channelId: string, msg_id: string, content: Fragment) {
content = h.normalize(content).join('')
if (isDirectChannel(channelId)) {
await this.request('POST', '/user-chat/update-msg', { msg_id, content })
} else {
await this.request('POST', '/message/update', { msg_id, content })
}
}
async getMessage(channelId: string, msg_id: string) {
if (isDirectChannel(channelId)) {
return adaptMessage(await this.request('POST', '/user-chat/view', { msg_id }))
} else {
return adaptMessage(await this.request('POST', '/message/view', { msg_id }))
}
}
async $createReaction(channelId: string, msg_id: string, emoji: string) {
if (isDirectChannel(channelId)) {
await this.request('POST', '/direct-message/add-reaction', { msg_id, emoji })
} else {
await this.request('POST', '/message/add-reaction', { msg_id, emoji })
}
}
async $deleteReaction(channelId: string, messageId: string, emoji: string, user_id?: string) {
if (isDirectChannel(channelId)) {
await this.request('POST', '/direct-message/delete-reaction', { msg_id: messageId, emoji })
} else {
await this.request('POST', '/message/delete-reaction', { msg_id: messageId, emoji, user_id })
}
}
async getLogin() {
this.user = adaptUser(await this.request<Kook.Self>('GET', '/user/me'))
return this.toJSON()
}
async getGuildList() {
const { items } = await this.request<Kook.GuildList>('GET', '/guild/list')
return { data: items.map(adaptGroup) }
}
async getGuildMemberList(guild_id: string) {
const { items } = await this.request<Kook.GuildUserList>('GET', '/guild/user-list', { guild_id })
return { data: items.map(decodeGuildMember) }
}
async setGroupNickname(guild_id: string, user_id: string, nickname: string) {
await this.request('POST', '/guild/nickname', { guild_id, user_id, nickname })
}
async leaveGroup(guild_id: string) {
await this.request('POST', '/guild/leave', { guild_id })
}
async kickGroup(guild_id: string, user_id: string) {
await this.request('POST', '/guild/kickout', { guild_id, user_id })
}
async createDirectChannel(userId: string) {
const { code } = await this.request('POST', '/user-chat/create', { target_id: userId })
return { id: code, type: Universal.Channel.Type.DIRECT }
}
createReaction(channelId: string, messageId: string, emoji: string) {
if (isDirectChannel(channelId)) {
return this.internal.addDirectMessageReaction({ msg_id: messageId, emoji })
} else {
return this.internal.addMessageReaction({ msg_id: messageId, emoji })
}
}
deleteReaction(channelId: string, messageId: string, emoji: string, userId?: string) {
if (isDirectChannel(channelId)) {
return this.internal.deleteDirectMessageReaction({ msg_id: messageId, emoji, user_id: userId })
} else {
return this.internal.deleteMessageReaction({ msg_id: messageId, emoji, user_id: userId })
}
}
async getReactionList(channelId: string, messageId: string, emoji: string) {
let users: Kook.User[]
if (isDirectChannel(channelId)) {
users = await this.internal.getDirectMessageReactionList({ msg_id: messageId, emoji })
} else {
users = await this.internal.getMessageReactionList({ msg_id: messageId, emoji })
}
return { data: users.map(adaptUser) }
}
async setGuildMemberRole(guildId: string, userId: string, roleId: string) {
await this.internal.grantGuildRole({ guild_id: guildId, user_id: userId, role_id: +roleId })
}
async unsetGuildMemberRole(guildId: string, userId: string, roleId: string) {
await this.internal.revokeGuildRole({ guild_id: guildId, user_id: userId, role_id: +roleId })
}
async getGuildRoles(guildId: string) {
const { items } = await this.internal.getGuildRoleList({ guild_id: guildId })
return { data: items.map(decodeRole) }
}
async createGuildRole(guildId: string, data: Partial<Universal.GuildRole>) {
const role = await this.internal.createGuildRole({
guild_id: guildId,
...data,
})
return decodeRole(role)
}
async updateGuildRole(guildId: string, roleId: string, data: Partial<Universal.GuildRole>) {
await this.internal.updateGuildRole({
guild_id: guildId,
...encodeRole(data),
role_id: +roleId,
})
}
async deleteGuildRole(guildId: string, roleId: string) {
await this.internal.deleteGuildRole({ guild_id: guildId, role_id: +roleId })
}
}
export namespace KookBot {
export interface BaseConfig extends Quester.Config, KookMessageEncoder.Config {}
export type Config = BaseConfig & (HttpServer.Options | WsClient.Options)
export const Config: Schema<Config> = Schema.intersect([
Schema.object({
protocol: process.env.KOISHI_ENV === 'browser'
? Schema.const('ws').default('ws')
: Schema.union(['http', 'ws']).description('选择要使用的协议。').required(),
}),
Schema.union([
WsClient.Options,
HttpServer.Options,
]),
KookMessageEncoder.Config,
Quester.createConfig('https://www.kookapp.cn/api/v3'),
] as const)
}