diff --git a/src/helpers/messages/delete_message.ts b/src/helpers/messages/delete_message.ts index ad12e2992..6926f1011 100644 --- a/src/helpers/messages/delete_message.ts +++ b/src/helpers/messages/delete_message.ts @@ -1,3 +1,4 @@ +import { cacheHandlers } from "../../cache.ts"; import type { Bot } from "../../bot.ts"; /** Delete a message with the channel id and message id only. */ diff --git a/src/helpers/messages/get_reactions.ts b/src/helpers/messages/get_reactions.ts index 7bf1ba976..600d682e9 100644 --- a/src/helpers/messages/get_reactions.ts +++ b/src/helpers/messages/get_reactions.ts @@ -1,14 +1,14 @@ -import { rest } from "../../rest/rest.ts"; import type { GetReactions } from "../../types/messages/message_get_reactions.ts"; import type { User } from "../../types/users/user.ts"; import { Collection } from "../../util/collection.ts"; -import { endpoints } from "../../util/constants.ts"; +import type { Bot } from "../../bot.ts"; /** Get a list of users that reacted with this emoji. */ -export async function getReactions(channelId: bigint, messageId: bigint, reaction: string, options?: GetReactions) { - const users = await rest.runMethod( +export async function getReactions(bot: Bot, channelId: bigint, messageId: bigint, reaction: string, options?: GetReactions) { + const users = await bot.rest.runMethod( + bot.rest, "get", - endpoints.CHANNEL_MESSAGE_REACTION(channelId, messageId, reaction), + bot.constants.endpoints.CHANNEL_MESSAGE_REACTION(channelId, messageId, reaction), options ); diff --git a/src/helpers/messages/pin_message.ts b/src/helpers/messages/pin_message.ts index 9bc16270c..2ef455fc2 100644 --- a/src/helpers/messages/pin_message.ts +++ b/src/helpers/messages/pin_message.ts @@ -1,12 +1,10 @@ -import { rest } from "../../rest/rest.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotChannelPermissions } from "../../util/permissions.ts"; +import type { Bot } from "../../bot.ts"; /** Pin a message in a channel. Requires MANAGE_MESSAGES. Max pins allowed in a channel = 50. */ -export async function pin(channelId: bigint, messageId: bigint) { - await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]); +export async function pin(bot: Bot, channelId: bigint, messageId: bigint) { + await bot.utils.requireBotChannelPermissions(bot, channelId, ["MANAGE_MESSAGES"]); - return await rest.runMethod("put", endpoints.CHANNEL_PIN(channelId, messageId)); + return await bot.rest.runMethod(bot.rest,"put", bot.constants.endpoints.CHANNEL_PIN(channelId, messageId)); } // aliases diff --git a/src/helpers/messages/publish_message.ts b/src/helpers/messages/publish_message.ts index e2c858c43..0e0a62e03 100644 --- a/src/helpers/messages/publish_message.ts +++ b/src/helpers/messages/publish_message.ts @@ -1,11 +1,10 @@ -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 {Bot} from "../../bot.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Crosspost a message in a News Channel to following channels. */ -export async function publishMessage(channelId: bigint, messageId: bigint) { - const data = await rest.runMethod("post", endpoints.CHANNEL_MESSAGE_CROSSPOST(channelId, messageId)); +export async function publishMessage(bot: Bot, channelId: bigint, messageId: bigint) { + const data = await bot.rest.runMethod>(bot.rest, "post", bot.constants.endpoints.CHANNEL_MESSAGE_CROSSPOST(channelId, messageId)); - return await structures.createDiscordenoMessage(data); + return await bot.transformers.message(bot, data); } diff --git a/src/helpers/messages/remove_all_reactions.ts b/src/helpers/messages/remove_all_reactions.ts index a2682e71b..db45bac29 100644 --- a/src/helpers/messages/remove_all_reactions.ts +++ b/src/helpers/messages/remove_all_reactions.ts @@ -1,10 +1,8 @@ -import { rest } from "../../rest/rest.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotChannelPermissions } from "../../util/permissions.ts"; +import {Bot} from "../../bot.ts"; /** Removes all reactions for all emojis on this message. */ -export async function removeAllReactions(channelId: bigint, messageId: bigint) { - await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]); +export async function removeAllReactions(bot: Bot, channelId: bigint, messageId: bigint) { + await bot.utils.requireBotChannelPermissions(bot, channelId, ["MANAGE_MESSAGES"]); - return await rest.runMethod("delete", endpoints.CHANNEL_MESSAGE_REACTIONS(channelId, messageId)); + return await bot.rest.runMethod(bot.rest,"delete", bot.constants.endpoints.CHANNEL_MESSAGE_REACTIONS(channelId, messageId)); } diff --git a/src/helpers/messages/remove_reaction.ts b/src/helpers/messages/remove_reaction.ts index 081488cad..3eb7e5bb2 100644 --- a/src/helpers/messages/remove_reaction.ts +++ b/src/helpers/messages/remove_reaction.ts @@ -1,16 +1,15 @@ -import { rest } from "../../rest/rest.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotChannelPermissions } from "../../util/permissions.ts"; +import {Bot} from "../../bot.ts"; /** Removes a reaction from the given user on this message, defaults to bot. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */ export async function removeReaction( + bot: Bot, channelId: bigint, messageId: bigint, reaction: string, options?: { userId?: bigint } ) { if (options?.userId) { - await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]); + await bot.utils.requireBotChannelPermissions(bot, channelId, ["MANAGE_MESSAGES"]); } if (reaction.startsWith("<:")) { @@ -19,10 +18,11 @@ export async function removeReaction( reaction = reaction.substring(3, reaction.length - 1); } - return await rest.runMethod( + return await bot.rest.runMethod( + bot.rest, "delete", options?.userId - ? endpoints.CHANNEL_MESSAGE_REACTION_USER(channelId, messageId, reaction, options.userId) - : endpoints.CHANNEL_MESSAGE_REACTION_ME(channelId, messageId, reaction) + ? bot.constants.endpoints.CHANNEL_MESSAGE_REACTION_USER(channelId, messageId, reaction, options.userId) + : bot.constants.endpoints.CHANNEL_MESSAGE_REACTION_ME(channelId, messageId, reaction) ); } diff --git a/src/helpers/messages/remove_reaction_emoji.ts b/src/helpers/messages/remove_reaction_emoji.ts index 69a70805d..f9bac3de7 100644 --- a/src/helpers/messages/remove_reaction_emoji.ts +++ b/src/helpers/messages/remove_reaction_emoji.ts @@ -1,10 +1,9 @@ -import { rest } from "../../rest/rest.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotChannelPermissions } from "../../util/permissions.ts"; - /** Removes all reactions for a single emoji on this message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */ -export async function removeReactionEmoji(channelId: bigint, messageId: bigint, reaction: string) { - await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]); +import {Bot} from "../../bot.ts"; + + +export async function removeReactionEmoji(bot: Bot, channelId: bigint, messageId: bigint, reaction: string) { + await bot.utils.requireBotChannelPermissions(bot, channelId, ["MANAGE_MESSAGES"]); if (reaction.startsWith("<:")) { reaction = reaction.substring(2, reaction.length - 1); @@ -12,5 +11,5 @@ export async function removeReactionEmoji(channelId: bigint, messageId: bigint, reaction = reaction.substring(3, reaction.length - 1); } - return await rest.runMethod("delete", endpoints.CHANNEL_MESSAGE_REACTION(channelId, messageId, reaction)); + return await bot.rest.runMethod(bot.rest,"delete", bot.constants.endpoints.CHANNEL_MESSAGE_REACTION(channelId, messageId, reaction)); } diff --git a/src/helpers/messages/send_message.ts b/src/helpers/messages/send_message.ts index 0b4ae5f48..e3ac7c724 100644 --- a/src/helpers/messages/send_message.ts +++ b/src/helpers/messages/send_message.ts @@ -1,19 +1,15 @@ import { cacheHandlers } from "../../cache.ts"; -import { rest } from "../../rest/rest.ts"; -import { structures } from "../../structures/mod.ts"; import { DiscordChannelTypes } from "../../types/channels/channel_types.ts"; import { Errors } from "../../types/discordeno/errors.ts"; import { DiscordAllowedMentionsTypes } from "../../types/messages/allowed_mentions_types.ts"; import type { CreateMessage } from "../../types/messages/create_message.ts"; import type { Message } from "../../types/messages/message.ts"; import type { PermissionStrings } from "../../types/permissions/permission_strings.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotChannelPermissions } from "../../util/permissions.ts"; -import { snakelize, validateComponents } from "../../util/utils.ts"; -import { validateLength } from "../../util/validate_length.ts"; +import type { Bot } from "../../bot.ts"; +import type { SnakeCasedPropertiesDeep } from "../../types/util.ts"; /** Send a message to the channel. Requires SEND_MESSAGES permission. */ -export async function sendMessage(channelId: bigint, content: string | CreateMessage) { +export async function sendMessage(bot: Bot, channelId: bigint, content: string | CreateMessage) { if (typeof content === "string") content = { content }; const channel = await cacheHandlers.get("channels", channelId); @@ -43,16 +39,16 @@ export async function sendMessage(channelId: bigint, content: string | CreateMes requiredPerms.add("READ_MESSAGE_HISTORY"); } - await requireBotChannelPermissions(channelId, [...requiredPerms]); + await bot.utils.requireBotChannelPermissions(bot, channelId, [...requiredPerms]); } // Use ... for content length due to unicode characters and js .length handling - if (content.content && !validateLength(content.content, { max: 2000 })) { - throw new Error(Errors.MESSAGE_MAX_LENGTH); + if (content.content && !bot.utils.validateLength(content.content, { max: 2000 })) { + throw new Error(bot.constants.Errors.MESSAGE_MAX_LENGTH); } if (content.components?.length) { - validateComponents(content.components); + bot.utils.validateComponents(bot, content.components); } if (content.allowedMentions) { @@ -77,10 +73,11 @@ export async function sendMessage(channelId: bigint, content: string | CreateMes } } - const result = await rest.runMethod( + const result = await bot.rest.runMethod>( + bot.rest, "post", - endpoints.CHANNEL_MESSAGES(channelId), - snakelize({ + bot.constants.endpoints.CHANNEL_MESSAGES(channelId), + bot.utils.snakelize({ ...content, ...(content.messageReference?.messageId ? { @@ -93,5 +90,5 @@ export async function sendMessage(channelId: bigint, content: string | CreateMes }) ); - return structures.createDiscordenoMessage(result); + return bot.transformers.message(bot, result); } diff --git a/src/helpers/messages/suppress_embeds.ts b/src/helpers/messages/suppress_embeds.ts index c4d0efdcd..59bc2400d 100644 --- a/src/helpers/messages/suppress_embeds.ts +++ b/src/helpers/messages/suppress_embeds.ts @@ -1,26 +1,20 @@ -import { botId } from "../../bot.ts"; +import type { Bot } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; -import { rest } from "../../rest/rest.ts"; -import { structures } from "../../structures/mod.ts"; -import { Errors } from "../../types/discordeno/errors.ts"; -import { EditMessage } from "../../types/messages/edit_message.ts"; import type { Message } from "../../types/messages/message.ts"; -import type { PermissionStrings } from "../../types/permissions/permission_strings.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotChannelPermissions } from "../../util/permissions.ts"; -import { snakelize, validateComponents } from "../../util/utils.ts"; +import type { SnakeCasedPropertiesDeep } from "../../types/util.ts"; /** Suppress all the embeds in this message */ -export async function suppressEmbeds(channelId: bigint, messageId: bigint) { +export async function suppressEmbeds(bot: Bot, channelId: bigint, messageId: bigint) { const message = await cacheHandlers.get("messages", messageId); - await requireBotChannelPermissions(channelId, message ? ["MANAGE_MESSAGES"] : ["SEND_MESSAGES"]); + await bot.utils.requireBotChannelPermissions(bot, channelId, message ? ["MANAGE_MESSAGES"] : ["SEND_MESSAGES"]); - const result = await rest.runMethod( + const result = await bot.rest.runMethod>( + bot.rest, "patch", - endpoints.CHANNEL_MESSAGE(channelId, messageId), - snakelize({ flags: 4 }) + bot.constants.endpoints.CHANNEL_MESSAGE(channelId, messageId), + bot.utils.snakelize({ flags: 4 }) ); - return await structures.createDiscordenoMessage(result); + return await bot.transformers.message(bot, result); } diff --git a/src/helpers/messages/unpin_message.ts b/src/helpers/messages/unpin_message.ts index ee7542c42..b8a6e01e7 100644 --- a/src/helpers/messages/unpin_message.ts +++ b/src/helpers/messages/unpin_message.ts @@ -1,12 +1,10 @@ -import { rest } from "../../rest/rest.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotChannelPermissions } from "../../util/permissions.ts"; - /** Unpin a message in a channel. Requires MANAGE_MESSAGES. */ -export async function unpin(channelId: bigint, messageId: bigint): Promise { - await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]); +import {Bot} from "../../bot.ts"; - return await rest.runMethod("delete", endpoints.CHANNEL_PIN(channelId, messageId)); +export async function unpin(bot: Bot, channelId: bigint, messageId: bigint): Promise { + await bot.utils.requireBotChannelPermissions(bot, channelId, ["MANAGE_MESSAGES"]); + + return await bot.rest.runMethod(bot.rest,"delete", bot.constants.endpoints.CHANNEL_PIN(channelId, messageId)); } // aliases