diff --git a/src/helpers/channels/category_children.ts b/src/helpers/channels/category_children.ts index c0f31ad83..566d0c790 100644 --- a/src/helpers/channels/category_children.ts +++ b/src/helpers/channels/category_children.ts @@ -1,8 +1,9 @@ -import { cache } from "../../cache.ts"; +import {Bot} from "../../bot.ts"; /** Gets an array of all the channels ids that are the children of this category. * ⚠️ This does not work for custom cache users! */ -export function categoryChildren(parentChannelId: bigint) { - return cache.channels.filter((channel) => channel.parentId === parentChannelId); +export function categoryChildren(bot: Bot, parentChannelId: bigint) { + // TODO: Cache Filter ? + return bot.cache.channels.filter((channel) => channel.parentId === parentChannelId); } diff --git a/src/helpers/channels/clone_channel.ts b/src/helpers/channels/clone_channel.ts index 70c2d41a6..1363011d7 100644 --- a/src/helpers/channels/clone_channel.ts +++ b/src/helpers/channels/clone_channel.ts @@ -1,18 +1,15 @@ -import { cacheHandlers } from "../../cache.ts"; +import type { Bot } from "../../bot.ts"; import { DiscordChannelTypes } from "../../types/channels/channel_types.ts"; import type { CreateGuildChannel } from "../../types/guilds/create_guild_channel.ts"; -import { Errors } from "../../types/discordeno/errors.ts"; -import { calculatePermissions } from "../../util/permissions.ts"; -import { helpers } from "../mod.ts"; /** Create a copy of a channel */ -export async function cloneChannel(channelId: bigint, reason?: string) { - const channelToClone = await cacheHandlers.get("channels", channelId); - if (!channelToClone) throw new Error(Errors.CHANNEL_NOT_FOUND); +export async function cloneChannel(bot: Bot, channelId: bigint, reason?: string) { + const channelToClone = await bot.cache.channels.get(channelId); + if (!channelToClone) throw new Error(bot.constants.Errors.CHANNEL_NOT_FOUND); //Check for DM channel if (channelToClone.type === DiscordChannelTypes.DM || channelToClone.type === DiscordChannelTypes.GroupDm) { - throw new Error(Errors.CHANNEL_NOT_IN_GUILD); + throw new Error(bot.constants.Errors.CHANNEL_NOT_IN_GUILD); } const createChannelOptions: CreateGuildChannel = { @@ -22,11 +19,11 @@ export async function cloneChannel(channelId: bigint, reason?: string) { permissionOverwrites: channelToClone.permissionOverwrites.map((overwrite) => ({ id: overwrite.id.toString(), type: overwrite.type, - allow: calculatePermissions(overwrite.allow), - deny: calculatePermissions(overwrite.deny), + allow: bot.utils.calculatePermissions(overwrite.allow), + deny: bot.utils.calculatePermissions(overwrite.deny), })), }; //Create the channel (also handles permissions) - return await helpers.createChannel(channelToClone.guildId!, createChannelOptions, reason); + return await bot.helpers.createChannel(bot, channelToClone.guildId!, createChannelOptions, reason); } diff --git a/src/helpers/channels/create_channel.ts b/src/helpers/channels/create_channel.ts index 0561f95c1..7e8f84b8f 100644 --- a/src/helpers/channels/create_channel.ts +++ b/src/helpers/channels/create_channel.ts @@ -1,39 +1,42 @@ -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 { DiscordChannelTypes } from "../../types/channels/channel_types.ts"; import type { CreateGuildChannel, DiscordCreateGuildChannel } from "../../types/guilds/create_guild_channel.ts"; -import { endpoints } from "../../util/constants.ts"; -import { calculateBits, requireOverwritePermissions } from "../../util/permissions.ts"; -import { snakelize } from "../../util/utils.ts"; +import type { Bot } from "../../bot.ts"; /** Create a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */ -export async function createChannel(guildId: bigint, options?: CreateGuildChannel, reason?: string) { - if (options?.permissionOverwrites) { - await requireOverwritePermissions(guildId, options.permissionOverwrites); - } +export async function createChannel(bot: Bot, guildId: bigint, options?: CreateGuildChannel, reason?: string) { + if (options?.permissionOverwrites) { + await bot.utils.requireOverwritePermissions(bot, guildId, options.permissionOverwrites); + } - // BITRATES ARE IN THOUSANDS SO IF USER PROVIDES 32 WE CONVERT TO 32000 - if (options?.bitrate && options.bitrate < 1000) options.bitrate *= 1000; + // BITRATES ARE IN THOUSANDS SO IF USER PROVIDES 32 WE CONVERT TO 32000 + if (options?.bitrate && options.bitrate < 1000) options.bitrate *= 1000; - const result = await rest.runMethod( - "post", - endpoints.GUILD_CHANNELS(guildId), - snakelize({ - ...options, - permissionOverwrites: options?.permissionOverwrites?.map((perm) => ({ - ...perm, - allow: calculateBits(perm.allow), - deny: calculateBits(perm.deny), - })), - type: options?.type || DiscordChannelTypes.GuildText, - reason, - }) - ); + const result = await bot.rest.runMethod( + bot.rest, + "post", + bot.constants.endpoints.GUILD_CHANNELS(guildId), + options ? { + name: options.name, + topic: options.topic, + bitrate: options.bitrate, + userLimit: options.userLimit, + rateLimitPerUser: options.rateLimitPerUser, + position: options.position, + parentId: options.parentId, + nsfw: options.nsfw, + permission_overwrites: options?.permissionOverwrites?.map((perm) => ({ + ...perm, + allow: bot.utils.calculateBits(perm.allow), + deny: bot.utils.calculateBits(perm.deny), + })), + type: options?.type || DiscordChannelTypes.GuildText, + reason, + } : {} + ); - const discordenoChannel = await structures.createDiscordenoChannel(result); - await cacheHandlers.set("channels", discordenoChannel.id, discordenoChannel); + const discordenoChannel = bot.transformers.channel(result); + await bot.cache.channels.set(discordenoChannel.id, discordenoChannel); - return discordenoChannel; + return discordenoChannel; } diff --git a/src/helpers/channels/create_stage_instance.ts b/src/helpers/channels/create_stage_instance.ts index 17728e9a0..bf0aaf56e 100644 --- a/src/helpers/channels/create_stage_instance.ts +++ b/src/helpers/channels/create_stage_instance.ts @@ -1,37 +1,32 @@ -import { validateLength } from "../../util/validate_length.ts"; -import { Errors } from "../../types/discordeno/errors.ts"; -import { rest } from "../../rest/rest.ts"; -import { endpoints } from "../../util/constants.ts"; import type { StageInstance } from "../../types/channels/stage_instance.ts"; -import { cacheHandlers } from "../../cache.ts"; +import type { Bot } from "../../bot.ts"; import { ChannelTypes } from "../../types/channels/channel_types.ts"; -import { requireBotChannelPermissions } from "../../util/permissions.ts"; import { PrivacyLevel } from "../../types/channels/privacy_level.ts"; -import { snakelize } from "../../util/utils.ts"; /** Creates a new Stage instance associated to a Stage channel. Requires the user to be a moderator of the Stage channel. */ -export async function createStageInstance(channelId: bigint, topic: string, privacyLevel?: PrivacyLevel) { - const channel = await cacheHandlers.get("channels", channelId); +export async function createStageInstance(bot: Bot, channelId: bigint, topic: string, privacyLevel?: PrivacyLevel) { + const channel = await bot.cache.channels.get(channelId); if (channel) { if (channel.type !== ChannelTypes.GuildStageVoice) { - throw new Error(Errors.CHANNEL_NOT_STAGE_VOICE); + throw new Error(bot.constants.Errors.CHANNEL_NOT_STAGE_VOICE); } - await requireBotChannelPermissions(channel, ["MANAGE_CHANNELS", "MUTE_MEMBERS", "MOVE_MEMBERS"]); + await bot.utils.requireBotChannelPermissions(bot, channel, ["MANAGE_CHANNELS", "MUTE_MEMBERS", "MOVE_MEMBERS"]); } - if (!validateLength(topic, { max: 120, min: 1 })) { - throw new Error(Errors.INVALID_TOPIC_LENGTH); + if (!bot.utils.validateLength(topic, { max: 120, min: 1 })) { + throw new Error(bot.constants.Errors.INVALID_TOPIC_LENGTH); } - return await rest.runMethod( + return await bot.rest.runMethod( + bot.rest, "post", - endpoints.STAGE_INSTANCES, - snakelize({ - channelId, + bot.constants.endpoints.STAGE_INSTANCES, + { + channel_id: channelId, topic, - privacyLevel, - }) + privacy_level: privacyLevel, + } ); } diff --git a/src/helpers/channels/delete_channel.ts b/src/helpers/channels/delete_channel.ts index c9df3bcc1..1a6379303 100644 --- a/src/helpers/channels/delete_channel.ts +++ b/src/helpers/channels/delete_channel.ts @@ -1,27 +1,23 @@ -import { cacheHandlers } from "../../cache.ts"; -import { rest } from "../../rest/rest.ts"; -import { Errors } from "../../types/discordeno/errors.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotGuildPermissions } from "../../util/permissions.ts"; +import type { Bot } from "../../bot.ts"; /** Delete a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */ -export async function deleteChannel(channelId: bigint, reason?: string) { +export async function deleteChannel(bot: Bot, channelId: bigint, reason?: string) { const channel = await cacheHandlers.get("channels", channelId); if (channel?.guildId) { const guild = await cacheHandlers.get("guilds", channel.guildId); - if (!guild) throw new Error(Errors.GUILD_NOT_FOUND); + if (!guild) throw new Error(bot.constants.Errors.GUILD_NOT_FOUND); if (guild.rulesChannelId === channelId) { - throw new Error(Errors.RULES_CHANNEL_CANNOT_BE_DELETED); + throw new Error(bot.constants.Errors.RULES_CHANNEL_CANNOT_BE_DELETED); } if (guild.publicUpdatesChannelId === channelId) { - throw new Error(Errors.UPDATES_CHANNEL_CANNOT_BE_DELETED); + throw new Error(bot.constants.Errors.UPDATES_CHANNEL_CANNOT_BE_DELETED); } - await requireBotGuildPermissions(guild, ["MANAGE_CHANNELS"]); + await bot.utils.requireBotGuildPermissions(bot, guild, ["MANAGE_CHANNELS"]); } - return await rest.runMethod("delete", endpoints.CHANNEL_BASE(channelId), { reason }); + return await bot.rest.runMethod(bot.rest, "delete", bot.constants.endpoints.CHANNEL_BASE(channelId), { reason }); } diff --git a/src/helpers/channels/delete_channel_overwrite.ts b/src/helpers/channels/delete_channel_overwrite.ts index 40dd141f7..0f5d66ec8 100644 --- a/src/helpers/channels/delete_channel_overwrite.ts +++ b/src/helpers/channels/delete_channel_overwrite.ts @@ -1,14 +1,13 @@ -import { rest } from "../../rest/rest.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotGuildPermissions } from "../../util/permissions.ts"; +import type { Bot } from "../../bot.ts"; /** Delete the channel permission overwrites for a user or role in this channel. Requires `MANAGE_ROLES` permission. */ export async function deleteChannelOverwrite( + bot: Bot, guildId: bigint, channelId: bigint, overwriteId: bigint ): Promise { - await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]); + await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_ROLES"]); - return await rest.runMethod("delete", endpoints.CHANNEL_OVERWRITE(channelId, overwriteId)); + return await bot.rest.runMethod(bot.rest,"delete", bot.constants.endpoints.CHANNEL_OVERWRITE(channelId, overwriteId)); } diff --git a/src/helpers/channels/delete_stage_instance.ts b/src/helpers/channels/delete_stage_instance.ts index 735da6353..c8fb9e4c0 100644 --- a/src/helpers/channels/delete_stage_instance.ts +++ b/src/helpers/channels/delete_stage_instance.ts @@ -1,21 +1,17 @@ -import { cacheHandlers } from "../../cache.ts"; -import { rest } from "../../rest/rest.ts"; import { ChannelTypes } from "../../types/channels/channel_types.ts"; -import { Errors } from "../../types/discordeno/errors.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotChannelPermissions } from "../../util/permissions.ts"; +import type { Bot } from "../../bot.ts"; /** Deletes the Stage instance. Requires the user to be a moderator of the Stage channel. */ -export async function deleteStageInstance(channelId: bigint) { - const channel = await cacheHandlers.get("channels", channelId); +export async function deleteStageInstance(bot: Bot, channelId: bigint) { + const channel = await bot.cache.channels.get(channelId); if (channel) { if (channel.type !== ChannelTypes.GuildStageVoice) { - throw new Error(Errors.CHANNEL_NOT_STAGE_VOICE); + throw new Error(bot.constants.Errors.CHANNEL_NOT_STAGE_VOICE); } - await requireBotChannelPermissions(channel, ["MUTE_MEMBERS", "MANAGE_CHANNELS", "MOVE_MEMBERS"]); + await bot.utils.requireBotChannelPermissions(bot, channel, ["MUTE_MEMBERS", "MANAGE_CHANNELS", "MOVE_MEMBERS"]); } - return await rest.runMethod("delete", endpoints.STAGE_INSTANCE(channelId)); + return await bot.rest.runMethod(bot.rest, "delete", bot.constants.endpoints.STAGE_INSTANCE(channelId)); } diff --git a/src/helpers/channels/edit_channel.ts b/src/helpers/channels/edit_channel.ts index 05e577954..e7e4defe3 100644 --- a/src/helpers/channels/edit_channel.ts +++ b/src/helpers/channels/edit_channel.ts @@ -1,21 +1,16 @@ -import { eventHandlers } from "../../bot.ts"; -import { cacheHandlers } from "../../cache.ts"; -import { rest } from "../../rest/rest.ts"; import type { DiscordenoChannel } from "../../structures/channel.ts"; -import { structures } from "../../structures/mod.ts"; import type { Channel } from "../../types/channels/channel.ts"; import type { ModifyChannel } from "../../types/channels/modify_channel.ts"; -import { endpoints } from "../../util/constants.ts"; -import { calculateBits, requireOverwritePermissions } from "../../util/permissions.ts"; -import { snakelize } from "../../util/utils.ts"; +import type { Bot } from "../../bot.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Update a channel's settings. Requires the `MANAGE_CHANNELS` permission for the guild. */ -export async function editChannel(channelId: bigint, options: ModifyChannel, reason?: string) { - const channel = await cacheHandlers.get("channels", channelId); +export async function editChannel(bot: Bot, channelId: bigint, options: ModifyChannel, reason?: string) { + const channel = await bot.cache.channels.get(channelId); if (channel) { if (options.permissionOverwrites && Array.isArray(options.permissionOverwrites)) { - await requireOverwritePermissions(channel.guildId, options.permissionOverwrites); + await bot.utils.requireOverwritePermissions(bot, channel.guildId, options.permissionOverwrites); } } @@ -40,16 +35,25 @@ export async function editChannel(channelId: bigint, options: ModifyChannel, rea request.items.push({ channelId, options, resolve, reject }); if (editChannelProcessing) return; editChannelProcessing = true; - processEditChannelQueue(); + processEditChannelQueue(bot); }); } } - const result = await rest.runMethod( + const result = await bot.rest.runMethod>( + bot.rest, "patch", - endpoints.CHANNEL_BASE(channelId), - snakelize({ - ...options, + bot.constants.endpoints.CHANNEL_BASE(channelId), + { + name: options.name, + topic: options.topic, + bitrate: options.bitrate, + userLimit: options.userLimit, + rateLimitPerUser: options.rateLimitPerUser, + position: options.position, + parentId: options.parentId, + nsfw: options.nsfw, + type: options.type, permissionOverwrites: options.permissionOverwrites ? options.permissionOverwrites?.map((overwrite) => { return { @@ -60,10 +64,10 @@ export async function editChannel(channelId: bigint, options: ModifyChannel, rea }) : undefined, reason, - }) + } ); - return await structures.createDiscordenoChannel(result); + return bot.transformers.channel(result); } interface EditChannelRequest { @@ -82,7 +86,7 @@ interface EditChannelRequest { const editChannelNameTopicQueue = new Map(); let editChannelProcessing = false; -function processEditChannelQueue() { +function processEditChannelQueue(bot: Bot) { if (!editChannelProcessing) return; const now = Date.now(); @@ -99,13 +103,13 @@ function processEditChannelQueue() { if (!details) return; - await editChannel(details.channelId, details.options) + await bot.helpers.editChannel(bot, details.channelId, details.options) .then((result) => details.resolve(result)) .catch(details.reject); const secondDetails = request.items.shift(); if (!secondDetails) return; - await editChannel(secondDetails.channelId, secondDetails.options) + await bot.helpers.editChannel(bot, secondDetails.channelId, secondDetails.options) .then((result) => secondDetails.resolve(result)) .catch(secondDetails.reject); return; @@ -114,7 +118,7 @@ function processEditChannelQueue() { if (editChannelNameTopicQueue.size) { setTimeout(() => { eventHandlers.debug?.("loop", `Running setTimeout in EDIT_CHANNEL file.`); - processEditChannelQueue(); + processEditChannelQueue(bot); }, 60000); } else { editChannelProcessing = false; diff --git a/src/helpers/channels/edit_channel_overwrite.ts b/src/helpers/channels/edit_channel_overwrite.ts index c9b6c2db7..bcfe5cf9d 100644 --- a/src/helpers/channels/edit_channel_overwrite.ts +++ b/src/helpers/channels/edit_channel_overwrite.ts @@ -1,20 +1,19 @@ -import { rest } from "../../rest/rest.ts"; import type { Overwrite } from "../../types/channels/overwrite.ts"; -import { endpoints } from "../../util/constants.ts"; -import { calculateBits, requireBotGuildPermissions } from "../../util/permissions.ts"; +import type { Bot } from "../../bot.ts"; /** Edit the channel permission overwrites for a user or role in this channel. Requires `MANAGE_ROLES` permission. */ export async function editChannelOverwrite( + bot: Bot, guildId: bigint, channelId: bigint, overwriteId: bigint, options: Omit ): Promise { - await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]); + await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_ROLES"]); - return await rest.runMethod("put", endpoints.CHANNEL_OVERWRITE(channelId, overwriteId), { - allow: calculateBits(options.allow), - deny: calculateBits(options.deny), + return await bot.rest.runMethod(bot.rest,"put", bot.constants.endpoints.CHANNEL_OVERWRITE(channelId, overwriteId), { + allow: bot.utils.calculateBits(options.allow), + deny: bot.utils.calculateBits(options.deny), type: options.type, }); } diff --git a/src/helpers/channels/follow_channel.ts b/src/helpers/channels/follow_channel.ts index bd9d7d6ad..9512f4b29 100644 --- a/src/helpers/channels/follow_channel.ts +++ b/src/helpers/channels/follow_channel.ts @@ -1,13 +1,11 @@ -import { rest } from "../../rest/rest.ts"; import type { FollowedChannel } from "../../types/channels/followed_channel.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotChannelPermissions } from "../../util/permissions.ts"; +import type { Bot } from "../../bot.ts"; /** Follow a News Channel to send messages to a target channel. Requires the `MANAGE_WEBHOOKS` permission in the target channel. Returns the webhook id. */ -export async function followChannel(sourceChannelId: bigint, targetChannelId: bigint) { - await requireBotChannelPermissions(targetChannelId, ["MANAGE_WEBHOOKS"]); +export async function followChannel(bot: Bot, sourceChannelId: bigint, targetChannelId: bigint) { + await bot.utils.requireBotChannelPermissions(bot, targetChannelId, ["MANAGE_WEBHOOKS"]); - const data = await rest.runMethod("post", endpoints.CHANNEL_FOLLOW(sourceChannelId), { + const data = await bot.rest.runMethod(bot.rest,"post", bot.constants.endpoints.CHANNEL_FOLLOW(sourceChannelId), { webhook_channel_id: targetChannelId, }); diff --git a/src/helpers/channels/get_channel.ts b/src/helpers/channels/get_channel.ts index 3b14c40c4..6866a1cf8 100644 --- a/src/helpers/channels/get_channel.ts +++ b/src/helpers/channels/get_channel.ts @@ -1,23 +1,21 @@ -import { cacheHandlers } from "../../cache.ts"; -import { rest } from "../../rest/rest.ts"; -import { structures } from "../../structures/mod.ts"; +import type { Bot } from "../../bot.ts"; import type { Channel } from "../../types/channels/channel.ts"; -import { snowflakeToBigint } from "../../util/bigint.ts"; -import { endpoints } from "../../util/constants.ts"; /** Fetches a single channel object from the api. * * ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your channels will be cached in your guild.** */ -export async function getChannel(channelId: bigint, addToCache = true) { - const result = await rest.runMethod("get", endpoints.CHANNEL_BASE(channelId)); +export async function getChannel(bot: Bot, channelId: bigint, addToCache = true) { + const result = await bot.rest.runMethod(bot.rest,"get", bot.constants.endpoints.CHANNEL_BASE(channelId)); - const discordenoChannel = await structures.createDiscordenoChannel( - result, - result.guildId ? snowflakeToBigint(result.guildId) : undefined + const discordenoChannel = bot.transformers.channel( + { + channel: result, + guildId: result.guildId ? bot.transformers.snowflake(result.guildId) : undefined + } ); if (addToCache) { - await cacheHandlers.set("channels", discordenoChannel.id, discordenoChannel); + await bot.cache.channels.set(discordenoChannel.id, discordenoChannel); } return discordenoChannel; diff --git a/src/helpers/channels/get_channel_webhooks.ts b/src/helpers/channels/get_channel_webhooks.ts index fc1bac9cb..99da26ab1 100644 --- a/src/helpers/channels/get_channel_webhooks.ts +++ b/src/helpers/channels/get_channel_webhooks.ts @@ -1,14 +1,12 @@ -import { rest } from "../../rest/rest.ts"; import type { Webhook } from "../../types/webhooks/webhook.ts"; import { Collection } from "../../util/collection.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotChannelPermissions } from "../../util/permissions.ts"; +import type { Bot } from "../../bot.ts"; /** Gets the webhooks for this channel. Requires MANAGE_WEBHOOKS */ -export async function getChannelWebhooks(channelId: bigint) { - await requireBotChannelPermissions(channelId, ["MANAGE_WEBHOOKS"]); +export async function getChannelWebhooks(bot: Bot, channelId: bigint) { + await bot.utils.requireBotChannelPermissions(bot, channelId, ["MANAGE_WEBHOOKS"]); - const result = await rest.runMethod("get", endpoints.CHANNEL_WEBHOOKS(channelId)); + const result = await bot.rest.runMethod(bot.rest,"get", bot.constants.endpoints.CHANNEL_WEBHOOKS(channelId)); return new Collection(result.map((webhook) => [webhook.id, webhook])); } diff --git a/src/helpers/channels/get_channels.ts b/src/helpers/channels/get_channels.ts index 30c78ecfd..49e9970d3 100644 --- a/src/helpers/channels/get_channels.ts +++ b/src/helpers/channels/get_channels.ts @@ -1,24 +1,21 @@ -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 { Collection } from "../../util/collection.ts"; -import { endpoints } from "../../util/constants.ts"; +import type { Bot } from "../../bot.ts"; /** Returns a list of guild channel objects. * * ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your channels will be cached in your guild.** */ -export async function getChannels(guildId: bigint, addToCache = true) { - const result = await rest.runMethod("get", endpoints.GUILD_CHANNELS(guildId)); +export async function getChannels(bot: Bot, guildId: bigint, addToCache = true) { + const result = await bot.rest.runMethod(bot.rest,"get", bot.constants.endpoints.GUILD_CHANNELS(guildId)); return new Collection( ( await Promise.all( result.map(async (res) => { - const discordenoChannel = await structures.createDiscordenoChannel(res, guildId); + const discordenoChannel = await bot.transformers.channel({channel: res, guildId}); if (addToCache) { - await cacheHandlers.set("channels", discordenoChannel.id, discordenoChannel); + await bot.cache.channels.set(discordenoChannel.id, discordenoChannel); } return discordenoChannel; diff --git a/src/helpers/channels/get_pins.ts b/src/helpers/channels/get_pins.ts index 1b58916c1..97c8ef1d5 100644 --- a/src/helpers/channels/get_pins.ts +++ b/src/helpers/channels/get_pins.ts @@ -1,11 +1,9 @@ -import { rest } from "../../rest/rest.ts"; -import { structures } from "../../structures/mod.ts"; import type { Message } from "../../types/messages/message.ts"; -import { endpoints } from "../../util/constants.ts"; +import type { Bot } from "../../bot.ts"; /** Get pinned messages in this channel. */ -export async function getPins(channelId: bigint) { - const result = await rest.runMethod("get", endpoints.CHANNEL_PINS(channelId)); +export async function getPins(bot: Bot, channelId: bigint) { + const result = await bot.rest.runMethod(bot.rest,"get", bot.constants.endpoints.CHANNEL_PINS(channelId)); - return Promise.all(result.map((res) => structures.createDiscordenoMessage(res))); + return result.map(bot.transformers.message); } diff --git a/src/helpers/channels/get_stage_instance.ts b/src/helpers/channels/get_stage_instance.ts index 5a3b4e633..bfb727e31 100644 --- a/src/helpers/channels/get_stage_instance.ts +++ b/src/helpers/channels/get_stage_instance.ts @@ -1,19 +1,16 @@ -import { cacheHandlers } from "../../cache.ts"; -import { rest } from "../../rest/rest.ts"; -import { ChannelTypes } from "../../types/channels/channel_types.ts"; +import type { ChannelTypes } from "../../types/channels/channel_types.ts"; import type { StageInstance } from "../../types/channels/stage_instance.ts"; -import { Errors } from "../../types/discordeno/errors.ts"; -import { endpoints } from "../../util/constants.ts"; +import type { Bot } from "../../bot.ts"; /** Gets the stage instance associated with the Stage channel, if it exists. */ -export async function getStageInstance(channelId: bigint) { - const channel = await cacheHandlers.get("channels", channelId); +export async function getStageInstance(bot: Bot, channelId: bigint) { + const channel = await bot.cache.channels.get(channelId); if (channel) { if (channel.type !== ChannelTypes.GuildStageVoice) { - throw new Error(Errors.CHANNEL_NOT_STAGE_VOICE); + throw new Error(bot.constants.Errors.CHANNEL_NOT_STAGE_VOICE); } } - return await rest.runMethod("get", endpoints.STAGE_INSTANCE(channelId)); + return await bot.rest.runMethod(bot.rest,"get", bot.constants.endpoints.STAGE_INSTANCE(channelId)); } diff --git a/src/helpers/channels/is_channel_synced.ts b/src/helpers/channels/is_channel_synced.ts index ed6798f78..b3c4c2a03 100644 --- a/src/helpers/channels/is_channel_synced.ts +++ b/src/helpers/channels/is_channel_synced.ts @@ -1,11 +1,11 @@ -import { cacheHandlers } from "../../cache.ts"; +import type { Bot } from "../../bot.ts"; /** Checks whether a channel is synchronized with its parent/category channel or not. */ -export async function isChannelSynced(channelId: bigint) { - const channel = await cacheHandlers.get("channels", channelId); +export async function isChannelSynced(bot: Bot, channelId: bigint) { + const channel = await bot.cache.channels.get(channelId); if (!channel?.parentId) return false; - const parentChannel = await cacheHandlers.get("channels", channel.parentId); + const parentChannel = await bot.cache.channels.get(channel.parentId); if (!parentChannel) return false; return channel.permissionOverwrites?.every((overwrite) => { diff --git a/src/helpers/channels/start_typing.ts b/src/helpers/channels/start_typing.ts index 5fd7a922e..6999e6cec 100644 --- a/src/helpers/channels/start_typing.ts +++ b/src/helpers/channels/start_typing.ts @@ -1,17 +1,13 @@ -import { cacheHandlers } from "../../cache.ts"; -import { rest } from "../../rest/rest.ts"; import { DiscordChannelTypes } from "../../types/channels/channel_types.ts"; -import { Errors } from "../../types/discordeno/errors.ts"; -import { endpoints } from "../../util/constants.ts"; -import { botHasChannelPermissions } from "../../util/permissions.ts"; +import type { Bot } from "../../bot.ts"; /** * Trigger a typing indicator for the specified channel. Generally bots should **NOT** implement this route. * However, if a bot is responding to a command and expects the computation to take a few seconds, * this endpoint may be called to let the user know that the bot is processing their message. */ -export async function startTyping(channelId: bigint) { - const channel = await cacheHandlers.get("channels", channelId); +export async function startTyping(bot: Bot, channelId: bigint) { + const channel = await bot.cache.channels.get(channelId); // If the channel is cached, we can do extra checks/safety if (channel) { if ( @@ -24,14 +20,14 @@ export async function startTyping(channelId: bigint) { DiscordChannelTypes.GuildPublicThread, ].includes(channel.type) ) { - throw new Error(Errors.CHANNEL_NOT_TEXT_BASED); + throw new Error(bot.constants.Errors.CHANNEL_NOT_TEXT_BASED); } - const hasSendMessagesPerm = await botHasChannelPermissions(channelId, ["SEND_MESSAGES"]); + const hasSendMessagesPerm = await bot.utils.botHasChannelPermissions(bots, channelId, ["SEND_MESSAGES"]); if (!hasSendMessagesPerm) { - throw new Error(Errors.MISSING_SEND_MESSAGES); + throw new Error(bot.constants.Errors.MISSING_SEND_MESSAGES); } } - return await rest.runMethod("post", endpoints.CHANNEL_TYPING(channelId)); + return await bot.rest.runMethod(bot.rest,"post", bot.constants.endpoints.CHANNEL_TYPING(channelId)); } diff --git a/src/helpers/channels/swap_channels.ts b/src/helpers/channels/swap_channels.ts index cc515e159..22618b7a0 100644 --- a/src/helpers/channels/swap_channels.ts +++ b/src/helpers/channels/swap_channels.ts @@ -1,13 +1,18 @@ -import { rest } from "../../rest/rest.ts"; import type { ModifyGuildChannelPositions } from "../../types/guilds/modify_guild_channel_position.ts"; -import { endpoints } from "../../util/constants.ts"; -import { snakelize } from "../../util/utils.ts"; +import type { Bot } from "../../bot.ts"; -/** Modify the positions of channels on the guild. Requires MANAGE_CHANNELS permisison. */ -export async function swapChannels(guildId: bigint, channelPositions: ModifyGuildChannelPositions[]) { +/** Modify the positions of channels on the guild. Requires MANAGE_CHANNELS permission. */ +export async function swapChannels(bot: Bot, guildId: bigint, channelPositions: ModifyGuildChannelPositions[]) { if (channelPositions.length < 2) { throw "You must provide at least two channels to be swapped."; } - return await rest.runMethod("patch", endpoints.GUILD_CHANNELS(guildId), snakelize(channelPositions)); + return await bot.rest.runMethod(bot.rest,"patch", bot.constants.endpoints.GUILD_CHANNELS(guildId), channelPositions.map((channelPosition) => { + return { + id: channelPosition.id, + position: channelPosition.position, + lock_positions: channelPosition.lockPositions, + parent_id: channelPosition.parentId + } + })); } diff --git a/src/helpers/channels/update_stage_instance.ts b/src/helpers/channels/update_stage_instance.ts index c0cbe8e31..4a98472ed 100644 --- a/src/helpers/channels/update_stage_instance.ts +++ b/src/helpers/channels/update_stage_instance.ts @@ -1,37 +1,35 @@ -import { rest } from "../../rest/rest.ts"; -import { Errors } from "../../types/discordeno/errors.ts"; import type { StageInstance } from "../../types/channels/stage_instance.ts"; -import { endpoints } from "../../util/constants.ts"; -import { validateLength } from "../../util/validate_length.ts"; -import { cacheHandlers } from "../../cache.ts"; -import { requireBotChannelPermissions } from "../../util/permissions.ts"; -import { ChannelTypes } from "../../types/channels/channel_types.ts"; -import { snakelize } from "../../util/utils.ts"; +import type { Bot } from "../../bot.ts"; +import type { ChannelTypes } from "../../types/channels/channel_types.ts"; /** Updates fields of an existing Stage instance. Requires the user to be a moderator of the Stage channel. */ export async function updateStageInstance( + bot: Bot, channelId: bigint, data: Partial> = {} ) { - const channel = await cacheHandlers.get("channels", channelId); + const channel = await bot.cache.channels.get(channelId); if (channel) { if (channel.type !== ChannelTypes.GuildStageVoice) { - throw new Error(Errors.CHANNEL_NOT_STAGE_VOICE); + throw new Error(bot.constants.Errors.CHANNEL_NOT_STAGE_VOICE); } - await requireBotChannelPermissions(channel, ["MOVE_MEMBERS", "MUTE_MEMBERS", "MANAGE_CHANNELS"]); + await bot.utils.requireBotChannelPermissions(channel, ["MOVE_MEMBERS", "MUTE_MEMBERS", "MANAGE_CHANNELS"]); } if ( data?.topic && - !validateLength(data.topic, { + !bot.utils.validateLength(data.topic, { min: 1, max: 120, }) ) { - throw new Error(Errors.INVALID_TOPIC_LENGTH); + throw new Error(bot.constants.Errors.INVALID_TOPIC_LENGTH); } - return await rest.runMethod("patch", endpoints.STAGE_INSTANCE(channelId), snakelize(data)); + return await bot.rest.runMethod(bot.rest,"patch", bot.constants.endpoints.STAGE_INSTANCE(channelId), { + topic: data.topic, + privacy_level: data.privacyLevel + }); } diff --git a/src/helpers/channels/update_voice_state.ts b/src/helpers/channels/update_voice_state.ts index f2e37f9cf..7960733ae 100644 --- a/src/helpers/channels/update_voice_state.ts +++ b/src/helpers/channels/update_voice_state.ts @@ -1,8 +1,6 @@ -import { rest } from "../../rest/rest.ts"; -import { UpdateOthersVoiceState } from "../../types/guilds/update_others_voice_state.ts"; +import type { UpdateOthersVoiceState } from "../../types/guilds/update_others_voice_state.ts"; import type { UpdateSelfVoiceState } from "../../types/guilds/update_self_voice_state.ts"; -import { endpoints } from "../../util/constants.ts"; -import { hasOwnProperty, snakelize } from "../../util/utils.ts"; +import type { Bot } from "../../bot.ts"; /** * Updates the a user's voice state, defaults to the current user @@ -19,9 +17,15 @@ export async function updateBotVoiceState( guildId: bigint, options: UpdateSelfVoiceState | ({ userId: bigint } & UpdateOthersVoiceState) ) { - return await rest.runMethod( + return await bot.rest.runMethod( + bot.rest, "patch", - endpoints.UPDATE_VOICE_STATE(guildId, hasOwnProperty(options, "userId") ? options.userId : undefined), - snakelize(options) + bot.constants.endpoints.UPDATE_VOICE_STATE(guildId, bot.utils.hasOwnProperty(options, "userId") ? options.userId : undefined), + { + channel_id: options.channelId, + suppress: options.suppress, + request_to_speak_timestamp: options.requestToSpeakTimestamp, + user_id: options.userId + } ); }