From b87de9cd3b84786df4451c1b4cd68652c8165777 Mon Sep 17 00:00:00 2001 From: Skillz Date: Fri, 22 May 2020 15:53:03 -0400 Subject: [PATCH] adds dm support for androz slowmode --- structures/user.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/structures/user.ts b/structures/user.ts index 37241aebf..b90ae9d30 100644 --- a/structures/user.ts +++ b/structures/user.ts @@ -2,6 +2,11 @@ import { formatImageURL } from "../utils/cdn.ts"; import { endpoints } from "../constants/discord.ts"; import { ImageSize, ImageFormats } from "../types/cdn.ts"; import { UserPayload } from "../types/guild.ts"; +import { RequestManager } from "../module/requestManager.ts"; +import { MessageContent, DMChannelCreatePayload } from "../types/channel.ts"; +import { cache } from "../utils/cache.ts"; +import { logRed, logYellow } from "../utils/logger.ts"; +import { createChannel } from "./channel.ts"; export const enum PremiumType { NitroClassic = 1, @@ -27,6 +32,26 @@ export const createUser = (data: UserPayload) => ({ format, ) : endpoints.USER_DEFAULT_AVATAR(Number(data.discriminator) % 5), + /** Send a message to a users DM. Note: this takes 2 API calls. 1 is to fetch the users dm channel. 2 is to send a message to that channel. */ + sendMessage: async function (content: string | MessageContent) { + let dmChannel = cache.channels.get(data.id); + if (!dmChannel) { + // If not available in cache create a new one. + const dmChannelData = await RequestManager.post( + endpoints.USER_CREATE_DM, + { recipient_id: data.id }, + ) as DMChannelCreatePayload; + // Channel create event will have added this channel to the cache + cache.channels.delete(dmChannelData.id); + const channel = createChannel(dmChannelData); + // Recreate the channel and add it undert he users id + cache.channels.set(data.id, channel); + dmChannel = channel; + } + + // If it does exist try sending a message to this user + return dmChannel?.sendMessage(content); + }, }); export interface User extends ReturnType {}