From dc1bb00e413595a54d202ce7b220c6d55823977b Mon Sep 17 00:00:00 2001 From: Shigma Date: Tue, 27 Jun 2023 01:08:07 +0800 Subject: [PATCH] fix(discord, kook): fix `bot.sendPrivateMessage()` not working --- adapters/discord/src/bot.ts | 9 ++++++++- adapters/discord/src/types/channel.ts | 4 ++-- adapters/kook/src/bot.ts | 22 ++++++++++++++-------- adapters/kook/src/message.ts | 4 ---- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/adapters/discord/src/bot.ts b/adapters/discord/src/bot.ts index 73d757aa..85c08e38 100644 --- a/adapters/discord/src/bot.ts +++ b/adapters/discord/src/bot.ts @@ -1,4 +1,4 @@ -import { Bot, Context, Fragment, h, Quester, Schema, Universal } from '@satorijs/satori' +import { Bot, Context, Fragment, h, Quester, Schema, SendOptions, Universal } from '@satorijs/satori' import { adaptChannel, adaptGuild, adaptMessage, adaptUser, decodeRole, encodeRole } from './utils' import { DiscordMessageEncoder } from './message' import { Internal, Webhook } from './types' @@ -181,6 +181,13 @@ export class DiscordBot extends Bot { deleteGuildRole(guildId: string, roleId: string) { return this.internal.deleteGuildRole(guildId, roleId) } + + async sendPrivateMessage(userId: string, content: Fragment, options?: SendOptions) { + const channel = await this.internal.createDM({ + recipient_id: userId, + }) + return this.sendMessage(channel.id, content, null, options) + } } export namespace DiscordBot { diff --git a/adapters/discord/src/types/channel.ts b/adapters/discord/src/types/channel.ts index cb93c981..9571f068 100644 --- a/adapters/discord/src/types/channel.ts +++ b/adapters/discord/src/types/channel.ts @@ -385,12 +385,12 @@ declare module './internal' { * Create a new DM channel with a user. Returns a DM channel object. * @see https://discord.com/developers/docs/resources/user#create-dm */ - createDM(params: Channel.Params.CreateDM): Promise + createDM(params: Channel.Params.CreateDM): Promise /** * Create a new group DM channel with multiple users. Returns a DM channel object. This endpoint was intended to be used with the now-deprecated GameBridge SDK. DMs created with this endpoint will not be shown in the Discord client * @see https://discord.com/developers/docs/resources/user#create-group-dm */ - createGroupDM(params: Channel.Params.CreateGroupDM): Promise + createGroupDM(params: Channel.Params.CreateGroupDM): Promise } } diff --git a/adapters/kook/src/bot.ts b/adapters/kook/src/bot.ts index cd478939..fea2a50c 100644 --- a/adapters/kook/src/bot.ts +++ b/adapters/kook/src/bot.ts @@ -1,5 +1,4 @@ -import { Bot, Context, Fragment, h, Quester, Schema, Universal } from '@satorijs/satori' -import { Method } from 'axios' +import { Bot, Context, Fragment, h, Quester, Schema, SendOptions, Universal } from '@satorijs/satori' import { adaptAuthor, adaptGroup, adaptMessage, adaptUser, decodeRole, encodeRole } from './utils' import * as Kook from './types' import FormData from 'form-data' @@ -8,6 +7,8 @@ import { HttpServer } from './http' import { isDirectChannel, KookMessageEncoder } from './message' export class KookBot extends Bot { + static MessageEncoder = KookMessageEncoder + http: Quester internal: Kook.Internal @@ -28,7 +29,7 @@ export class KookBot extends Bot { } } - async request(method: Method, path: string, data = {}, headers: any = {}): Promise { + async request(method: Quester.Method, path: string, data = {}, headers: any = {}): Promise { if (method === 'GET') { return (await this.http.get(path, { params: data, headers })).data } else { @@ -38,7 +39,7 @@ export class KookBot extends Bot { } async deleteMessage(channelId: string, msg_id: string) { - if (channelId.length > 30) { + if (isDirectChannel(channelId)) { await this.request('POST', '/user-chat/delete-msg', { msg_id }) } else { await this.request('POST', '/message/delete', { msg_id }) @@ -47,7 +48,7 @@ export class KookBot extends Bot { async editMessage(channelId: string, msg_id: string, content: Fragment) { content = h.normalize(content).join('') - if (channelId.length > 30) { + if (isDirectChannel(channelId)) { await this.request('POST', '/user-chat/update-msg', { msg_id, content }) } else { await this.request('POST', '/message/update', { msg_id, content }) @@ -55,7 +56,7 @@ export class KookBot extends Bot { } async getMessage(channelId: string, msg_id: string) { - if (channelId.length > 30) { + 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 })) @@ -63,7 +64,7 @@ export class KookBot extends Bot { } async $createReaction(channelId: string, msg_id: string, emoji: string) { - if (channelId.length > 30) { + 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 }) @@ -71,7 +72,7 @@ export class KookBot extends Bot { } async $deleteReaction(channelId: string, messageId: string, emoji: string, user_id?: string) { - if (channelId.length > 30) { + 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 }) @@ -107,6 +108,11 @@ export class KookBot extends Bot { await this.request('POST', '/guild/kickout', { guild_id, user_id }) } + async sendPrivateMessage(userId: string, content: Fragment, options?: SendOptions) { + const { code } = await this.request('POST', '/user-chat/create', { target_id: userId }) + return this.sendMessage(code, content, null, options) + } + createReaction(channelId: string, messageId: string, emoji: string) { if (isDirectChannel(channelId)) { return this.internal.addDirectMessageReaction({ msg_id: messageId, emoji }) diff --git a/adapters/kook/src/message.ts b/adapters/kook/src/message.ts index 2cdea1e1..fbf005c2 100644 --- a/adapters/kook/src/message.ts +++ b/adapters/kook/src/message.ts @@ -18,10 +18,6 @@ export class KookMessageEncoder extends MessageEncoder { private buffer: string = '' async prepare() { - if (this.session.subtype === 'private') { - const { code } = await this.bot.request('POST', '/user-chat/create', { target_id: this.session.channelId }) - this.session.channelId = code - } if (isDirectChannel(this.session.channelId)) { this.params.chat_code = this.session.channelId this.path = '/user-chat/create-msg'