Members helpers

This commit is contained in:
TriForMine
2021-10-21 18:06:45 +02:00
parent 83da6897a6
commit fe22c5ea73
17 changed files with 122 additions and 144 deletions
+10 -15
View File
@@ -1,29 +1,24 @@
import { botId } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts";
import type { Channel } from "../../types/channels/channel.ts";
import type { CreateMessage } from "../../types/messages/create_message.ts";
import { Errors } from "../../types/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { sendMessage } from "../messages/send_message.ts";
import type {Bot} from "../../bot.ts";
import type {SnakeCasedPropertiesDeep} from "../../types/util.ts";
/** 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. */
export async function sendDirectMessage(memberId: bigint, content: string | CreateMessage) {
if (memberId === botId) throw new Error(Errors.YOU_CAN_NOT_DM_THE_BOT_ITSELF);
let dmChannel = await cacheHandlers.get("channels", memberId);
export async function sendDirectMessage(bot: Bot, memberId: bigint, content: string | CreateMessage) {
if (memberId === bot.id) throw new Error(bot.constants.Errors.YOU_CAN_NOT_DM_THE_BOT_ITSELF);
let dmChannel = await bot.cache.channels.get(memberId);
if (!dmChannel) {
// If not available in cache create a new one.
const dmChannelData = await rest.runMethod<Channel>("post", endpoints.USER_DM, {
const dmChannelData = await bot.rest.runMethod<SnakeCasedPropertiesDeep<Channel>>(bot.rest,"post", bot.constants.endpoints.USER_DM, {
recipient_id: memberId,
});
const discordenoChannel = await structures.createDiscordenoChannel(dmChannelData);
const discordenoChannel = await bot.transformers.channel(bot, dmChannelData);
// Recreate the channel and add it under the users id
await cacheHandlers.set("channels", memberId, discordenoChannel);
await bot.cache.channels.set(memberId, discordenoChannel);
dmChannel = discordenoChannel;
}
// If it does exist try sending a message to this user
return await sendMessage(dmChannel.id, content);
return await bot.helpers.sendMessage(dmChannel.id, content);
}