Skip to content

Commit

Permalink
fix(discord, kook): fix bot.sendPrivateMessage() not working
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 26, 2023
1 parent 593a39b commit dc1bb00
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
9 changes: 8 additions & 1 deletion adapters/discord/src/bot.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -181,6 +181,13 @@ export class DiscordBot extends Bot<DiscordBot.Config> {
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 {
Expand Down
4 changes: 2 additions & 2 deletions adapters/discord/src/types/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>
createDM(params: Channel.Params.CreateDM): Promise<Channel>
/**
* 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<void>
createGroupDM(params: Channel.Params.CreateGroupDM): Promise<Channel>
}
}

Expand Down
22 changes: 14 additions & 8 deletions adapters/kook/src/bot.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -8,6 +7,8 @@ import { HttpServer } from './http'
import { isDirectChannel, KookMessageEncoder } from './message'

export class KookBot<T extends KookBot.Config = KookBot.Config> extends Bot<T> {
static MessageEncoder = KookMessageEncoder

http: Quester
internal: Kook.Internal

Expand All @@ -28,7 +29,7 @@ export class KookBot<T extends KookBot.Config = KookBot.Config> extends Bot<T> {
}
}

async request<T = any>(method: Method, path: string, data = {}, headers: any = {}): Promise<T> {
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 {
Expand All @@ -38,7 +39,7 @@ export class KookBot<T extends KookBot.Config = KookBot.Config> extends Bot<T> {
}

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 })
Expand All @@ -47,31 +48,31 @@ export class KookBot<T extends KookBot.Config = KookBot.Config> extends Bot<T> {

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 })
}
}

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 }))
}
}

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 })
}
}

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 })
Expand Down Expand Up @@ -107,6 +108,11 @@ export class KookBot<T extends KookBot.Config = KookBot.Config> extends Bot<T> {
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 })
Expand Down
4 changes: 0 additions & 4 deletions adapters/kook/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ export class KookMessageEncoder extends MessageEncoder<KookBot> {
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'
Expand Down

0 comments on commit dc1bb00

Please sign in to comment.