feat: User#send

This commit is contained in:
DjDeveloperr 2021-04-30 11:35:19 +05:30
parent 718a4658eb
commit b64bc29fbe
4 changed files with 47 additions and 2 deletions

View File

@ -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<ChannelPayload, Channel> {
super(client, 'channels', Channel)
}
async getUserDM(user: User | string): Promise<string | undefined> {
return this.client.cache.get(
'user_dms',
typeof user === 'string' ? user : user.id
)
}
async setUserDM(user: User | string, id: string): Promise<void> {
await this.client.cache.set(
'user_dms',
typeof user === 'string' ? user : user.id,
id
)
}
// Override get method as Generic
async get<T = Channel>(key: string): Promise<T | undefined> {
const data = await this._get(key)
@ -99,7 +115,7 @@ export class ChannelsManager extends BaseManager<ChannelPayload, Channel> {
}
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<ChannelPayload, Channel> {
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,

View File

@ -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<DMChannel> {
return this.client.createDM(this)
}
async resolveDM(): Promise<DMChannel> {
const dmID = await this.client.channels.getUserDM(this.id)
const dm =
(dmID !== undefined
? await this.client.channels.get<DMChannel>(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<Message> {
const dm = await this.resolveDM()
return dm.send(content, options)
}
}

View File

@ -204,6 +204,7 @@ export interface AllowedMentionsPayload {
}
export interface MessageOptions {
content?: string
tts?: boolean
embed?: Embed
file?: MessageAttachment

View File

@ -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(() => {