From b64bc29fbee0eec650c289f41894b1c355b33e1a Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Fri, 30 Apr 2021 11:35:19 +0530 Subject: [PATCH] feat: User#send --- src/managers/channels.ts | 20 ++++++++++++++++++-- src/structures/user.ts | 23 +++++++++++++++++++++++ src/types/channel.ts | 1 + test/index.ts | 5 +++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/managers/channels.ts b/src/managers/channels.ts index a8c4b30..6cb30bb 100644 --- a/src/managers/channels.ts +++ b/src/managers/channels.ts @@ -3,6 +3,7 @@ import { Channel } from '../structures/channel.ts' import { Embed } from '../structures/embed.ts' import { Message } from '../structures/message.ts' import type { TextChannel } from '../structures/textChannel.ts' +import type { User } from '../structures/user.ts' import type { ChannelPayload, GuildChannelPayload, @@ -21,6 +22,21 @@ export class ChannelsManager extends BaseManager { super(client, 'channels', Channel) } + async getUserDM(user: User | string): Promise { + return this.client.cache.get( + 'user_dms', + typeof user === 'string' ? user : user.id + ) + } + + async setUserDM(user: User | string, id: string): Promise { + await this.client.cache.set( + 'user_dms', + typeof user === 'string' ? user : user.id, + id + ) + } + // Override get method as Generic async get(key: string): Promise { const data = await this._get(key) @@ -99,7 +115,7 @@ export class ChannelsManager extends BaseManager { } const payload: any = { - content: content, + content: content ?? option?.content, embed: option?.embed, file: option?.file, files: option?.files, @@ -165,7 +181,7 @@ export class ChannelsManager extends BaseManager { const newMsg = await this.client.rest.api.channels[channelID].messages[ typeof message === 'string' ? message : message.id ].patch({ - content: text, + content: text ?? option?.content, embed: option?.embed !== undefined ? option.embed.toJSON() : undefined, // Cannot upload new files with Message // file: option?.file, diff --git a/src/structures/user.ts b/src/structures/user.ts index 4f1ede0..0a085be 100644 --- a/src/structures/user.ts +++ b/src/structures/user.ts @@ -6,6 +6,8 @@ import { ImageURL } from './cdn.ts' import type { ImageSize, ImageFormats } from '../types/cdn.ts' import { DEFAULT_USER_AVATAR, USER_AVATAR } from '../types/endpoint.ts' import type { DMChannel } from './dmChannel.ts' +import { AllMessageOptions } from './textChannel.ts' +import { Message } from './message.ts' export class User extends SnowflakeBase { id: string @@ -94,4 +96,25 @@ export class User extends SnowflakeBase { async createDM(): Promise { return this.client.createDM(this) } + + async resolveDM(): Promise { + const dmID = await this.client.channels.getUserDM(this.id) + const dm = + (dmID !== undefined + ? await this.client.channels.get(dmID) + : undefined) ?? + (await this.createDM().then((chan) => + this.client.channels.setUserDM(this.id, chan.id).then(() => chan) + )) + // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion + return dm! + } + + async send( + content: string | AllMessageOptions, + options?: AllMessageOptions + ): Promise { + const dm = await this.resolveDM() + return dm.send(content, options) + } } diff --git a/src/types/channel.ts b/src/types/channel.ts index 8db4e5e..dbb82a2 100644 --- a/src/types/channel.ts +++ b/src/types/channel.ts @@ -204,6 +204,7 @@ export interface AllowedMentionsPayload { } export interface MessageOptions { + content?: string tts?: boolean embed?: Embed file?: MessageAttachment diff --git a/test/index.ts b/test/index.ts index 9e17c19..fa4d8ff 100644 --- a/test/index.ts +++ b/test/index.ts @@ -256,6 +256,11 @@ client.on('messageCreate', async (msg: Message) => { msg.reply(buf) } else if (msg.content === '!addrole') { msg.member?.roles.add('837255383759716362') + } else if (msg.content === '!dm') { + console.log('wtf') + msg.author.send('UwU').then((m) => { + msg.reply(`Done, ${m.id}`) + }) } else if (msg.content === '!timer') { msg.channel.send('3...').then((msg) => { setTimeout(() => {