From 9e664bfcbe06372e73cd5ad65123025c11177bfc Mon Sep 17 00:00:00 2001 From: TriForMine Date: Fri, 15 Oct 2021 14:53:39 +0200 Subject: [PATCH 01/34] Starting to convert helpers --- src/bot.ts | 88 ++++++++++--------------- src/helpers/messages/add_reaction.ts | 10 ++- src/helpers/messages/add_reactions.ts | 10 +-- src/helpers/messages/delete_message.ts | 13 ++-- src/helpers/messages/delete_messages.ts | 13 ++-- src/helpers/messages/edit_message.ts | 29 ++++---- src/helpers/messages/get_message.ts | 14 ++-- src/rest/mod.ts | 2 + src/ws/mod.ts | 2 + 9 files changed, 75 insertions(+), 106 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 341d5cea6..7fa34a338 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -11,63 +11,46 @@ import { requireChannelPermissions, highestRole, higherRolePosition, + requireBotChannelPermissions } from "./util/permissions.ts"; import { getGatewayBot } from "./helpers/misc/get_gateway_bot.ts"; -import { checkRateLimits } from "./rest/check_rate_limits.ts"; -import { cleanupQueues } from "./rest/cleanup_queues.ts"; -import { createRequestBody } from "./rest/create_request_body.ts"; -import { processRateLimitedPaths } from "./rest/process_rate_limited_paths.ts"; -import { processRequest } from "./rest/process_request.ts"; -import { processRequestHeaders } from "./rest/process_request_headers.ts"; +import { checkRateLimits, processQueue, cleanupQueues, createRequestBody, processRateLimitedPaths, processRequest, processRequestHeaders, runMethod, processGlobalQueue, simplifyUrl } from "./rest/mod.ts"; import type { RestPayload, RestRateLimitedPath, RestRequest } from "./rest/rest.ts"; -import { runMethod } from "./rest/run_method.ts"; -import { simplifyUrl } from "./rest/simplify_url.ts"; import { DiscordGatewayIntents } from "./types/gateway/gateway_intents.ts"; import { GetGatewayBot } from "./types/gateway/get_gateway_bot.ts"; -import { dispatchRequirements } from "./util/dispatch_requirements.ts"; -import { processQueue } from "./rest/process_queue.ts"; -import { bigintToSnowflake, snowflakeToBigint } from "./util/bigint.ts"; +import {bigintToSnowflake, snowflakeToBigint} from "./util/bigint.ts"; import { Collection } from "./util/collection.ts"; -import type { DiscordenoMember, DiscordenoUser } from "./transformers/member.ts"; -import { transformMember, transformUser } from "./transformers/member.ts"; -import { SnakeCasedPropertiesDeep } from "./types/util.ts"; -import { Channel } from "./types/channels/channel.ts"; -import { DiscordenoChannel, transformChannel } from "./transformers/channel.ts"; -import { DiscordenoVoiceState, transformVoiceState } from "./transformers/voice_state.ts"; -import { transformRole } from "./transformers/role.ts"; -import { transformMessage } from "./transformers/message.ts"; -import { DiscordenoGuild, transformGuild } from "./transformers/guild.ts"; -import type { DiscordenoShard } from "./ws/ws.ts"; -import { startGateway } from "./ws/start_gateway.ts"; -import { spawnShards } from "./ws/spawn_shards.ts"; -import { createShard } from "./ws/create_shard.ts"; -import { identify } from "./ws/identify.ts"; -import { heartbeat } from "./ws/heartbeat.ts"; -import { resharder } from "./ws/resharder.ts"; -import { tellClusterToIdentify } from "./ws/tell_cluster_to_identify.ts"; -import { handleDiscordPayload } from "./ws/handle_discord_payload.ts"; -import { log } from "./ws/events.ts"; -import { handleOnMessage } from "./ws/handle_on_message.ts"; -import { closeWS } from "./ws/close_ws.ts"; -import { sendShardMessage } from "./ws/send_shard_message.ts"; -import { resume } from "./ws/resume.ts"; -import { calculateShardId } from "./util/calculate_shard_id.ts"; +import {DiscordenoMember, DiscordenoUser, transformMember, transformUser, DiscordenoGuild, transformGuild, DiscordenoChannel, transformChannel, transformMessage, transformRole, DiscordenoVoiceState, transformVoiceState} from "./transformers/mod.ts"; import { - baseEndpoints, - CHANNEL_MENTION_REGEX, - CONTEXT_MENU_COMMANDS_NAME_REGEX, + baseEndpoints, CHANNEL_MENTION_REGEX, + CONTEXT_MENU_COMMANDS_NAME_REGEX, DISCORD_SNOWFLAKE_REGEX, DISCORDENO_VERSION, - DISCORD_SNOWFLAKE_REGEX, endpoints, SLASH_COMMANDS_NAME_REGEX, - USER_AGENT, + USER_AGENT } from "./util/constants.ts"; -import { GatewayPayload } from "./types/gateway/gateway_payload.ts"; -import { delay, validateSlashOptionChoices, validateSlashOptions } from "./util/utils.ts"; -import { iconBigintToHash, iconHashToBigInt } from "./util/hash.ts"; -import { validateLength } from "./util/validate_length.ts"; -import { processGlobalQueue } from "./rest/process_global_queue.ts"; -import { ChannelPinsUpdate } from "./types/channels/channel_pins_update.ts"; +import {Errors} from "./types/discordeno/errors.ts"; +import {GatewayPayload} from "./types/gateway/gateway_payload.ts"; +import { + closeWS, + handleOnMessage, + resume, + resharder, + log, + startGateway, + spawnShards, + createShard, + identify, + heartbeat, + handleDiscordPayload, + tellClusterToIdentify, + sendShardMessage, + DiscordenoShard +} from "./ws/mod.ts"; +import {validateLength} from "./util/validate_length.ts"; +import {delay, validateSlashOptionChoices, validateSlashOptions} from "./util/utils.ts"; +import {iconBigintToHash, iconHashToBigInt} from "./util/hash.ts"; +import {calculateShardId} from "./util/calculate_shard_id.ts"; export async function createBot(options: CreateBotOptions) { return { @@ -274,6 +257,7 @@ export function createUtils(options: Partial) { validateLength, validateSlashOptions, validateSlashOptionChoices, + requireBotChannelPermissions, }; } @@ -299,6 +283,7 @@ export interface HelperUtils { validateLength: typeof validateLength; validateSlashOptions: typeof validateSlashOptions; validateSlashOptionChoices: typeof validateSlashOptionChoices; + requireBotChannelPermissions: typeof requireBotChannelPermissions; } export function createGatewayManager(options: Partial): GatewayManager { @@ -331,7 +316,6 @@ export function createGatewayManager(options: Partial): GatewayM loadingShards: options.loadingShards ?? new Collection(), buckets: new Collection(), utf8decoder: new TextDecoder(), - startGateway, spawnShards, createShard, @@ -379,8 +363,8 @@ export interface Transformers { snowflake: typeof snowflakeToBigint; channel: typeof transformChannel; guild: typeof transformGuild; - user: typeof transformUser; member: typeof transformMember; + user: typeof transformUser; message: typeof transformMessage; role: typeof transformRole; voiceState: typeof transformVoiceState; @@ -389,13 +373,6 @@ export interface Transformers { export function createTransformers(options: Partial) { return { snowflake: options.snowflake || snowflakeToBigint, - channel: options.channel || transformChannel, - guild: options.guild || transformGuild, - user: options.user || transformUser, - member: options.member || transformMember, - message: options.message || transformMessage, - role: options.role || transformRole, - voiceState: options.voiceState || transformVoiceState, }; } @@ -554,5 +531,6 @@ export function createBotConstants() { CHANNEL_MENTION_REGEX, DISCORD_SNOWFLAKE_REGEX, }, + Errors }; } diff --git a/src/helpers/messages/add_reaction.ts b/src/helpers/messages/add_reaction.ts index cc8691179..7ed7d6536 100644 --- a/src/helpers/messages/add_reaction.ts +++ b/src/helpers/messages/add_reaction.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"; /** Create a reaction for the message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. Requires READ_MESSAGE_HISTORY and ADD_REACTIONS */ -export async function addReaction(channelId: bigint, messageId: bigint, reaction: string) { - await requireBotChannelPermissions(channelId, ["ADD_REACTIONS", "READ_MESSAGE_HISTORY"]); +export async function addReaction(bot: Bot, channelId: bigint, messageId: bigint, reaction: string) { + await bot.utils.requireBotChannelPermissions(channelId, ["ADD_REACTIONS", "READ_MESSAGE_HISTORY"]); if (reaction.startsWith("<:")) { reaction = reaction.substring(2, reaction.length - 1); @@ -12,5 +10,5 @@ export async function addReaction(channelId: bigint, messageId: bigint, reaction reaction = reaction.substring(3, reaction.length - 1); } - return await rest.runMethod("put", endpoints.CHANNEL_MESSAGE_REACTION_ME(channelId, messageId, reaction)); + return await bot.rest.runMethod(bot.rest, "put", bot.constants.endpoints.CHANNEL_MESSAGE_REACTION_ME(channelId, messageId, reaction)); } diff --git a/src/helpers/messages/add_reactions.ts b/src/helpers/messages/add_reactions.ts index 00d3d6803..a2cb0e4e4 100644 --- a/src/helpers/messages/add_reactions.ts +++ b/src/helpers/messages/add_reactions.ts @@ -1,14 +1,14 @@ -import { eventHandlers } from "../../bot.ts"; +import {Bot} from "../../bot.ts"; import { addReaction } from "./add_reaction.ts"; /** Adds multiple reactions to a message. If `ordered` is true(default is false), it will add the reactions one at a time in the order provided. Note: Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. Requires READ_MESSAGE_HISTORY and ADD_REACTIONS */ -export async function addReactions(channelId: bigint, messageId: bigint, reactions: string[], ordered = false) { +export async function addReactions(bot: Bot, channelId: bigint, messageId: bigint, reactions: string[], ordered = false) { if (!ordered) { - await Promise.all(reactions.map((reaction) => addReaction(channelId, messageId, reaction))); + await Promise.all(reactions.map((reaction) => addReaction(bot, channelId, messageId, reaction))); } else { for (const reaction of reactions) { - eventHandlers.debug?.("loop", "Running for of loop in addReactions function."); - await addReaction(channelId, messageId, reaction); + bot.events.debug?.("loop", "Running for of loop in addReactions function."); + await addReaction(bot, channelId, messageId, reaction); } } } diff --git a/src/helpers/messages/delete_message.ts b/src/helpers/messages/delete_message.ts index 1753254cf..2d704dbd5 100644 --- a/src/helpers/messages/delete_message.ts +++ b/src/helpers/messages/delete_message.ts @@ -1,19 +1,16 @@ -import { botId } from "../../bot.ts"; +import { Bot } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; -import { rest } from "../../rest/rest.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotChannelPermissions } from "../../util/permissions.ts"; import { delay } from "../../util/utils.ts"; /** Delete a message with the channel id and message id only. */ -export async function deleteMessage(channelId: bigint, messageId: bigint, reason?: string, delayMilliseconds = 0) { +export async function deleteMessage(bot: Bot, channelId: bigint, messageId: bigint, reason?: string, delayMilliseconds = 0) { const message = await cacheHandlers.get("messages", messageId); - if (message && message.authorId !== botId) { - await requireBotChannelPermissions(message.channelId, ["MANAGE_MESSAGES"]); + if (message && message.authorId !== bot.id) { + await bot.utils.requireBotChannelPermissions(message.channelId, ["MANAGE_MESSAGES"]); } if (delayMilliseconds) await delay(delayMilliseconds); - return await rest.runMethod("delete", endpoints.CHANNEL_MESSAGE(channelId, messageId), { reason }); + return await bot.rest.runMethod(bot.rest,"delete", bot.constants.endpoints.CHANNEL_MESSAGE(channelId, messageId), { reason }); } diff --git a/src/helpers/messages/delete_messages.ts b/src/helpers/messages/delete_messages.ts index f95ec54dd..049b7f2d9 100644 --- a/src/helpers/messages/delete_messages.ts +++ b/src/helpers/messages/delete_messages.ts @@ -1,21 +1,18 @@ -import { rest } from "../../rest/rest.ts"; -import { Errors } from "../../types/discordeno/errors.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotChannelPermissions } from "../../util/permissions.ts"; +import { Bot } from "../../bot.ts"; /** Delete messages from the channel. 2-100. Requires the MANAGE_MESSAGES permission */ -export async function deleteMessages(channelId: bigint, ids: bigint[], reason?: string) { - await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]); +export async function deleteMessages(bot: Bot, channelId: bigint, ids: bigint[], reason?: string) { + await bot.utils.requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]); if (ids.length < 2) { - throw new Error(Errors.DELETE_MESSAGES_MIN); + throw new Error(bot.constants.Errors.DELETE_MESSAGES_MIN); } if (ids.length > 100) { console.warn(`This endpoint only accepts a maximum of 100 messages. Deleting the first 100 message ids provided.`); } - return await rest.runMethod("post", endpoints.CHANNEL_BULK_DELETE(channelId), { + return await bot.rest.runMethod(bot.rest,"post", bot.constants.endpoints.CHANNEL_BULK_DELETE(channelId), { messages: ids.splice(0, 100), reason, }); diff --git a/src/helpers/messages/edit_message.ts b/src/helpers/messages/edit_message.ts index bd58ba51f..18818e72d 100644 --- a/src/helpers/messages/edit_message.ts +++ b/src/helpers/messages/edit_message.ts @@ -1,45 +1,42 @@ -import { botId } 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 { validateComponents } from "../../util/utils.ts"; +import {Bot} from "../../bot.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Edit the message. */ -export async function editMessage(channelId: bigint, messageId: bigint, content: string | EditMessage) { +export async function editMessage(bot: Bot, channelId: bigint, messageId: bigint, content: string | EditMessage) { const message = await cacheHandlers.get("messages", messageId); if (message) { - if (message.authorId !== botId) { + if (message.authorId !== bot.id) { throw new Error("You can only edit a message that was sent by the bot."); } const requiredPerms: PermissionStrings[] = ["SEND_MESSAGES"]; - await requireBotChannelPermissions(message.channelId, requiredPerms); + await bot.utils.requireBotChannelPermissions(message.channelId, requiredPerms); } if (typeof content === "string") content = { content }; if (content.components?.length) { - validateComponents(content.components); + validateComponents(bot, content.components); } content.embeds?.splice(10); if (content.content && content.content.length > 2000) { - throw new Error(Errors.MESSAGE_MAX_LENGTH); + throw new Error(bot.constants.Errors.MESSAGE_MAX_LENGTH); } - const result = await rest.runMethod( + const result = await bot.rest.runMethod>( + bot.rest, "patch", - endpoints.CHANNEL_MESSAGE(channelId, messageId), - snakelize(content) + bot.constants.endpoints.CHANNEL_MESSAGE(channelId, messageId), + bot.utils.snakelize(content) ); - return await structures.createDiscordenoMessage(result); + return bot.transformers.message(bot, result); } diff --git a/src/helpers/messages/get_message.ts b/src/helpers/messages/get_message.ts index b925abf98..beefd4e0f 100644 --- a/src/helpers/messages/get_message.ts +++ b/src/helpers/messages/get_message.ts @@ -1,17 +1,15 @@ import { cacheHandlers } from "../../cache.ts"; -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 { requireBotChannelPermissions } from "../../util/permissions.ts"; +import {Bot} from "../../bot.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Fetch a single message from the server. Requires VIEW_CHANNEL and READ_MESSAGE_HISTORY */ -export async function getMessage(channelId: bigint, id: bigint) { +export async function getMessage(bot: Bot, channelId: bigint, id: bigint) { if (await cacheHandlers.has("channels", channelId)) { - await requireBotChannelPermissions(channelId, ["VIEW_CHANNEL", "READ_MESSAGE_HISTORY"]); + await bot.utils.requireBotChannelPermissions(channelId, ["VIEW_CHANNEL", "READ_MESSAGE_HISTORY"]); } - const result = await rest.runMethod("get", endpoints.CHANNEL_MESSAGE(channelId, id)); + const result = await bot.rest.runMethod>(bot.rest,"get", bot.constants.endpoints.CHANNEL_MESSAGE(channelId, id)); - return await structures.createDiscordenoMessage(result); + return await bot.transformers.message(bot, result); } diff --git a/src/rest/mod.ts b/src/rest/mod.ts index 4667ec676..0b4f8ded0 100644 --- a/src/rest/mod.ts +++ b/src/rest/mod.ts @@ -1,9 +1,11 @@ export * from "./check_rate_limits.ts"; export * from "./cleanup_queues.ts"; export * from "./create_request_body.ts"; +export * from "./process_global_queue.ts"; export * from "./process_queue.ts"; export * from "./process_rate_limited_paths.ts"; export * from "./process_request.ts"; export * from "./process_request_headers.ts"; export * from "./rest.ts"; export * from "./run_method.ts"; +export * from "./simplify_url.ts"; diff --git a/src/ws/mod.ts b/src/ws/mod.ts index 3dc203936..7380bc32d 100644 --- a/src/ws/mod.ts +++ b/src/ws/mod.ts @@ -1,3 +1,4 @@ +export * from "./close_ws.ts"; export * from "./create_shard.ts"; export * from "./events.ts"; export * from "./handle_discord_payload.ts"; @@ -7,6 +8,7 @@ export * from "./identify.ts"; export * from "./resharder.ts"; export * from "./resume.ts"; export * from "./spawn_shards.ts"; +export * from "./send_shard_message.ts"; export * from "./start_gateway_options.ts"; export * from "./start_gateway.ts"; export * from "./tell_cluster_to_identify.ts"; From 7cfef637829a7d7ec432b0ee2a5209e36ba3d81d Mon Sep 17 00:00:00 2001 From: TriForMine Date: Fri, 15 Oct 2021 12:54:39 +0000 Subject: [PATCH 02/34] change: prettier code --- src/bot.ts | 56 ++++++++++++++++++------- src/helpers/messages/add_reaction.ts | 6 ++- src/helpers/messages/add_reactions.ts | 10 ++++- src/helpers/messages/delete_message.ts | 15 ++++++- src/helpers/messages/delete_messages.ts | 2 +- src/helpers/messages/edit_message.ts | 6 +-- src/helpers/messages/get_message.ts | 10 +++-- 7 files changed, 78 insertions(+), 27 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 7fa34a338..8a3127222 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -11,26 +11,52 @@ import { requireChannelPermissions, highestRole, higherRolePosition, - requireBotChannelPermissions + requireBotChannelPermissions, } from "./util/permissions.ts"; import { getGatewayBot } from "./helpers/misc/get_gateway_bot.ts"; -import { checkRateLimits, processQueue, cleanupQueues, createRequestBody, processRateLimitedPaths, processRequest, processRequestHeaders, runMethod, processGlobalQueue, simplifyUrl } from "./rest/mod.ts"; +import { + checkRateLimits, + processQueue, + cleanupQueues, + createRequestBody, + processRateLimitedPaths, + processRequest, + processRequestHeaders, + runMethod, + processGlobalQueue, + simplifyUrl, +} from "./rest/mod.ts"; import type { RestPayload, RestRateLimitedPath, RestRequest } from "./rest/rest.ts"; import { DiscordGatewayIntents } from "./types/gateway/gateway_intents.ts"; import { GetGatewayBot } from "./types/gateway/get_gateway_bot.ts"; -import {bigintToSnowflake, snowflakeToBigint} from "./util/bigint.ts"; +import { bigintToSnowflake, snowflakeToBigint } from "./util/bigint.ts"; import { Collection } from "./util/collection.ts"; -import {DiscordenoMember, DiscordenoUser, transformMember, transformUser, DiscordenoGuild, transformGuild, DiscordenoChannel, transformChannel, transformMessage, transformRole, DiscordenoVoiceState, transformVoiceState} from "./transformers/mod.ts"; import { - baseEndpoints, CHANNEL_MENTION_REGEX, - CONTEXT_MENU_COMMANDS_NAME_REGEX, DISCORD_SNOWFLAKE_REGEX, + DiscordenoMember, + DiscordenoUser, + transformMember, + transformUser, + DiscordenoGuild, + transformGuild, + DiscordenoChannel, + transformChannel, + transformMessage, + transformRole, + DiscordenoVoiceState, + transformVoiceState, +} from "./transformers/mod.ts"; +import { + baseEndpoints, + CHANNEL_MENTION_REGEX, + CONTEXT_MENU_COMMANDS_NAME_REGEX, + DISCORD_SNOWFLAKE_REGEX, DISCORDENO_VERSION, endpoints, SLASH_COMMANDS_NAME_REGEX, - USER_AGENT + USER_AGENT, } from "./util/constants.ts"; -import {Errors} from "./types/discordeno/errors.ts"; -import {GatewayPayload} from "./types/gateway/gateway_payload.ts"; +import { Errors } from "./types/discordeno/errors.ts"; +import { GatewayPayload } from "./types/gateway/gateway_payload.ts"; import { closeWS, handleOnMessage, @@ -45,12 +71,12 @@ import { handleDiscordPayload, tellClusterToIdentify, sendShardMessage, - DiscordenoShard + DiscordenoShard, } from "./ws/mod.ts"; -import {validateLength} from "./util/validate_length.ts"; -import {delay, validateSlashOptionChoices, validateSlashOptions} from "./util/utils.ts"; -import {iconBigintToHash, iconHashToBigInt} from "./util/hash.ts"; -import {calculateShardId} from "./util/calculate_shard_id.ts"; +import { validateLength } from "./util/validate_length.ts"; +import { delay, validateSlashOptionChoices, validateSlashOptions } from "./util/utils.ts"; +import { iconBigintToHash, iconHashToBigInt } from "./util/hash.ts"; +import { calculateShardId } from "./util/calculate_shard_id.ts"; export async function createBot(options: CreateBotOptions) { return { @@ -531,6 +557,6 @@ export function createBotConstants() { CHANNEL_MENTION_REGEX, DISCORD_SNOWFLAKE_REGEX, }, - Errors + Errors, }; } diff --git a/src/helpers/messages/add_reaction.ts b/src/helpers/messages/add_reaction.ts index 7ed7d6536..f5e106593 100644 --- a/src/helpers/messages/add_reaction.ts +++ b/src/helpers/messages/add_reaction.ts @@ -10,5 +10,9 @@ export async function addReaction(bot: Bot, channelId: bigint, messageId: bigint reaction = reaction.substring(3, reaction.length - 1); } - return await bot.rest.runMethod(bot.rest, "put", bot.constants.endpoints.CHANNEL_MESSAGE_REACTION_ME(channelId, messageId, reaction)); + return await bot.rest.runMethod( + bot.rest, + "put", + bot.constants.endpoints.CHANNEL_MESSAGE_REACTION_ME(channelId, messageId, reaction) + ); } diff --git a/src/helpers/messages/add_reactions.ts b/src/helpers/messages/add_reactions.ts index a2cb0e4e4..06ed6f3f4 100644 --- a/src/helpers/messages/add_reactions.ts +++ b/src/helpers/messages/add_reactions.ts @@ -1,8 +1,14 @@ -import {Bot} from "../../bot.ts"; +import { Bot } from "../../bot.ts"; import { addReaction } from "./add_reaction.ts"; /** Adds multiple reactions to a message. If `ordered` is true(default is false), it will add the reactions one at a time in the order provided. Note: Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. Requires READ_MESSAGE_HISTORY and ADD_REACTIONS */ -export async function addReactions(bot: Bot, channelId: bigint, messageId: bigint, reactions: string[], ordered = false) { +export async function addReactions( + bot: Bot, + channelId: bigint, + messageId: bigint, + reactions: string[], + ordered = false +) { if (!ordered) { await Promise.all(reactions.map((reaction) => addReaction(bot, channelId, messageId, reaction))); } else { diff --git a/src/helpers/messages/delete_message.ts b/src/helpers/messages/delete_message.ts index 2d704dbd5..8249c43ed 100644 --- a/src/helpers/messages/delete_message.ts +++ b/src/helpers/messages/delete_message.ts @@ -3,7 +3,13 @@ import { cacheHandlers } from "../../cache.ts"; import { delay } from "../../util/utils.ts"; /** Delete a message with the channel id and message id only. */ -export async function deleteMessage(bot: Bot, channelId: bigint, messageId: bigint, reason?: string, delayMilliseconds = 0) { +export async function deleteMessage( + bot: Bot, + channelId: bigint, + messageId: bigint, + reason?: string, + delayMilliseconds = 0 +) { const message = await cacheHandlers.get("messages", messageId); if (message && message.authorId !== bot.id) { @@ -12,5 +18,10 @@ export async function deleteMessage(bot: Bot, channelId: bigint, messageId: bigi if (delayMilliseconds) await delay(delayMilliseconds); - return await bot.rest.runMethod(bot.rest,"delete", bot.constants.endpoints.CHANNEL_MESSAGE(channelId, messageId), { reason }); + return await bot.rest.runMethod( + bot.rest, + "delete", + bot.constants.endpoints.CHANNEL_MESSAGE(channelId, messageId), + { reason } + ); } diff --git a/src/helpers/messages/delete_messages.ts b/src/helpers/messages/delete_messages.ts index 049b7f2d9..6b342638b 100644 --- a/src/helpers/messages/delete_messages.ts +++ b/src/helpers/messages/delete_messages.ts @@ -12,7 +12,7 @@ export async function deleteMessages(bot: Bot, channelId: bigint, ids: bigint[], console.warn(`This endpoint only accepts a maximum of 100 messages. Deleting the first 100 message ids provided.`); } - return await bot.rest.runMethod(bot.rest,"post", bot.constants.endpoints.CHANNEL_BULK_DELETE(channelId), { + return await bot.rest.runMethod(bot.rest, "post", bot.constants.endpoints.CHANNEL_BULK_DELETE(channelId), { messages: ids.splice(0, 100), reason, }); diff --git a/src/helpers/messages/edit_message.ts b/src/helpers/messages/edit_message.ts index 18818e72d..cea01dff1 100644 --- a/src/helpers/messages/edit_message.ts +++ b/src/helpers/messages/edit_message.ts @@ -3,8 +3,8 @@ 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 { validateComponents } from "../../util/utils.ts"; -import {Bot} from "../../bot.ts"; -import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; +import { Bot } from "../../bot.ts"; +import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; /** Edit the message. */ export async function editMessage(bot: Bot, channelId: bigint, messageId: bigint, content: string | EditMessage) { @@ -32,7 +32,7 @@ export async function editMessage(bot: Bot, channelId: bigint, messageId: bigint } const result = await bot.rest.runMethod>( - bot.rest, + bot.rest, "patch", bot.constants.endpoints.CHANNEL_MESSAGE(channelId, messageId), bot.utils.snakelize(content) diff --git a/src/helpers/messages/get_message.ts b/src/helpers/messages/get_message.ts index beefd4e0f..b7c41859a 100644 --- a/src/helpers/messages/get_message.ts +++ b/src/helpers/messages/get_message.ts @@ -1,7 +1,7 @@ import { cacheHandlers } from "../../cache.ts"; import type { Message } from "../../types/messages/message.ts"; -import {Bot} from "../../bot.ts"; -import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; +import { Bot } from "../../bot.ts"; +import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; /** Fetch a single message from the server. Requires VIEW_CHANNEL and READ_MESSAGE_HISTORY */ export async function getMessage(bot: Bot, channelId: bigint, id: bigint) { @@ -9,7 +9,11 @@ export async function getMessage(bot: Bot, channelId: bigint, id: bigint) { await bot.utils.requireBotChannelPermissions(channelId, ["VIEW_CHANNEL", "READ_MESSAGE_HISTORY"]); } - const result = await bot.rest.runMethod>(bot.rest,"get", bot.constants.endpoints.CHANNEL_MESSAGE(channelId, id)); + const result = await bot.rest.runMethod>( + bot.rest, + "get", + bot.constants.endpoints.CHANNEL_MESSAGE(channelId, id) + ); return await bot.transformers.message(bot, result); } From 8acc8b9a6b6cd1b44e2d85731fef4c1752f4b82d Mon Sep 17 00:00:00 2001 From: TriForMine Date: Fri, 15 Oct 2021 15:00:35 +0200 Subject: [PATCH 03/34] Fix params --- src/helpers/messages/add_reaction.ts | 2 +- src/helpers/messages/delete_message.ts | 2 +- src/helpers/messages/delete_messages.ts | 2 +- src/helpers/messages/edit_message.ts | 2 +- src/helpers/messages/get_message.ts | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/helpers/messages/add_reaction.ts b/src/helpers/messages/add_reaction.ts index 7ed7d6536..8dd203c74 100644 --- a/src/helpers/messages/add_reaction.ts +++ b/src/helpers/messages/add_reaction.ts @@ -2,7 +2,7 @@ import { Bot } from "../../bot.ts"; /** Create a reaction for the message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. Requires READ_MESSAGE_HISTORY and ADD_REACTIONS */ export async function addReaction(bot: Bot, channelId: bigint, messageId: bigint, reaction: string) { - await bot.utils.requireBotChannelPermissions(channelId, ["ADD_REACTIONS", "READ_MESSAGE_HISTORY"]); + await bot.utils.requireBotChannelPermissions(bot, channelId, ["ADD_REACTIONS", "READ_MESSAGE_HISTORY"]); if (reaction.startsWith("<:")) { reaction = reaction.substring(2, reaction.length - 1); diff --git a/src/helpers/messages/delete_message.ts b/src/helpers/messages/delete_message.ts index 2d704dbd5..c8c92f7d0 100644 --- a/src/helpers/messages/delete_message.ts +++ b/src/helpers/messages/delete_message.ts @@ -7,7 +7,7 @@ export async function deleteMessage(bot: Bot, channelId: bigint, messageId: bigi const message = await cacheHandlers.get("messages", messageId); if (message && message.authorId !== bot.id) { - await bot.utils.requireBotChannelPermissions(message.channelId, ["MANAGE_MESSAGES"]); + await bot.utils.requireBotChannelPermissions(bot, message.channelId, ["MANAGE_MESSAGES"]); } if (delayMilliseconds) await delay(delayMilliseconds); diff --git a/src/helpers/messages/delete_messages.ts b/src/helpers/messages/delete_messages.ts index 049b7f2d9..dfed14f47 100644 --- a/src/helpers/messages/delete_messages.ts +++ b/src/helpers/messages/delete_messages.ts @@ -2,7 +2,7 @@ import { Bot } from "../../bot.ts"; /** Delete messages from the channel. 2-100. Requires the MANAGE_MESSAGES permission */ export async function deleteMessages(bot: Bot, channelId: bigint, ids: bigint[], reason?: string) { - await bot.utils.requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]); + await bot.utils.requireBotChannelPermissions(bot, channelId, ["MANAGE_MESSAGES"]); if (ids.length < 2) { throw new Error(bot.constants.Errors.DELETE_MESSAGES_MIN); diff --git a/src/helpers/messages/edit_message.ts b/src/helpers/messages/edit_message.ts index 18818e72d..88b8e4b34 100644 --- a/src/helpers/messages/edit_message.ts +++ b/src/helpers/messages/edit_message.ts @@ -16,7 +16,7 @@ export async function editMessage(bot: Bot, channelId: bigint, messageId: bigint } const requiredPerms: PermissionStrings[] = ["SEND_MESSAGES"]; - await bot.utils.requireBotChannelPermissions(message.channelId, requiredPerms); + await bot.utils.requireBotChannelPermissions(bot, message.channelId, requiredPerms); } if (typeof content === "string") content = { content }; diff --git a/src/helpers/messages/get_message.ts b/src/helpers/messages/get_message.ts index beefd4e0f..aebc74bd4 100644 --- a/src/helpers/messages/get_message.ts +++ b/src/helpers/messages/get_message.ts @@ -6,7 +6,7 @@ import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Fetch a single message from the server. Requires VIEW_CHANNEL and READ_MESSAGE_HISTORY */ export async function getMessage(bot: Bot, channelId: bigint, id: bigint) { if (await cacheHandlers.has("channels", channelId)) { - await bot.utils.requireBotChannelPermissions(channelId, ["VIEW_CHANNEL", "READ_MESSAGE_HISTORY"]); + await bot.utils.requireBotChannelPermissions(bot, channelId, ["VIEW_CHANNEL", "READ_MESSAGE_HISTORY"]); } const result = await bot.rest.runMethod>(bot.rest,"get", bot.constants.endpoints.CHANNEL_MESSAGE(channelId, id)); From 597f0615812d209f424e5fc325dff5d641e7f707 Mon Sep 17 00:00:00 2001 From: TriForMine Date: Fri, 15 Oct 2021 15:04:23 +0200 Subject: [PATCH 04/34] Progress --- src/bot.ts | 4 +++- src/helpers/messages/add_reaction.ts | 2 +- src/helpers/messages/add_reactions.ts | 2 +- src/helpers/messages/delete_message.ts | 6 ++---- src/helpers/messages/delete_messages.ts | 2 +- src/helpers/messages/edit_message.ts | 9 ++++----- src/helpers/messages/get_message.ts | 4 ++-- src/helpers/messages/get_messages.ts | 18 ++++++++---------- 8 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 8a3127222..2a4f1adca 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -74,7 +74,7 @@ import { DiscordenoShard, } from "./ws/mod.ts"; import { validateLength } from "./util/validate_length.ts"; -import { delay, validateSlashOptionChoices, validateSlashOptions } from "./util/utils.ts"; +import {delay, validateComponents, validateSlashOptionChoices, validateSlashOptions} from "./util/utils.ts"; import { iconBigintToHash, iconHashToBigInt } from "./util/hash.ts"; import { calculateShardId } from "./util/calculate_shard_id.ts"; @@ -284,6 +284,7 @@ export function createUtils(options: Partial) { validateSlashOptions, validateSlashOptionChoices, requireBotChannelPermissions, + validateComponents }; } @@ -310,6 +311,7 @@ export interface HelperUtils { validateSlashOptions: typeof validateSlashOptions; validateSlashOptionChoices: typeof validateSlashOptionChoices; requireBotChannelPermissions: typeof requireBotChannelPermissions; + validateComponents: typeof validateComponents; } export function createGatewayManager(options: Partial): GatewayManager { diff --git a/src/helpers/messages/add_reaction.ts b/src/helpers/messages/add_reaction.ts index 0c0400f47..c832b53c3 100644 --- a/src/helpers/messages/add_reaction.ts +++ b/src/helpers/messages/add_reaction.ts @@ -1,4 +1,4 @@ -import { Bot } from "../../bot.ts"; +import type { Bot } from "../../bot.ts"; /** Create a reaction for the message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. Requires READ_MESSAGE_HISTORY and ADD_REACTIONS */ export async function addReaction(bot: Bot, channelId: bigint, messageId: bigint, reaction: string) { diff --git a/src/helpers/messages/add_reactions.ts b/src/helpers/messages/add_reactions.ts index 06ed6f3f4..9a013414e 100644 --- a/src/helpers/messages/add_reactions.ts +++ b/src/helpers/messages/add_reactions.ts @@ -1,4 +1,4 @@ -import { Bot } from "../../bot.ts"; +import type { Bot } from "../../bot.ts"; import { addReaction } from "./add_reaction.ts"; /** Adds multiple reactions to a message. If `ordered` is true(default is false), it will add the reactions one at a time in the order provided. Note: Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. Requires READ_MESSAGE_HISTORY and ADD_REACTIONS */ diff --git a/src/helpers/messages/delete_message.ts b/src/helpers/messages/delete_message.ts index 71ecf3ce5..ad12e2992 100644 --- a/src/helpers/messages/delete_message.ts +++ b/src/helpers/messages/delete_message.ts @@ -1,6 +1,4 @@ -import { Bot } from "../../bot.ts"; -import { cacheHandlers } from "../../cache.ts"; -import { delay } from "../../util/utils.ts"; +import type { Bot } from "../../bot.ts"; /** Delete a message with the channel id and message id only. */ export async function deleteMessage( @@ -16,7 +14,7 @@ export async function deleteMessage( await bot.utils.requireBotChannelPermissions(bot, message.channelId, ["MANAGE_MESSAGES"]); } - if (delayMilliseconds) await delay(delayMilliseconds); + if (delayMilliseconds) await bot.utils.delay(delayMilliseconds); return await bot.rest.runMethod( bot.rest, diff --git a/src/helpers/messages/delete_messages.ts b/src/helpers/messages/delete_messages.ts index 8421957cf..57159b481 100644 --- a/src/helpers/messages/delete_messages.ts +++ b/src/helpers/messages/delete_messages.ts @@ -1,4 +1,4 @@ -import { Bot } from "../../bot.ts"; +import type { Bot } from "../../bot.ts"; /** Delete messages from the channel. 2-100. Requires the MANAGE_MESSAGES permission */ export async function deleteMessages(bot: Bot, channelId: bigint, ids: bigint[], reason?: string) { diff --git a/src/helpers/messages/edit_message.ts b/src/helpers/messages/edit_message.ts index 9286c41bf..c8e227cf3 100644 --- a/src/helpers/messages/edit_message.ts +++ b/src/helpers/messages/edit_message.ts @@ -1,10 +1,9 @@ import { cacheHandlers } from "../../cache.ts"; -import { EditMessage } from "../../types/messages/edit_message.ts"; +import type { 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 { validateComponents } from "../../util/utils.ts"; -import { Bot } from "../../bot.ts"; -import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; +import type { Bot } from "../../bot.ts"; +import type { SnakeCasedPropertiesDeep } from "../../types/util.ts"; /** Edit the message. */ export async function editMessage(bot: Bot, channelId: bigint, messageId: bigint, content: string | EditMessage) { @@ -22,7 +21,7 @@ export async function editMessage(bot: Bot, channelId: bigint, messageId: bigint if (typeof content === "string") content = { content }; if (content.components?.length) { - validateComponents(bot, content.components); + bot.utils.validateComponents(bot, content.components); } content.embeds?.splice(10); diff --git a/src/helpers/messages/get_message.ts b/src/helpers/messages/get_message.ts index 417d568d3..b48833799 100644 --- a/src/helpers/messages/get_message.ts +++ b/src/helpers/messages/get_message.ts @@ -1,7 +1,7 @@ import { cacheHandlers } from "../../cache.ts"; import type { Message } from "../../types/messages/message.ts"; -import { Bot } from "../../bot.ts"; -import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; +import type { Bot } from "../../bot.ts"; +import type { SnakeCasedPropertiesDeep } from "../../types/util.ts"; /** Fetch a single message from the server. Requires VIEW_CHANNEL and READ_MESSAGE_HISTORY */ export async function getMessage(bot: Bot, channelId: bigint, id: bigint) { diff --git a/src/helpers/messages/get_messages.ts b/src/helpers/messages/get_messages.ts index 38e9c2ea8..b052dafce 100644 --- a/src/helpers/messages/get_messages.ts +++ b/src/helpers/messages/get_messages.ts @@ -1,28 +1,26 @@ -import { rest } from "../../rest/rest.ts"; -import { structures } from "../../structures/mod.ts"; -import { Errors } from "../../types/discordeno/errors.ts"; -import { +import type { GetMessagesAfter, GetMessagesAround, GetMessagesBefore, GetMessagesLimit, } from "../../types/messages/get_messages.ts"; import type { Message } from "../../types/messages/message.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotChannelPermissions } from "../../util/permissions.ts"; +import type { Bot } from "../../bot.ts"; +import type {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Fetches between 2-100 messages. Requires VIEW_CHANNEL and READ_MESSAGE_HISTORY */ export async function getMessages( + bot: Bot, channelId: bigint, options?: GetMessagesAfter | GetMessagesBefore | GetMessagesAround | GetMessagesLimit ) { - await requireBotChannelPermissions(channelId, ["VIEW_CHANNEL", "READ_MESSAGE_HISTORY"]); + await bot.utils.requireBotChannelPermissions(bot, channelId, ["VIEW_CHANNEL", "READ_MESSAGE_HISTORY"]); if (options?.limit && (options.limit < 0 || options.limit > 100)) { - throw new Error(Errors.INVALID_GET_MESSAGES_LIMIT); + throw new Error(bot.constants.Errors.INVALID_GET_MESSAGES_LIMIT); } - const result = await rest.runMethod("get", endpoints.CHANNEL_MESSAGES(channelId), options); + const result = await bot.rest.runMethod[]>(bot.rest, "get", bot.constants.endpoints.CHANNEL_MESSAGES(channelId), options); - return await Promise.all(result.map((res) => structures.createDiscordenoMessage(res))); + return await Promise.all(result.map((res) => bot.transformers.message(bot, res))); } From 0d60ae8b4c423a845a30cbd73505740d693a7520 Mon Sep 17 00:00:00 2001 From: TriForMine Date: Fri, 15 Oct 2021 13:05:11 +0000 Subject: [PATCH 05/34] change: prettier code --- src/bot.ts | 4 ++-- src/helpers/messages/get_messages.ts | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index 2a4f1adca..e539566ef 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -74,7 +74,7 @@ import { DiscordenoShard, } from "./ws/mod.ts"; import { validateLength } from "./util/validate_length.ts"; -import {delay, validateComponents, validateSlashOptionChoices, validateSlashOptions} from "./util/utils.ts"; +import { delay, validateComponents, validateSlashOptionChoices, validateSlashOptions } from "./util/utils.ts"; import { iconBigintToHash, iconHashToBigInt } from "./util/hash.ts"; import { calculateShardId } from "./util/calculate_shard_id.ts"; @@ -284,7 +284,7 @@ export function createUtils(options: Partial) { validateSlashOptions, validateSlashOptionChoices, requireBotChannelPermissions, - validateComponents + validateComponents, }; } diff --git a/src/helpers/messages/get_messages.ts b/src/helpers/messages/get_messages.ts index b052dafce..d5ece4f63 100644 --- a/src/helpers/messages/get_messages.ts +++ b/src/helpers/messages/get_messages.ts @@ -6,7 +6,7 @@ import type { } from "../../types/messages/get_messages.ts"; import type { Message } from "../../types/messages/message.ts"; import type { Bot } from "../../bot.ts"; -import type {SnakeCasedPropertiesDeep} from "../../types/util.ts"; +import type { SnakeCasedPropertiesDeep } from "../../types/util.ts"; /** Fetches between 2-100 messages. Requires VIEW_CHANNEL and READ_MESSAGE_HISTORY */ export async function getMessages( @@ -20,7 +20,12 @@ export async function getMessages( throw new Error(bot.constants.Errors.INVALID_GET_MESSAGES_LIMIT); } - const result = await bot.rest.runMethod[]>(bot.rest, "get", bot.constants.endpoints.CHANNEL_MESSAGES(channelId), options); + const result = await bot.rest.runMethod[]>( + bot.rest, + "get", + bot.constants.endpoints.CHANNEL_MESSAGES(channelId), + options + ); return await Promise.all(result.map((res) => bot.transformers.message(bot, res))); } From a97b01846d32978fc44624a0a7cfff5f1dedc715 Mon Sep 17 00:00:00 2001 From: TriForMine Date: Fri, 15 Oct 2021 15:17:19 +0200 Subject: [PATCH 06/34] Finish message helpers --- src/helpers/messages/delete_message.ts | 1 + src/helpers/messages/get_reactions.ts | 10 +++---- src/helpers/messages/pin_message.ts | 10 +++---- src/helpers/messages/publish_message.ts | 11 ++++---- src/helpers/messages/remove_all_reactions.ts | 10 +++---- src/helpers/messages/remove_reaction.ts | 14 +++++----- src/helpers/messages/remove_reaction_emoji.ts | 13 +++++---- src/helpers/messages/send_message.ts | 27 +++++++++---------- src/helpers/messages/suppress_embeds.ts | 24 +++++++---------- src/helpers/messages/unpin_message.ts | 12 ++++----- 10 files changed, 58 insertions(+), 74 deletions(-) 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 From b68a5eca6f6385d9e04a8dcf8095bac4eacf4c11 Mon Sep 17 00:00:00 2001 From: TriForMine Date: Mon, 18 Oct 2021 20:34:58 +0200 Subject: [PATCH 07/34] Some changes --- src/helpers/messages/add_reactions.ts | 7 +++---- src/helpers/messages/suppress_embeds.ts | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/helpers/messages/add_reactions.ts b/src/helpers/messages/add_reactions.ts index 9a013414e..4fe1fbff0 100644 --- a/src/helpers/messages/add_reactions.ts +++ b/src/helpers/messages/add_reactions.ts @@ -1,5 +1,4 @@ import type { Bot } from "../../bot.ts"; -import { addReaction } from "./add_reaction.ts"; /** Adds multiple reactions to a message. If `ordered` is true(default is false), it will add the reactions one at a time in the order provided. Note: Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. Requires READ_MESSAGE_HISTORY and ADD_REACTIONS */ export async function addReactions( @@ -10,11 +9,11 @@ export async function addReactions( ordered = false ) { if (!ordered) { - await Promise.all(reactions.map((reaction) => addReaction(bot, channelId, messageId, reaction))); + await Promise.all(reactions.map((reaction) => bot.helpers.addReaction(bot, channelId, messageId, reaction))); } else { for (const reaction of reactions) { - bot.events.debug?.("loop", "Running for of loop in addReactions function."); - await addReaction(bot, channelId, messageId, reaction); + bot.events.debug("loop", "Running for of loop in addReactions function."); + await bot.helpers.addReaction(bot, channelId, messageId, reaction); } } } diff --git a/src/helpers/messages/suppress_embeds.ts b/src/helpers/messages/suppress_embeds.ts index 59bc2400d..d9f3e9713 100644 --- a/src/helpers/messages/suppress_embeds.ts +++ b/src/helpers/messages/suppress_embeds.ts @@ -13,7 +13,7 @@ export async function suppressEmbeds(bot: Bot, channelId: bigint, messageId: big bot.rest, "patch", bot.constants.endpoints.CHANNEL_MESSAGE(channelId, messageId), - bot.utils.snakelize({ flags: 4 }) + { flags: 4 } ); return await bot.transformers.message(bot, result); From d1800006a482f4da8611fdf36e30a355b766bcf2 Mon Sep 17 00:00:00 2001 From: TriForMine Date: Mon, 18 Oct 2021 18:35:40 +0000 Subject: [PATCH 08/34] change: prettier code --- src/helpers/messages/suppress_embeds.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/helpers/messages/suppress_embeds.ts b/src/helpers/messages/suppress_embeds.ts index d9f3e9713..e28ba0eb6 100644 --- a/src/helpers/messages/suppress_embeds.ts +++ b/src/helpers/messages/suppress_embeds.ts @@ -10,7 +10,7 @@ export async function suppressEmbeds(bot: Bot, channelId: bigint, messageId: big await bot.utils.requireBotChannelPermissions(bot, channelId, message ? ["MANAGE_MESSAGES"] : ["SEND_MESSAGES"]); const result = await bot.rest.runMethod>( - bot.rest, + bot.rest, "patch", bot.constants.endpoints.CHANNEL_MESSAGE(channelId, messageId), { flags: 4 } From 64e316647eac0ba384117729abef504498c988f2 Mon Sep 17 00:00:00 2001 From: TriForMine Date: Mon, 18 Oct 2021 21:03:24 +0200 Subject: [PATCH 09/34] More work --- src/bot.ts | 3 +++ .../integrations/delete_integration.ts | 10 +++----- src/helpers/invites/create_invite.ts | 20 +++++++++------ src/helpers/invites/delete_invite.ts | 16 ++++++------ src/helpers/invites/get_channel_invites.ts | 11 ++++---- src/helpers/invites/get_invite.ts | 12 +++++---- src/helpers/invites/get_invites.ts | 11 ++++---- src/helpers/misc/edit_bot_profile.ts | 11 ++++---- src/helpers/misc/edit_bot_status.ts | 15 ++++++----- src/helpers/misc/get_gateway_bot.ts | 8 +++--- src/helpers/misc/get_user.ts | 8 +++--- src/helpers/oauth/get_application.ts | 8 +++--- src/helpers/voice/connect_to_voice_channel.ts | 25 ++++++++----------- 13 files changed, 79 insertions(+), 79 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index e539566ef..d24a6ebd4 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -12,6 +12,7 @@ import { highestRole, higherRolePosition, requireBotChannelPermissions, + requireBotGuildPermissions } from "./util/permissions.ts"; import { getGatewayBot } from "./helpers/misc/get_gateway_bot.ts"; import { @@ -284,6 +285,7 @@ export function createUtils(options: Partial) { validateSlashOptions, validateSlashOptionChoices, requireBotChannelPermissions, + requireBotGuildPermissions, validateComponents, }; } @@ -311,6 +313,7 @@ export interface HelperUtils { validateSlashOptions: typeof validateSlashOptions; validateSlashOptionChoices: typeof validateSlashOptionChoices; requireBotChannelPermissions: typeof requireBotChannelPermissions; + requireBotGuildPermissions: typeof requireBotGuildPermissions; validateComponents: typeof validateComponents; } diff --git a/src/helpers/integrations/delete_integration.ts b/src/helpers/integrations/delete_integration.ts index 8de324a4b..68893fd1c 100644 --- a/src/helpers/integrations/delete_integration.ts +++ b/src/helpers/integrations/delete_integration.ts @@ -1,10 +1,8 @@ -import { rest } from "../../rest/rest.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotGuildPermissions } from "../../util/permissions.ts"; +import {Bot} from "../../bot.ts"; /** Delete the attached integration object for the guild with this id. Requires MANAGE_GUILD permission. */ -export async function deleteIntegration(guildId: bigint, id: bigint) { - await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); +export async function deleteIntegration(bot: Bot, guildId: bigint, id: bigint) { + await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_GUILD"]); - return await rest.runMethod("delete", endpoints.GUILD_INTEGRATION(guildId, id)); + return await bot.rest.runMethod(bot.rest,"delete", bot.constants.endpoints.GUILD_INTEGRATION(guildId, id)); } diff --git a/src/helpers/invites/create_invite.ts b/src/helpers/invites/create_invite.ts index 9060a0822..fe3189b71 100644 --- a/src/helpers/invites/create_invite.ts +++ b/src/helpers/invites/create_invite.ts @@ -1,14 +1,12 @@ -import { rest } from "../../rest/rest.ts"; import type { CreateChannelInvite } from "../../types/invites/create_channel_invite.ts"; import type { InviteMetadata } from "../../types/invites/invite_metadata.ts"; import { Errors } from "../../types/discordeno/errors.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotChannelPermissions } from "../../util/permissions.ts"; -import { snakelize } from "../../util/utils.ts"; +import { Bot } from "../../bot.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Creates a new invite for this channel. Requires CREATE_INSTANT_INVITE */ -export async function createInvite(channelId: bigint, options: CreateChannelInvite = {}) { - await requireBotChannelPermissions(channelId, ["CREATE_INSTANT_INVITE"]); +export async function createInvite(bot: Bot, channelId: bigint, options: CreateChannelInvite = {}) { + await bot.utils.requireBotChannelPermissions(channelId, ["CREATE_INSTANT_INVITE"]); if (options.maxAge && (options.maxAge < 0 || options.maxAge > 604800)) { throw new Error(Errors.INVITE_MAX_AGE_INVALID); @@ -17,5 +15,13 @@ export async function createInvite(channelId: bigint, options: CreateChannelInvi throw new Error(Errors.INVITE_MAX_USES_INVALID); } - return await rest.runMethod("post", endpoints.CHANNEL_INVITES(channelId), snakelize(options)); + return await bot.rest.runMethod>(bot.rest,"post", bot.constants.endpoints.CHANNEL_INVITES(channelId), { + max_age: options.maxAge, + max_uses: options.maxUses, + temporary: options.temporary, + unique: options.unique, + target_type: options.targetType, + target_user_id: options.targetUserId, + target_application_id: options.targetUserId, + }); } diff --git a/src/helpers/invites/delete_invite.ts b/src/helpers/invites/delete_invite.ts index fe033ddf6..aecd9f62a 100644 --- a/src/helpers/invites/delete_invite.ts +++ b/src/helpers/invites/delete_invite.ts @@ -1,19 +1,17 @@ -import { cacheHandlers } from "../../cache.ts"; -import { rest } from "../../rest/rest.ts"; import type { InviteMetadata } from "../../types/invites/invite_metadata.ts"; -import { endpoints } from "../../util/constants.ts"; -import { botHasChannelPermissions, requireBotGuildPermissions } from "../../util/permissions.ts"; +import {Bot} from "../../bot.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Deletes an invite for the given code. Requires `MANAGE_CHANNELS` or `MANAGE_GUILD` permission */ -export async function deleteInvite(channelId: bigint, inviteCode: string) { - const channel = await cacheHandlers.get("channels", channelId); +export async function deleteInvite(bot: Bot, channelId: bigint, inviteCode: string) { + const channel = await bot.cache.channels.get(channelId); if (channel) { - const hasPerm = await botHasChannelPermissions(channel, ["MANAGE_CHANNELS"]); + const hasPerm = await bot.utils.botHasChannelPermissions(channel, ["MANAGE_CHANNELS"]); if (!hasPerm) { - await requireBotGuildPermissions(channel.guildId, ["MANAGE_GUILD"]); + await bot.utils.requireBotGuildPermissions(channel.guildId, ["MANAGE_GUILD"]); } } - return await rest.runMethod("delete", endpoints.INVITE(inviteCode)); + return await bot.rest.runMethod>(bot.rest,"delete", bot.constants.endpoints.INVITE(inviteCode)); } diff --git a/src/helpers/invites/get_channel_invites.ts b/src/helpers/invites/get_channel_invites.ts index 3877b6c7f..8fdfadf0d 100644 --- a/src/helpers/invites/get_channel_invites.ts +++ b/src/helpers/invites/get_channel_invites.ts @@ -1,14 +1,13 @@ -import { rest } from "../../rest/rest.ts"; import type { InviteMetadata } from "../../types/invites/invite_metadata.ts"; import { Collection } from "../../util/collection.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotChannelPermissions } from "../../util/permissions.ts"; +import {Bot} from "../../bot.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Gets the invites for this channel. Requires MANAGE_CHANNEL */ -export async function getChannelInvites(channelId: bigint) { - await requireBotChannelPermissions(channelId, ["MANAGE_CHANNELS"]); +export async function getChannelInvites(bot: Bot, channelId: bigint) { + await bot.utils.requireBotChannelPermissions(channelId, ["MANAGE_CHANNELS"]); - const result = await rest.runMethod("get", endpoints.CHANNEL_INVITES(channelId)); + const result = await bot.rest.runMethod[]>(bot.rest,"get", bot.constants.endpoints.CHANNEL_INVITES(channelId)); return new Collection(result.map((invite) => [invite.code, invite])); } diff --git a/src/helpers/invites/get_invite.ts b/src/helpers/invites/get_invite.ts index dbe9c7062..0a26a311e 100644 --- a/src/helpers/invites/get_invite.ts +++ b/src/helpers/invites/get_invite.ts @@ -1,10 +1,12 @@ -import { rest } from "../../rest/rest.ts"; import { GetInvite } from "../../types/invites/get_invite.ts"; import type { InviteMetadata } from "../../types/invites/invite_metadata.ts"; -import { endpoints } from "../../util/constants.ts"; -import { snakelize } from "../../util/utils.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; +import {Bot} from "../../bot.ts"; /** Returns an invite for the given code or throws an error if the invite doesn't exists. */ -export async function getInvite(inviteCode: string, options?: GetInvite) { - return await rest.runMethod("get", endpoints.INVITE(inviteCode), snakelize(options ?? {})); +export async function getInvite(bot: Bot, inviteCode: string, options?: GetInvite) { + return await bot.rest.runMethod>("get", bot.constants.endpoints.INVITE(inviteCode), { + with_counts: options.withCounts, + with_expiration: options.withExpiration, + }); } diff --git a/src/helpers/invites/get_invites.ts b/src/helpers/invites/get_invites.ts index 59098bfbf..43e5c6ae4 100644 --- a/src/helpers/invites/get_invites.ts +++ b/src/helpers/invites/get_invites.ts @@ -1,14 +1,13 @@ -import { rest } from "../../rest/rest.ts"; import type { InviteMetadata } from "../../types/invites/invite_metadata.ts"; import { Collection } from "../../util/collection.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotGuildPermissions } from "../../util/permissions.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; +import {Bot} from "../../bot.ts"; /** Get all the invites for this guild. Requires MANAGE_GUILD permission */ -export async function getInvites(guildId: bigint) { - await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); +export async function getInvites(bot: Bot, guildId: bigint) { + await bot.utils.requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); - const result = await rest.runMethod("get", endpoints.GUILD_INVITES(guildId)); + const result = await bot.rest.runMethod[]>("get", bot.constants.endpoints.GUILD_INVITES(guildId)); return new Collection(result.map((invite) => [invite.code, invite])); } diff --git a/src/helpers/misc/edit_bot_profile.ts b/src/helpers/misc/edit_bot_profile.ts index 886de902f..769e8dc90 100644 --- a/src/helpers/misc/edit_bot_profile.ts +++ b/src/helpers/misc/edit_bot_profile.ts @@ -1,13 +1,12 @@ -import { rest } from "../../rest/rest.ts"; import { Errors } from "../../types/discordeno/errors.ts"; import type { User } from "../../types/users/user.ts"; -import { endpoints } from "../../util/constants.ts"; -import { urlToBase64 } from "../../util/utils.ts"; +import {Bot} from "../../bot.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Modifies the bot's username or avatar. * NOTE: username: if changed may cause the bot's discriminator to be randomized. */ -export async function editBotProfile(options: { username?: string; botAvatarURL?: string | null }) { +export async function editBotProfile(bot: Bot, options: { username?: string; botAvatarURL?: string | null }) { // Nothing was edited if (!options.username && options.botAvatarURL === undefined) return; // Check username requirements if username was provided @@ -26,9 +25,9 @@ export async function editBotProfile(options: { username?: string; botAvatarURL? } } - const avatar = options?.botAvatarURL ? await urlToBase64(options?.botAvatarURL) : options?.botAvatarURL; + const avatar = options?.botAvatarURL ? await bot.utils.urlToBase64(options?.botAvatarURL) : options?.botAvatarURL; - return await rest.runMethod("patch", endpoints.USER_BOT, { + return await bot.rest.runMethod>(bot,"patch", bot.constants.endpoints.USER_BOT, { username: options.username?.trim(), avatar, }); diff --git a/src/helpers/misc/edit_bot_status.ts b/src/helpers/misc/edit_bot_status.ts index 0b2664085..aadbeab10 100644 --- a/src/helpers/misc/edit_bot_status.ts +++ b/src/helpers/misc/edit_bot_status.ts @@ -1,19 +1,18 @@ -import { eventHandlers } from "../../bot.ts"; +import {Bot} from "../../bot.ts"; import { DiscordGatewayOpcodes } from "../../types/codes/gateway_opcodes.ts"; import type { StatusUpdate } from "../../types/gateway/status_update.ts"; -import { snakelize } from "../../util/utils.ts"; -import { ws } from "../../ws/ws.ts"; -export function editBotStatus(data: Omit) { - ws.shards.forEach((shard) => { - eventHandlers.debug?.("loop", `Running forEach loop in editBotStatus function.`); +export function editBotStatus(bot: Bot, data: Omit) { + bot.ws.shards.forEach((shard) => { + bot.events.debug("loop", `Running forEach loop in editBotStatus function.`); - ws.sendShardMessage(shard, { + bot.ws.sendShardMessage(shard, { op: DiscordGatewayOpcodes.StatusUpdate, d: { since: null, afk: false, - ...snakelize>(data), + activities: data.activities, + status: data.status }, }); }); diff --git a/src/helpers/misc/get_gateway_bot.ts b/src/helpers/misc/get_gateway_bot.ts index ac7594177..79e4bed05 100644 --- a/src/helpers/misc/get_gateway_bot.ts +++ b/src/helpers/misc/get_gateway_bot.ts @@ -1,8 +1,8 @@ -import { rest } from "../../rest/rest.ts"; import type { GetGatewayBot } from "../../types/gateway/get_gateway_bot.ts"; -import { endpoints } from "../../util/constants.ts"; +import {Bot} from "../../bot.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Get the bots Gateway metadata that can help during the operation of large or sharded bots. */ -export async function getGatewayBot() { - return await rest.runMethod("get", endpoints.GATEWAY_BOT); +export async function getGatewayBot(bot: Bot) { + return await bot.rest.runMethod>(bot.rest,"get", bot.constants.endpoints.GATEWAY_BOT); } diff --git a/src/helpers/misc/get_user.ts b/src/helpers/misc/get_user.ts index f4632beb5..5a96dfc7d 100644 --- a/src/helpers/misc/get_user.ts +++ b/src/helpers/misc/get_user.ts @@ -1,8 +1,8 @@ -import { rest } from "../../rest/rest.ts"; import type { User } from "../../types/users/user.ts"; -import { endpoints } from "../../util/constants.ts"; +import {Bot} from "../../bot.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** This function will return the raw user payload in the rare cases you need to fetch a user directly from the API. */ -export async function getUser(userId: bigint) { - return await rest.runMethod("get", endpoints.USER(userId)); +export async function getUser(bot: Bot, userId: bigint) { + return await bot.rest.runMethod>(bot.rest,"get", bot.constants.endpoints.USER(userId)); } diff --git a/src/helpers/oauth/get_application.ts b/src/helpers/oauth/get_application.ts index 425819ad0..4262eba04 100644 --- a/src/helpers/oauth/get_application.ts +++ b/src/helpers/oauth/get_application.ts @@ -1,8 +1,8 @@ -import { rest } from "../../rest/rest.ts"; -import { endpoints } from "../../util/constants.ts"; import { Application } from "../../types/applications/application.ts"; +import { Bot } from "../../bot.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Get the applications info */ -export async function getApplicationInfo() { - return await rest.runMethod>("get", endpoints.OAUTH2_APPLICATION); +export async function getApplicationInfo(bot: Bot) { + return await bot.rest.runMethod>>(bot.rest,"get", bot.constants.endpoints.OAUTH2_APPLICATION); } diff --git a/src/helpers/voice/connect_to_voice_channel.ts b/src/helpers/voice/connect_to_voice_channel.ts index eb815c083..510d45be9 100644 --- a/src/helpers/voice/connect_to_voice_channel.ts +++ b/src/helpers/voice/connect_to_voice_channel.ts @@ -1,26 +1,23 @@ -import { DiscordGatewayOpcodes } from "../../types/codes/gateway_opcodes.ts"; import type { UpdateVoiceState } from "../../types/voice/update_voice_state.ts"; -import { requireBotChannelPermissions } from "../../util/permissions.ts"; -import { calculateShardId } from "../../util/calculate_shard_id.ts"; -import { snakelize } from "../../util/utils.ts"; -import { ws } from "../../ws/ws.ts"; import type { AtLeastOne } from "../../types/util.ts"; +import {Bot} from "../../bot.ts"; /** Connect or join a voice channel inside a guild. By default, the "selfDeaf" option is true. Requires `CONNECT` and `VIEW_CHANNEL` permissions. */ export async function connectToVoiceChannel( + bot: Bot, guildId: bigint, channelId: bigint, options?: AtLeastOne> ) { - await requireBotChannelPermissions(channelId, ["CONNECT", "VIEW_CHANNEL"]); + await bot.utils.requireBotChannelPermissions(bot, channelId, ["CONNECT", "VIEW_CHANNEL"]); - ws.sendShardMessage(calculateShardId(guildId), { - op: DiscordGatewayOpcodes.VoiceStateUpdate, - d: snakelize({ - guildId, - channelId, - selfMute: Boolean(options?.selfMute), - selfDeaf: options?.selfDeaf ?? true, - }), + bot.ws.sendShardMessage(bot.utils.calculateShardId(guildId), { + op: bot.constants.DiscordGatewayOpcodes.VoiceStateUpdate, + d: { + guild_id: guildId, + channel_id: channelId, + self_mute: Boolean(options?.selfMute), + self_deaf: options?.selfDeaf ?? true, + }, }); } From 6ece2dc1067c18a78395b7584692aa484700af3d Mon Sep 17 00:00:00 2001 From: TriForMine Date: Mon, 18 Oct 2021 19:04:01 +0000 Subject: [PATCH 10/34] change: prettier code --- src/bot.ts | 2 +- .../integrations/delete_integration.ts | 8 ++++-- src/helpers/invites/create_invite.ts | 25 +++++++++++-------- src/helpers/invites/delete_invite.ts | 10 +++++--- src/helpers/invites/get_channel_invites.ts | 10 +++++--- src/helpers/invites/get_invite.ts | 16 +++++++----- src/helpers/invites/get_invites.ts | 9 ++++--- src/helpers/misc/edit_bot_profile.ts | 6 ++--- src/helpers/misc/edit_bot_status.ts | 4 +-- src/helpers/misc/get_gateway_bot.ts | 10 +++++--- src/helpers/misc/get_user.ts | 10 +++++--- src/helpers/oauth/get_application.ts | 8 ++++-- src/helpers/voice/connect_to_voice_channel.ts | 2 +- 13 files changed, 78 insertions(+), 42 deletions(-) diff --git a/src/bot.ts b/src/bot.ts index d24a6ebd4..39f7a29bb 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -12,7 +12,7 @@ import { highestRole, higherRolePosition, requireBotChannelPermissions, - requireBotGuildPermissions + requireBotGuildPermissions, } from "./util/permissions.ts"; import { getGatewayBot } from "./helpers/misc/get_gateway_bot.ts"; import { diff --git a/src/helpers/integrations/delete_integration.ts b/src/helpers/integrations/delete_integration.ts index 68893fd1c..75c00cc1a 100644 --- a/src/helpers/integrations/delete_integration.ts +++ b/src/helpers/integrations/delete_integration.ts @@ -1,8 +1,12 @@ -import {Bot} from "../../bot.ts"; +import { Bot } from "../../bot.ts"; /** Delete the attached integration object for the guild with this id. Requires MANAGE_GUILD permission. */ export async function deleteIntegration(bot: Bot, guildId: bigint, id: bigint) { await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_GUILD"]); - return await bot.rest.runMethod(bot.rest,"delete", bot.constants.endpoints.GUILD_INTEGRATION(guildId, id)); + return await bot.rest.runMethod( + bot.rest, + "delete", + bot.constants.endpoints.GUILD_INTEGRATION(guildId, id) + ); } diff --git a/src/helpers/invites/create_invite.ts b/src/helpers/invites/create_invite.ts index fe3189b71..a18b66f45 100644 --- a/src/helpers/invites/create_invite.ts +++ b/src/helpers/invites/create_invite.ts @@ -2,7 +2,7 @@ import type { CreateChannelInvite } from "../../types/invites/create_channel_inv import type { InviteMetadata } from "../../types/invites/invite_metadata.ts"; import { Errors } from "../../types/discordeno/errors.ts"; import { Bot } from "../../bot.ts"; -import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; +import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; /** Creates a new invite for this channel. Requires CREATE_INSTANT_INVITE */ export async function createInvite(bot: Bot, channelId: bigint, options: CreateChannelInvite = {}) { @@ -15,13 +15,18 @@ export async function createInvite(bot: Bot, channelId: bigint, options: CreateC throw new Error(Errors.INVITE_MAX_USES_INVALID); } - return await bot.rest.runMethod>(bot.rest,"post", bot.constants.endpoints.CHANNEL_INVITES(channelId), { - max_age: options.maxAge, - max_uses: options.maxUses, - temporary: options.temporary, - unique: options.unique, - target_type: options.targetType, - target_user_id: options.targetUserId, - target_application_id: options.targetUserId, - }); + return await bot.rest.runMethod>( + bot.rest, + "post", + bot.constants.endpoints.CHANNEL_INVITES(channelId), + { + max_age: options.maxAge, + max_uses: options.maxUses, + temporary: options.temporary, + unique: options.unique, + target_type: options.targetType, + target_user_id: options.targetUserId, + target_application_id: options.targetUserId, + } + ); } diff --git a/src/helpers/invites/delete_invite.ts b/src/helpers/invites/delete_invite.ts index aecd9f62a..ced9fd715 100644 --- a/src/helpers/invites/delete_invite.ts +++ b/src/helpers/invites/delete_invite.ts @@ -1,6 +1,6 @@ import type { InviteMetadata } from "../../types/invites/invite_metadata.ts"; -import {Bot} from "../../bot.ts"; -import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; +import { Bot } from "../../bot.ts"; +import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; /** Deletes an invite for the given code. Requires `MANAGE_CHANNELS` or `MANAGE_GUILD` permission */ export async function deleteInvite(bot: Bot, channelId: bigint, inviteCode: string) { @@ -13,5 +13,9 @@ export async function deleteInvite(bot: Bot, channelId: bigint, inviteCode: stri } } - return await bot.rest.runMethod>(bot.rest,"delete", bot.constants.endpoints.INVITE(inviteCode)); + return await bot.rest.runMethod>( + bot.rest, + "delete", + bot.constants.endpoints.INVITE(inviteCode) + ); } diff --git a/src/helpers/invites/get_channel_invites.ts b/src/helpers/invites/get_channel_invites.ts index 8fdfadf0d..b889f78fb 100644 --- a/src/helpers/invites/get_channel_invites.ts +++ b/src/helpers/invites/get_channel_invites.ts @@ -1,13 +1,17 @@ import type { InviteMetadata } from "../../types/invites/invite_metadata.ts"; import { Collection } from "../../util/collection.ts"; -import {Bot} from "../../bot.ts"; -import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; +import { Bot } from "../../bot.ts"; +import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; /** Gets the invites for this channel. Requires MANAGE_CHANNEL */ export async function getChannelInvites(bot: Bot, channelId: bigint) { await bot.utils.requireBotChannelPermissions(channelId, ["MANAGE_CHANNELS"]); - const result = await bot.rest.runMethod[]>(bot.rest,"get", bot.constants.endpoints.CHANNEL_INVITES(channelId)); + const result = await bot.rest.runMethod[]>( + bot.rest, + "get", + bot.constants.endpoints.CHANNEL_INVITES(channelId) + ); return new Collection(result.map((invite) => [invite.code, invite])); } diff --git a/src/helpers/invites/get_invite.ts b/src/helpers/invites/get_invite.ts index 0a26a311e..51037c241 100644 --- a/src/helpers/invites/get_invite.ts +++ b/src/helpers/invites/get_invite.ts @@ -1,12 +1,16 @@ import { GetInvite } from "../../types/invites/get_invite.ts"; import type { InviteMetadata } from "../../types/invites/invite_metadata.ts"; -import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; -import {Bot} from "../../bot.ts"; +import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; +import { Bot } from "../../bot.ts"; /** Returns an invite for the given code or throws an error if the invite doesn't exists. */ export async function getInvite(bot: Bot, inviteCode: string, options?: GetInvite) { - return await bot.rest.runMethod>("get", bot.constants.endpoints.INVITE(inviteCode), { - with_counts: options.withCounts, - with_expiration: options.withExpiration, - }); + return await bot.rest.runMethod>( + "get", + bot.constants.endpoints.INVITE(inviteCode), + { + with_counts: options.withCounts, + with_expiration: options.withExpiration, + } + ); } diff --git a/src/helpers/invites/get_invites.ts b/src/helpers/invites/get_invites.ts index 43e5c6ae4..5a8e88760 100644 --- a/src/helpers/invites/get_invites.ts +++ b/src/helpers/invites/get_invites.ts @@ -1,13 +1,16 @@ import type { InviteMetadata } from "../../types/invites/invite_metadata.ts"; import { Collection } from "../../util/collection.ts"; -import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; -import {Bot} from "../../bot.ts"; +import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; +import { Bot } from "../../bot.ts"; /** Get all the invites for this guild. Requires MANAGE_GUILD permission */ export async function getInvites(bot: Bot, guildId: bigint) { await bot.utils.requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); - const result = await bot.rest.runMethod[]>("get", bot.constants.endpoints.GUILD_INVITES(guildId)); + const result = await bot.rest.runMethod[]>( + "get", + bot.constants.endpoints.GUILD_INVITES(guildId) + ); return new Collection(result.map((invite) => [invite.code, invite])); } diff --git a/src/helpers/misc/edit_bot_profile.ts b/src/helpers/misc/edit_bot_profile.ts index 769e8dc90..66dac6510 100644 --- a/src/helpers/misc/edit_bot_profile.ts +++ b/src/helpers/misc/edit_bot_profile.ts @@ -1,7 +1,7 @@ import { Errors } from "../../types/discordeno/errors.ts"; import type { User } from "../../types/users/user.ts"; -import {Bot} from "../../bot.ts"; -import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; +import { Bot } from "../../bot.ts"; +import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; /** Modifies the bot's username or avatar. * NOTE: username: if changed may cause the bot's discriminator to be randomized. @@ -27,7 +27,7 @@ export async function editBotProfile(bot: Bot, options: { username?: string; bot const avatar = options?.botAvatarURL ? await bot.utils.urlToBase64(options?.botAvatarURL) : options?.botAvatarURL; - return await bot.rest.runMethod>(bot,"patch", bot.constants.endpoints.USER_BOT, { + return await bot.rest.runMethod>(bot, "patch", bot.constants.endpoints.USER_BOT, { username: options.username?.trim(), avatar, }); diff --git a/src/helpers/misc/edit_bot_status.ts b/src/helpers/misc/edit_bot_status.ts index aadbeab10..7bba6658c 100644 --- a/src/helpers/misc/edit_bot_status.ts +++ b/src/helpers/misc/edit_bot_status.ts @@ -1,4 +1,4 @@ -import {Bot} from "../../bot.ts"; +import { Bot } from "../../bot.ts"; import { DiscordGatewayOpcodes } from "../../types/codes/gateway_opcodes.ts"; import type { StatusUpdate } from "../../types/gateway/status_update.ts"; @@ -12,7 +12,7 @@ export function editBotStatus(bot: Bot, data: Omit>(bot.rest,"get", bot.constants.endpoints.GATEWAY_BOT); + return await bot.rest.runMethod>( + bot.rest, + "get", + bot.constants.endpoints.GATEWAY_BOT + ); } diff --git a/src/helpers/misc/get_user.ts b/src/helpers/misc/get_user.ts index 5a96dfc7d..eb3de22f7 100644 --- a/src/helpers/misc/get_user.ts +++ b/src/helpers/misc/get_user.ts @@ -1,8 +1,12 @@ import type { User } from "../../types/users/user.ts"; -import {Bot} from "../../bot.ts"; -import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; +import { Bot } from "../../bot.ts"; +import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; /** This function will return the raw user payload in the rare cases you need to fetch a user directly from the API. */ export async function getUser(bot: Bot, userId: bigint) { - return await bot.rest.runMethod>(bot.rest,"get", bot.constants.endpoints.USER(userId)); + return await bot.rest.runMethod>( + bot.rest, + "get", + bot.constants.endpoints.USER(userId) + ); } diff --git a/src/helpers/oauth/get_application.ts b/src/helpers/oauth/get_application.ts index 4262eba04..38e48af6b 100644 --- a/src/helpers/oauth/get_application.ts +++ b/src/helpers/oauth/get_application.ts @@ -1,8 +1,12 @@ import { Application } from "../../types/applications/application.ts"; import { Bot } from "../../bot.ts"; -import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; +import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; /** Get the applications info */ export async function getApplicationInfo(bot: Bot) { - return await bot.rest.runMethod>>(bot.rest,"get", bot.constants.endpoints.OAUTH2_APPLICATION); + return await bot.rest.runMethod>>( + bot.rest, + "get", + bot.constants.endpoints.OAUTH2_APPLICATION + ); } diff --git a/src/helpers/voice/connect_to_voice_channel.ts b/src/helpers/voice/connect_to_voice_channel.ts index 510d45be9..5d986ed94 100644 --- a/src/helpers/voice/connect_to_voice_channel.ts +++ b/src/helpers/voice/connect_to_voice_channel.ts @@ -1,6 +1,6 @@ import type { UpdateVoiceState } from "../../types/voice/update_voice_state.ts"; import type { AtLeastOne } from "../../types/util.ts"; -import {Bot} from "../../bot.ts"; +import { Bot } from "../../bot.ts"; /** Connect or join a voice channel inside a guild. By default, the "selfDeaf" option is true. Requires `CONNECT` and `VIEW_CHANNEL` permissions. */ export async function connectToVoiceChannel( From 026c6461f460c1ab82206b531373e8ab3ae70a05 Mon Sep 17 00:00:00 2001 From: TriForMine Date: Mon, 18 Oct 2021 21:04:51 +0200 Subject: [PATCH 11/34] yes --- src/helpers/integrations/get_integrations.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/helpers/integrations/get_integrations.ts b/src/helpers/integrations/get_integrations.ts index c7070e582..2944af268 100644 --- a/src/helpers/integrations/get_integrations.ts +++ b/src/helpers/integrations/get_integrations.ts @@ -1,11 +1,10 @@ -import { rest } from "../../rest/rest.ts"; import type { Integration } from "../../types/integrations/integration.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotGuildPermissions } from "../../util/permissions.ts"; +import {Bot} from "../../bot.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Returns a list of integrations for the guild. Requires the MANAGE_GUILD permission. */ -export async function getIntegrations(guildId: bigint) { - await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); +export async function getIntegrations(bot: Bot, guildId: bigint) { + await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_GUILD"]); - return await rest.runMethod("get", endpoints.GUILD_INTEGRATIONS(guildId)); + return await bot.rest.runMethod>(bot.rest,"get", bot.constants.endpoints.GUILD_INTEGRATIONS(guildId)); } From c0cf0ebba572a9d94d219aea21e1bfb17e5c0a0b Mon Sep 17 00:00:00 2001 From: TriForMine Date: Mon, 18 Oct 2021 21:12:42 +0200 Subject: [PATCH 12/34] Emojis --- src/helpers/emojis/create_emoji.ts | 19 ++++++++----------- src/helpers/emojis/delete_emoji.ts | 10 ++++------ src/helpers/emojis/edit_emoji.ts | 11 +++++------ src/helpers/emojis/get_emoji.ts | 12 +++++------- src/helpers/emojis/get_emojis.ts | 18 +++++++----------- 5 files changed, 29 insertions(+), 41 deletions(-) diff --git a/src/helpers/emojis/create_emoji.ts b/src/helpers/emojis/create_emoji.ts index e16adbe5d..2687fe34c 100644 --- a/src/helpers/emojis/create_emoji.ts +++ b/src/helpers/emojis/create_emoji.ts @@ -1,20 +1,17 @@ -import { rest } from "../../rest/rest.ts"; -import { CreateGuildEmoji } from "../../types/emojis/create_guild_emoji.ts"; +import type { CreateGuildEmoji } from "../../types/emojis/create_guild_emoji.ts"; import type { Emoji } from "../../types/emojis/emoji.ts"; -import { snowflakeToBigint } from "../../util/bigint.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotGuildPermissions } from "../../util/permissions.ts"; -import { urlToBase64 } from "../../util/utils.ts"; +import type {Bot} from "../../bot.ts"; +import type {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Create an emoji in the server. Emojis and animated emojis have a maximum file size of 256kb. Attempting to upload an emoji larger than this limit will fail and return 400 Bad Request and an error message, but not a JSON status code. If a URL is provided to the image parameter, Discordeno will automatically convert it to a base64 string internally. */ -export async function createEmoji(guildId: bigint, name: string, image: string, options: CreateGuildEmoji) { - await requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]); +export async function createEmoji(bot: Bot, guildId: bigint, name: string, image: string, options: CreateGuildEmoji) { + await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_EMOJIS"]); if (image && !image.startsWith("data:image/")) { - image = await urlToBase64(image); + image = await bot.utils.urlToBase64(image); } - const emoji = await rest.runMethod("post", endpoints.GUILD_EMOJIS(guildId), { + const emoji = await bot.rest.runMethod>("post", bot.constants.endpoints.GUILD_EMOJIS(guildId), { ...options, name, image, @@ -22,6 +19,6 @@ export async function createEmoji(guildId: bigint, name: string, image: string, return { ...emoji, - id: snowflakeToBigint(emoji.id!), + id: bot.transformers.snowflake(emoji.id!), }; } diff --git a/src/helpers/emojis/delete_emoji.ts b/src/helpers/emojis/delete_emoji.ts index 402a095dd..9119ff5f0 100644 --- a/src/helpers/emojis/delete_emoji.ts +++ b/src/helpers/emojis/delete_emoji.ts @@ -1,10 +1,8 @@ -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 given emoji. Requires the MANAGE_EMOJIS permission. Returns 204 No Content on success. */ -export async function deleteEmoji(guildId: bigint, id: bigint, reason?: string) { - await requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]); +export async function deleteEmoji(bot: Bot, guildId: bigint, id: bigint, reason?: string) { + await bot.utils.requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]); - return await rest.runMethod("delete", endpoints.GUILD_EMOJI(guildId, id), { reason }); + return await bot.rest.runMethod(bot.rest,"delete", bot.constants.endpoints.GUILD_EMOJI(guildId, id), { reason }); } diff --git a/src/helpers/emojis/edit_emoji.ts b/src/helpers/emojis/edit_emoji.ts index c1917ebef..be3e9149d 100644 --- a/src/helpers/emojis/edit_emoji.ts +++ b/src/helpers/emojis/edit_emoji.ts @@ -1,12 +1,11 @@ -import { rest } from "../../rest/rest.ts"; import type { Emoji } from "../../types/emojis/emoji.ts"; import type { ModifyGuildEmoji } from "../../types/emojis/modify_guild_emoji.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotGuildPermissions } from "../../util/permissions.ts"; +import type {Bot} from "../../bot.ts"; +import type {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Modify the given emoji. Requires the MANAGE_EMOJIS permission. */ -export async function editEmoji(guildId: bigint, id: bigint, options: ModifyGuildEmoji) { - await requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]); +export async function editEmoji(bot: Bot, guildId: bigint, id: bigint, options: ModifyGuildEmoji) { + await bot.utils.requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]); - return await rest.runMethod("patch", endpoints.GUILD_EMOJI(guildId, id), options); + return await bot.rest.runMethod>("patch", bot.constants.endpoints.GUILD_EMOJI(guildId, id), options); } diff --git a/src/helpers/emojis/get_emoji.ts b/src/helpers/emojis/get_emoji.ts index 7bde027ef..631e808b8 100644 --- a/src/helpers/emojis/get_emoji.ts +++ b/src/helpers/emojis/get_emoji.ts @@ -1,22 +1,20 @@ -import { cacheHandlers } from "../../cache.ts"; -import { rest } from "../../rest/rest.ts"; import type { Emoji } from "../../types/emojis/emoji.ts"; import { Errors } from "../../types/discordeno/errors.ts"; -import { endpoints } from "../../util/constants.ts"; +import {Bot} from "../../bot.ts"; /** * Returns an emoji for the given guild and emoji Id. * * ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()?.emojis */ -export async function getEmoji(guildId: bigint, emojiId: bigint, addToCache = true) { - const result = await rest.runMethod("get", endpoints.GUILD_EMOJI(guildId, emojiId)); +export async function getEmoji(bot: Bot, guildId: bigint, emojiId: bigint, addToCache = true) { + const result = await bot.rest.runMethod("get", bot.constants.endpoints.GUILD_EMOJI(guildId, emojiId)); if (addToCache) { - const guild = await cacheHandlers.get("guilds", guildId); + const guild = await bot.cache.guilds.get(guildId); if (!guild) throw new Error(Errors.GUILD_NOT_FOUND); guild.emojis.set(emojiId, result); - await cacheHandlers.set("guilds", guildId, guild); + await bot.cache.guilds.set(guildId, guild); } return result; diff --git a/src/helpers/emojis/get_emojis.ts b/src/helpers/emojis/get_emojis.ts index babbeb3c7..30bf9ae49 100644 --- a/src/helpers/emojis/get_emojis.ts +++ b/src/helpers/emojis/get_emojis.ts @@ -1,30 +1,26 @@ -import { eventHandlers } from "../../bot.ts"; -import { cacheHandlers } from "../../cache.ts"; -import { rest } from "../../rest/rest.ts"; +import {Bot} from "../../bot.ts"; import type { Emoji } from "../../types/emojis/emoji.ts"; import { Errors } from "../../types/discordeno/errors.ts"; -import { snowflakeToBigint } from "../../util/bigint.ts"; import { Collection } from "../../util/collection.ts"; -import { endpoints } from "../../util/constants.ts"; /** * Returns a list of emojis for the given guild. * * ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()?.emojis */ -export async function getEmojis(guildId: bigint, addToCache = true) { - const result = await rest.runMethod("get", endpoints.GUILD_EMOJIS(guildId)); +export async function getEmojis(bot: Bot, guildId: bigint, addToCache = true) { + const result = await bot.rest.runMethod(bot.rest,"get", bot.constants.endpoints.GUILD_EMOJIS(guildId)); if (addToCache) { - const guild = await cacheHandlers.get("guilds", guildId); + const guild = await bot.cache.guilds.get("guilds", guildId); if (!guild) throw new Error(Errors.GUILD_NOT_FOUND); result.forEach((emoji) => { - eventHandlers.debug?.("loop", `Running forEach loop in get_emojis file.`); - guild.emojis.set(snowflakeToBigint(emoji.id!), emoji); + bot.events.debug("loop", `Running forEach loop in get_emojis file.`); + guild.emojis.set(bot.transformers.snowflake(emoji.id!), emoji); }); - await cacheHandlers.set("guilds", guildId, guild); + await bot.cache.guilds.set(guildId, guild); } return new Collection(result.map((e) => [e.id!, e])); From b27e7c6d13dfd973fd1a73b991c44521306b362d Mon Sep 17 00:00:00 2001 From: TriForMine Date: Mon, 18 Oct 2021 19:13:17 +0000 Subject: [PATCH 13/34] change: prettier code --- src/helpers/emojis/create_emoji.ts | 18 +++++++++++------- src/helpers/emojis/delete_emoji.ts | 6 ++++-- src/helpers/emojis/edit_emoji.ts | 10 +++++++--- src/helpers/emojis/get_emoji.ts | 2 +- src/helpers/emojis/get_emojis.ts | 4 ++-- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/helpers/emojis/create_emoji.ts b/src/helpers/emojis/create_emoji.ts index 2687fe34c..ec1b518f3 100644 --- a/src/helpers/emojis/create_emoji.ts +++ b/src/helpers/emojis/create_emoji.ts @@ -1,7 +1,7 @@ import type { CreateGuildEmoji } from "../../types/emojis/create_guild_emoji.ts"; import type { Emoji } from "../../types/emojis/emoji.ts"; -import type {Bot} from "../../bot.ts"; -import type {SnakeCasedPropertiesDeep} from "../../types/util.ts"; +import type { Bot } from "../../bot.ts"; +import type { SnakeCasedPropertiesDeep } from "../../types/util.ts"; /** Create an emoji in the server. Emojis and animated emojis have a maximum file size of 256kb. Attempting to upload an emoji larger than this limit will fail and return 400 Bad Request and an error message, but not a JSON status code. If a URL is provided to the image parameter, Discordeno will automatically convert it to a base64 string internally. */ export async function createEmoji(bot: Bot, guildId: bigint, name: string, image: string, options: CreateGuildEmoji) { @@ -11,11 +11,15 @@ export async function createEmoji(bot: Bot, guildId: bigint, name: string, image image = await bot.utils.urlToBase64(image); } - const emoji = await bot.rest.runMethod>("post", bot.constants.endpoints.GUILD_EMOJIS(guildId), { - ...options, - name, - image, - }); + const emoji = await bot.rest.runMethod>( + "post", + bot.constants.endpoints.GUILD_EMOJIS(guildId), + { + ...options, + name, + image, + } + ); return { ...emoji, diff --git a/src/helpers/emojis/delete_emoji.ts b/src/helpers/emojis/delete_emoji.ts index 9119ff5f0..2369a0c02 100644 --- a/src/helpers/emojis/delete_emoji.ts +++ b/src/helpers/emojis/delete_emoji.ts @@ -1,8 +1,10 @@ -import type {Bot} from "../../bot.ts"; +import type { Bot } from "../../bot.ts"; /** Delete the given emoji. Requires the MANAGE_EMOJIS permission. Returns 204 No Content on success. */ export async function deleteEmoji(bot: Bot, guildId: bigint, id: bigint, reason?: string) { await bot.utils.requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]); - return await bot.rest.runMethod(bot.rest,"delete", bot.constants.endpoints.GUILD_EMOJI(guildId, id), { reason }); + return await bot.rest.runMethod(bot.rest, "delete", bot.constants.endpoints.GUILD_EMOJI(guildId, id), { + reason, + }); } diff --git a/src/helpers/emojis/edit_emoji.ts b/src/helpers/emojis/edit_emoji.ts index be3e9149d..6d0445a07 100644 --- a/src/helpers/emojis/edit_emoji.ts +++ b/src/helpers/emojis/edit_emoji.ts @@ -1,11 +1,15 @@ import type { Emoji } from "../../types/emojis/emoji.ts"; import type { ModifyGuildEmoji } from "../../types/emojis/modify_guild_emoji.ts"; -import type {Bot} from "../../bot.ts"; -import type {SnakeCasedPropertiesDeep} from "../../types/util.ts"; +import type { Bot } from "../../bot.ts"; +import type { SnakeCasedPropertiesDeep } from "../../types/util.ts"; /** Modify the given emoji. Requires the MANAGE_EMOJIS permission. */ export async function editEmoji(bot: Bot, guildId: bigint, id: bigint, options: ModifyGuildEmoji) { await bot.utils.requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]); - return await bot.rest.runMethod>("patch", bot.constants.endpoints.GUILD_EMOJI(guildId, id), options); + return await bot.rest.runMethod>( + "patch", + bot.constants.endpoints.GUILD_EMOJI(guildId, id), + options + ); } diff --git a/src/helpers/emojis/get_emoji.ts b/src/helpers/emojis/get_emoji.ts index 631e808b8..a99ec6baf 100644 --- a/src/helpers/emojis/get_emoji.ts +++ b/src/helpers/emojis/get_emoji.ts @@ -1,6 +1,6 @@ import type { Emoji } from "../../types/emojis/emoji.ts"; import { Errors } from "../../types/discordeno/errors.ts"; -import {Bot} from "../../bot.ts"; +import { Bot } from "../../bot.ts"; /** * Returns an emoji for the given guild and emoji Id. diff --git a/src/helpers/emojis/get_emojis.ts b/src/helpers/emojis/get_emojis.ts index 30bf9ae49..be3141caa 100644 --- a/src/helpers/emojis/get_emojis.ts +++ b/src/helpers/emojis/get_emojis.ts @@ -1,4 +1,4 @@ -import {Bot} from "../../bot.ts"; +import { Bot } from "../../bot.ts"; import type { Emoji } from "../../types/emojis/emoji.ts"; import { Errors } from "../../types/discordeno/errors.ts"; import { Collection } from "../../util/collection.ts"; @@ -9,7 +9,7 @@ import { Collection } from "../../util/collection.ts"; * ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()?.emojis */ export async function getEmojis(bot: Bot, guildId: bigint, addToCache = true) { - const result = await bot.rest.runMethod(bot.rest,"get", bot.constants.endpoints.GUILD_EMOJIS(guildId)); + const result = await bot.rest.runMethod(bot.rest, "get", bot.constants.endpoints.GUILD_EMOJIS(guildId)); if (addToCache) { const guild = await bot.cache.guilds.get("guilds", guildId); From 5c82abc3d23eaf007ad26317d058f3042efffbd8 Mon Sep 17 00:00:00 2001 From: TriForMine Date: Mon, 18 Oct 2021 21:20:57 +0200 Subject: [PATCH 14/34] Discovery --- .../discovery/add_discovery_subcategory.ts | 16 ++++++++-------- src/helpers/discovery/edit_discovery.ts | 16 +++++++++------- src/helpers/discovery/get_discovery.ts | 11 +++++------ .../discovery/get_discovery_categories.ts | 10 +++++----- .../discovery/remove_discovery_subcategory.ts | 10 ++++------ src/helpers/discovery/valid_discovery_term.ts | 8 ++++---- 6 files changed, 35 insertions(+), 36 deletions(-) diff --git a/src/helpers/discovery/add_discovery_subcategory.ts b/src/helpers/discovery/add_discovery_subcategory.ts index 038431dcf..fa5020979 100644 --- a/src/helpers/discovery/add_discovery_subcategory.ts +++ b/src/helpers/discovery/add_discovery_subcategory.ts @@ -1,14 +1,14 @@ -import { rest } from "../../rest/rest.ts"; import type { AddGuildDiscoverySubcategory } from "../../types/discovery/add_guild_discovery_subcategory.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotGuildPermissions } from "../../util/permissions.ts"; +import {Bot} from "../../bot.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Add a discovery subcategory to the guild. Requires the `MANAGE_GUILD` permission. */ -export async function addDiscoverySubcategory(guildId: bigint, categoryId: number) { - await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); +export async function addDiscoverySubcategory(bot: Bot, guildId: bigint, categoryId: number) { + await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_GUILD"]); - return await rest.runMethod( - "post", - endpoints.DISCOVERY_SUBCATEGORY(guildId, categoryId) + return await bot.rest.runMethod>( + bot.rest, + "post", + bot.constants.endpoints.DISCOVERY_SUBCATEGORY(guildId, categoryId) ); } diff --git a/src/helpers/discovery/edit_discovery.ts b/src/helpers/discovery/edit_discovery.ts index d894477df..dad432f7f 100644 --- a/src/helpers/discovery/edit_discovery.ts +++ b/src/helpers/discovery/edit_discovery.ts @@ -1,13 +1,15 @@ -import { rest } from "../../rest/rest.ts"; import type { DiscoveryMetadata } from "../../types/discovery/discovery_metadata.ts"; import type { ModifyGuildDiscoveryMetadata } from "../../types/discovery/modify_guild_discovery_metadata.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotGuildPermissions } from "../../util/permissions.ts"; -import { snakelize } from "../../util/utils.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; +import {Bot} from "../../bot.ts"; /** Modify the discovery metadata for the guild. Requires the MANAGE_GUILD permission. Returns the updated discovery metadata object on success. */ -export async function editDiscovery(guildId: bigint, data: ModifyGuildDiscoveryMetadata) { - await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); +export async function editDiscovery(bot: Bot, guildId: bigint, data: ModifyGuildDiscoveryMetadata) { + await bot.utils.requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); - return await rest.runMethod("patch", endpoints.DISCOVERY_METADATA(guildId), snakelize(data)); + return await bot.rest.runMethod>(bot.rest,"patch", bot.constants.endpoints.DISCOVERY_METADATA(guildId), { + primary_category_id: data.primaryCategoryId, + keywords: data.keywords, + emoji_discoverability_enabled: data.emojiDiscoverabilityEnabled, + }); } diff --git a/src/helpers/discovery/get_discovery.ts b/src/helpers/discovery/get_discovery.ts index 967235fa2..c94913e0e 100644 --- a/src/helpers/discovery/get_discovery.ts +++ b/src/helpers/discovery/get_discovery.ts @@ -1,11 +1,10 @@ -import { rest } from "../../rest/rest.ts"; import type { DiscoveryMetadata } from "../../types/discovery/discovery_metadata.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotGuildPermissions } from "../../util/permissions.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; +import {Bot} from "../../bot.ts"; /** Returns the discovery metadata object for the guild. Requires the `MANAGE_GUILD` permission. */ -export async function getDiscovery(guildId: bigint) { - await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); +export async function getDiscovery(bot: Bot, guildId: bigint) { + await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_GUILD"]); - return await rest.runMethod("get", endpoints.DISCOVERY_METADATA(guildId)); + return await bot.rest.runMethod>(bot.rest,"get", bot.constants.endpoints.DISCOVERY_METADATA(guildId)); } diff --git a/src/helpers/discovery/get_discovery_categories.ts b/src/helpers/discovery/get_discovery_categories.ts index 012bafb64..e47f7ddb8 100644 --- a/src/helpers/discovery/get_discovery_categories.ts +++ b/src/helpers/discovery/get_discovery_categories.ts @@ -1,11 +1,11 @@ -import { rest } from "../../rest/rest.ts"; import type { DiscoveryCategory } from "../../types/discovery/discovery_category.ts"; import { Collection } from "../../util/collection.ts"; -import { endpoints } from "../../util/constants.ts"; +import {Bot} from "../../bot.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Returns a Collection (mapped by Id of the discovery category object) of discovery category objects that can be used when editing guilds */ -export async function getDiscoveryCategories() { - const result = await rest.runMethod("get", endpoints.DISCOVERY_CATEGORIES); +export async function getDiscoveryCategories(bot: Bot) { + const result = await bot.rest.runMethod[]>(bot.rest,"get", bot.constants.endpoints.DISCOVERY_CATEGORIES); - return new Collection(result.map((category) => [category.id, category])); + return new Collection>(result.map((category) => [category.id, category])); } diff --git a/src/helpers/discovery/remove_discovery_subcategory.ts b/src/helpers/discovery/remove_discovery_subcategory.ts index aaa9f219c..7d989e07e 100644 --- a/src/helpers/discovery/remove_discovery_subcategory.ts +++ b/src/helpers/discovery/remove_discovery_subcategory.ts @@ -1,10 +1,8 @@ -import { rest } from "../../rest/rest.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotGuildPermissions } from "../../util/permissions.ts"; +import {Bot} from "../../bot.ts"; /** Removes a discovery subcategory from the guild. Requires the MANAGE_GUILD permission. Returns a 204 No Content on success. */ -export async function removeDiscoverySubcategory(guildId: bigint, categoryId: number) { - await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); +export async function removeDiscoverySubcategory(bot: Bot, guildId: bigint, categoryId: number) { + await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_GUILD"]); - return await rest.runMethod("delete", endpoints.DISCOVERY_SUBCATEGORY(guildId, categoryId)); + return await bot.rest.runMethod(bot.rest,"delete", bot.constants.endpoints.DISCOVERY_SUBCATEGORY(guildId, categoryId)); } diff --git a/src/helpers/discovery/valid_discovery_term.ts b/src/helpers/discovery/valid_discovery_term.ts index 9e2a850f6..054bf142b 100644 --- a/src/helpers/discovery/valid_discovery_term.ts +++ b/src/helpers/discovery/valid_discovery_term.ts @@ -1,9 +1,9 @@ -import { rest } from "../../rest/rest.ts"; import type { ValidateDiscoverySearchTerm } from "../../types/discovery/validate_discovery_search_term.ts"; -import { endpoints } from "../../util/constants.ts"; +import {Bot} from "../../bot.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; -export async function validDiscoveryTerm(term: string) { - const result = await rest.runMethod("get", endpoints.DISCOVERY_VALID_TERM, { term }); +export async function validDiscoveryTerm(bot: Bot, term: string) { + const result = await bot.rest.runMethod>("get", bot.constants.endpoints.DISCOVERY_VALID_TERM, { term }); return result.valid; } From 252fe62b72d26fe604ec11f97e433f13a3b3020c Mon Sep 17 00:00:00 2001 From: TriForMine Date: Mon, 18 Oct 2021 19:21:33 +0000 Subject: [PATCH 15/34] change: prettier code --- .../discovery/add_discovery_subcategory.ts | 10 +++++----- src/helpers/discovery/edit_discovery.ts | 19 ++++++++++++------- src/helpers/discovery/get_discovery.ts | 10 +++++++--- .../discovery/get_discovery_categories.ts | 14 ++++++++++---- .../discovery/remove_discovery_subcategory.ts | 8 ++++++-- src/helpers/discovery/valid_discovery_term.ts | 10 +++++++--- 6 files changed, 47 insertions(+), 24 deletions(-) diff --git a/src/helpers/discovery/add_discovery_subcategory.ts b/src/helpers/discovery/add_discovery_subcategory.ts index fa5020979..b442ac23d 100644 --- a/src/helpers/discovery/add_discovery_subcategory.ts +++ b/src/helpers/discovery/add_discovery_subcategory.ts @@ -1,14 +1,14 @@ import type { AddGuildDiscoverySubcategory } from "../../types/discovery/add_guild_discovery_subcategory.ts"; -import {Bot} from "../../bot.ts"; -import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; +import { Bot } from "../../bot.ts"; +import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; /** Add a discovery subcategory to the guild. Requires the `MANAGE_GUILD` permission. */ export async function addDiscoverySubcategory(bot: Bot, guildId: bigint, categoryId: number) { await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_GUILD"]); return await bot.rest.runMethod>( - bot.rest, - "post", - bot.constants.endpoints.DISCOVERY_SUBCATEGORY(guildId, categoryId) + bot.rest, + "post", + bot.constants.endpoints.DISCOVERY_SUBCATEGORY(guildId, categoryId) ); } diff --git a/src/helpers/discovery/edit_discovery.ts b/src/helpers/discovery/edit_discovery.ts index dad432f7f..015149a53 100644 --- a/src/helpers/discovery/edit_discovery.ts +++ b/src/helpers/discovery/edit_discovery.ts @@ -1,15 +1,20 @@ import type { DiscoveryMetadata } from "../../types/discovery/discovery_metadata.ts"; import type { ModifyGuildDiscoveryMetadata } from "../../types/discovery/modify_guild_discovery_metadata.ts"; -import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; -import {Bot} from "../../bot.ts"; +import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; +import { Bot } from "../../bot.ts"; /** Modify the discovery metadata for the guild. Requires the MANAGE_GUILD permission. Returns the updated discovery metadata object on success. */ export async function editDiscovery(bot: Bot, guildId: bigint, data: ModifyGuildDiscoveryMetadata) { await bot.utils.requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); - return await bot.rest.runMethod>(bot.rest,"patch", bot.constants.endpoints.DISCOVERY_METADATA(guildId), { - primary_category_id: data.primaryCategoryId, - keywords: data.keywords, - emoji_discoverability_enabled: data.emojiDiscoverabilityEnabled, - }); + return await bot.rest.runMethod>( + bot.rest, + "patch", + bot.constants.endpoints.DISCOVERY_METADATA(guildId), + { + primary_category_id: data.primaryCategoryId, + keywords: data.keywords, + emoji_discoverability_enabled: data.emojiDiscoverabilityEnabled, + } + ); } diff --git a/src/helpers/discovery/get_discovery.ts b/src/helpers/discovery/get_discovery.ts index c94913e0e..985d28882 100644 --- a/src/helpers/discovery/get_discovery.ts +++ b/src/helpers/discovery/get_discovery.ts @@ -1,10 +1,14 @@ import type { DiscoveryMetadata } from "../../types/discovery/discovery_metadata.ts"; -import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; -import {Bot} from "../../bot.ts"; +import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; +import { Bot } from "../../bot.ts"; /** Returns the discovery metadata object for the guild. Requires the `MANAGE_GUILD` permission. */ export async function getDiscovery(bot: Bot, guildId: bigint) { await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_GUILD"]); - return await bot.rest.runMethod>(bot.rest,"get", bot.constants.endpoints.DISCOVERY_METADATA(guildId)); + return await bot.rest.runMethod>( + bot.rest, + "get", + bot.constants.endpoints.DISCOVERY_METADATA(guildId) + ); } diff --git a/src/helpers/discovery/get_discovery_categories.ts b/src/helpers/discovery/get_discovery_categories.ts index e47f7ddb8..b6a991c96 100644 --- a/src/helpers/discovery/get_discovery_categories.ts +++ b/src/helpers/discovery/get_discovery_categories.ts @@ -1,11 +1,17 @@ import type { DiscoveryCategory } from "../../types/discovery/discovery_category.ts"; import { Collection } from "../../util/collection.ts"; -import {Bot} from "../../bot.ts"; -import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; +import { Bot } from "../../bot.ts"; +import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; /** Returns a Collection (mapped by Id of the discovery category object) of discovery category objects that can be used when editing guilds */ export async function getDiscoveryCategories(bot: Bot) { - const result = await bot.rest.runMethod[]>(bot.rest,"get", bot.constants.endpoints.DISCOVERY_CATEGORIES); + const result = await bot.rest.runMethod[]>( + bot.rest, + "get", + bot.constants.endpoints.DISCOVERY_CATEGORIES + ); - return new Collection>(result.map((category) => [category.id, category])); + return new Collection>( + result.map((category) => [category.id, category]) + ); } diff --git a/src/helpers/discovery/remove_discovery_subcategory.ts b/src/helpers/discovery/remove_discovery_subcategory.ts index 7d989e07e..5abafb24d 100644 --- a/src/helpers/discovery/remove_discovery_subcategory.ts +++ b/src/helpers/discovery/remove_discovery_subcategory.ts @@ -1,8 +1,12 @@ -import {Bot} from "../../bot.ts"; +import { Bot } from "../../bot.ts"; /** Removes a discovery subcategory from the guild. Requires the MANAGE_GUILD permission. Returns a 204 No Content on success. */ export async function removeDiscoverySubcategory(bot: Bot, guildId: bigint, categoryId: number) { await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_GUILD"]); - return await bot.rest.runMethod(bot.rest,"delete", bot.constants.endpoints.DISCOVERY_SUBCATEGORY(guildId, categoryId)); + return await bot.rest.runMethod( + bot.rest, + "delete", + bot.constants.endpoints.DISCOVERY_SUBCATEGORY(guildId, categoryId) + ); } diff --git a/src/helpers/discovery/valid_discovery_term.ts b/src/helpers/discovery/valid_discovery_term.ts index 054bf142b..7c8a278b4 100644 --- a/src/helpers/discovery/valid_discovery_term.ts +++ b/src/helpers/discovery/valid_discovery_term.ts @@ -1,9 +1,13 @@ import type { ValidateDiscoverySearchTerm } from "../../types/discovery/validate_discovery_search_term.ts"; -import {Bot} from "../../bot.ts"; -import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; +import { Bot } from "../../bot.ts"; +import { SnakeCasedPropertiesDeep } from "../../types/util.ts"; export async function validDiscoveryTerm(bot: Bot, term: string) { - const result = await bot.rest.runMethod>("get", bot.constants.endpoints.DISCOVERY_VALID_TERM, { term }); + const result = await bot.rest.runMethod>( + "get", + bot.constants.endpoints.DISCOVERY_VALID_TERM, + { term } + ); return result.valid; } From 55cb4d43751c437a90ef0050df3c9d18899ec736 Mon Sep 17 00:00:00 2001 From: TriForMine Date: Mon, 18 Oct 2021 21:33:52 +0200 Subject: [PATCH 16/34] Roles + Fixes --- src/helpers/discovery/edit_discovery.ts | 2 +- src/helpers/emojis/delete_emoji.ts | 2 +- src/helpers/emojis/edit_emoji.ts | 2 +- src/helpers/invites/get_invites.ts | 2 +- src/helpers/roles/add_role.ts | 16 ++++++-------- src/helpers/roles/create_role.ts | 28 ++++++++++++------------- src/helpers/roles/delete_role.ts | 10 ++++----- src/helpers/roles/edit_role.ts | 21 ++++++++++--------- src/helpers/roles/get_roles.ts | 26 +++++++++-------------- src/helpers/roles/remove_role.ts | 16 ++++++-------- 10 files changed, 55 insertions(+), 70 deletions(-) diff --git a/src/helpers/discovery/edit_discovery.ts b/src/helpers/discovery/edit_discovery.ts index dad432f7f..efffacabc 100644 --- a/src/helpers/discovery/edit_discovery.ts +++ b/src/helpers/discovery/edit_discovery.ts @@ -5,7 +5,7 @@ import {Bot} from "../../bot.ts"; /** Modify the discovery metadata for the guild. Requires the MANAGE_GUILD permission. Returns the updated discovery metadata object on success. */ export async function editDiscovery(bot: Bot, guildId: bigint, data: ModifyGuildDiscoveryMetadata) { - await bot.utils.requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); + await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_GUILD"]); return await bot.rest.runMethod>(bot.rest,"patch", bot.constants.endpoints.DISCOVERY_METADATA(guildId), { primary_category_id: data.primaryCategoryId, diff --git a/src/helpers/emojis/delete_emoji.ts b/src/helpers/emojis/delete_emoji.ts index 2369a0c02..075a835fa 100644 --- a/src/helpers/emojis/delete_emoji.ts +++ b/src/helpers/emojis/delete_emoji.ts @@ -2,7 +2,7 @@ import type { Bot } from "../../bot.ts"; /** Delete the given emoji. Requires the MANAGE_EMOJIS permission. Returns 204 No Content on success. */ export async function deleteEmoji(bot: Bot, guildId: bigint, id: bigint, reason?: string) { - await bot.utils.requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]); + await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_EMOJIS"]); return await bot.rest.runMethod(bot.rest, "delete", bot.constants.endpoints.GUILD_EMOJI(guildId, id), { reason, diff --git a/src/helpers/emojis/edit_emoji.ts b/src/helpers/emojis/edit_emoji.ts index 6d0445a07..1c86e0ccb 100644 --- a/src/helpers/emojis/edit_emoji.ts +++ b/src/helpers/emojis/edit_emoji.ts @@ -5,7 +5,7 @@ import type { SnakeCasedPropertiesDeep } from "../../types/util.ts"; /** Modify the given emoji. Requires the MANAGE_EMOJIS permission. */ export async function editEmoji(bot: Bot, guildId: bigint, id: bigint, options: ModifyGuildEmoji) { - await bot.utils.requireBotGuildPermissions(guildId, ["MANAGE_EMOJIS"]); + await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_EMOJIS"]); return await bot.rest.runMethod>( "patch", diff --git a/src/helpers/invites/get_invites.ts b/src/helpers/invites/get_invites.ts index 5a8e88760..e254bee54 100644 --- a/src/helpers/invites/get_invites.ts +++ b/src/helpers/invites/get_invites.ts @@ -5,7 +5,7 @@ import { Bot } from "../../bot.ts"; /** Get all the invites for this guild. Requires MANAGE_GUILD permission */ export async function getInvites(bot: Bot, guildId: bigint) { - await bot.utils.requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); + await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_GUILD"]); const result = await bot.rest.runMethod[]>( "get", diff --git a/src/helpers/roles/add_role.ts b/src/helpers/roles/add_role.ts index 367e3911c..f4a224f98 100644 --- a/src/helpers/roles/add_role.ts +++ b/src/helpers/roles/add_role.ts @@ -1,17 +1,13 @@ -import { botId } from "../../bot.ts"; -import { rest } from "../../rest/rest.ts"; -import { Errors } from "../../types/discordeno/errors.ts"; -import { endpoints } from "../../util/constants.ts"; -import { isHigherPosition, requireBotGuildPermissions } from "../../util/permissions.ts"; +import {Bot} from "../../bot.ts"; /** Add a role to the member */ -export async function addRole(guildId: bigint, memberId: bigint, roleId: bigint, reason?: string) { - const isHigherRolePosition = await isHigherPosition(guildId, botId, roleId); +export async function addRole(bot: Bot, guildId: bigint, memberId: bigint, roleId: bigint, reason?: string) { + const isHigherRolePosition = await bot.utils.isHigherPosition(guildId, bot.id, roleId); if (!isHigherRolePosition) { - throw new Error(Errors.BOTS_HIGHEST_ROLE_TOO_LOW); + throw new Error(bot.constants.Errors.BOTS_HIGHEST_ROLE_TOO_LOW); } - await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]); + await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_ROLES"]); - return await rest.runMethod("put", endpoints.GUILD_MEMBER_ROLE(guildId, memberId, roleId), { reason }); + return await bot.rest.runMethod(bot.rest,"put", bot.constants.endpoints.GUILD_MEMBER_ROLE(guildId, memberId, roleId), { reason }); } diff --git a/src/helpers/roles/create_role.ts b/src/helpers/roles/create_role.ts index 978a790f3..5e52be6a7 100644 --- a/src/helpers/roles/create_role.ts +++ b/src/helpers/roles/create_role.ts @@ -1,31 +1,31 @@ -import { cacheHandlers } from "../../cache.ts"; -import { rest } from "../../rest/rest.ts"; -import { structures } from "../../structures/mod.ts"; -import { CreateGuildRole } from "../../types/guilds/create_guild_role.ts"; +import type { CreateGuildRole } from "../../types/guilds/create_guild_role.ts"; import type { Role } from "../../types/permissions/role.ts"; -import { endpoints } from "../../util/constants.ts"; -import { calculateBits, requireBotGuildPermissions } from "../../util/permissions.ts"; +import type {Bot} from "../../bot.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Create a new role for the guild. Requires the MANAGE_ROLES permission. */ -export async function createRole(guildId: bigint, options: CreateGuildRole, reason?: string) { - await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]); +export async function createRole(bot: Bot, guildId: bigint, options: CreateGuildRole, reason?: string) { + await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_ROLES"]); - const result = await rest.runMethod("post", endpoints.GUILD_ROLES(guildId), { - ...options, - permissions: calculateBits(options?.permissions || []), + const result = await bot.rest.runMethod>("post", bot.constants.endpoints.GUILD_ROLES(guildId), { + name: options.name, + color: options.color, + hoist: options.hoist, + mentionable: options.mentionable, + permissions: bot.utils.calculateBits(options?.permissions || []), reason, }); - const role = await structures.createDiscordenoRole({ + const role = await bot.transformers.role(bot,{ role: result, guildId, }); - const guild = await cacheHandlers.get("guilds", guildId); + const guild = await bot.cache.guilds.get(guildId); if (guild) { guild.roles.set(role.id, role); - await cacheHandlers.set("guilds", guildId, guild); + await bot.cache.guilds.set(guildId, guild); } return role; diff --git a/src/helpers/roles/delete_role.ts b/src/helpers/roles/delete_role.ts index 8825234f9..d4f082632 100644 --- a/src/helpers/roles/delete_role.ts +++ b/src/helpers/roles/delete_role.ts @@ -1,10 +1,8 @@ -import { rest } from "../../rest/rest.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotGuildPermissions } from "../../util/permissions.ts"; +import {Bot} from "../../bot.ts"; /** Delete a guild role. Requires the MANAGE_ROLES permission. */ -export async function deleteRole(guildId: bigint, id: bigint) { - await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]); +export async function deleteRole(bot: Bot, guildId: bigint, id: bigint) { + await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_ROLES"]); - return await rest.runMethod("delete", endpoints.GUILD_ROLE(guildId, id)); + return await bot.rest.runMethod(bot.rest,"delete", bot.constants.endpoints.GUILD_ROLE(guildId, id)); } diff --git a/src/helpers/roles/edit_role.ts b/src/helpers/roles/edit_role.ts index 35ef00dbf..60a574b27 100644 --- a/src/helpers/roles/edit_role.ts +++ b/src/helpers/roles/edit_role.ts @@ -1,18 +1,19 @@ -import { rest } from "../../rest/rest.ts"; -import { structures } from "../../structures/mod.ts"; import type { CreateGuildRole } from "../../types/guilds/create_guild_role.ts"; import type { Role } from "../../types/permissions/role.ts"; -import { endpoints } from "../../util/constants.ts"; -import { calculateBits, requireBotGuildPermissions } from "../../util/permissions.ts"; +import {Bot} from "../../bot.ts"; +import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Edit a guild role. Requires the MANAGE_ROLES permission. */ -export async function editRole(guildId: bigint, id: bigint, options: CreateGuildRole) { - await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]); +export async function editRole(bot: Bot, guildId: bigint, id: bigint, options: CreateGuildRole) { + await bot.utils.requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]); - const result = await rest.runMethod("patch", endpoints.GUILD_ROLE(guildId, id), { - ...options, - permissions: options.permissions ? calculateBits(options.permissions) : undefined, + const result = await bot.rest.runMethod>(bot.rest,"patch", bot.constants.endpoints.GUILD_ROLE(guildId, id), { + name: options.name, + color: options.color, + hoist: options.hoist, + mentionable: options.mentionable, + permissions: options.permissions ? bot.utils.calculateBits(options.permissions) : undefined, }); - return await structures.createDiscordenoRole({ role: result, guildId }); + return bot.transformers.role(bot,{ role: result, guildId }); } diff --git a/src/helpers/roles/get_roles.ts b/src/helpers/roles/get_roles.ts index 5dc55535e..6a794325a 100644 --- a/src/helpers/roles/get_roles.ts +++ b/src/helpers/roles/get_roles.ts @@ -1,31 +1,25 @@ -import { cacheHandlers } from "../../cache.ts"; -import { rest } from "../../rest/rest.ts"; -import { structures } from "../../structures/mod.ts"; import type { Role } from "../../types/permissions/role.ts"; -import { Collection } from "../../util/collection.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotGuildPermissions } from "../../util/permissions.ts"; - +import {Bot} from "../../bot.ts"; +import {Collection} from "../../util/collection.ts"; +import {DiscordenoRole} from "../../transformers/role.ts"; /** Returns a list of role objects for the guild. * * ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your roles will be cached in your guild.** */ -export async function getRoles(guildId: bigint, addToCache = true) { - await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]); +export async function getRoles(bot: Bot, guildId: bigint, addToCache = true) { + await bot.utils.requireBotGuildPermissions(bot,guildId, ["MANAGE_ROLES"]); - const result = await rest.runMethod("get", endpoints.GUILD_ROLES(guildId)); + const result = await bot.rest.runMethod(bot.rest,"get", bot.constants.endpoints.GUILD_ROLES(guildId)); - const roleStructures = await Promise.all( - result.map(async (role) => await structures.createDiscordenoRole({ role, guildId })) - ); + const roleStructures = result.map((role: Role) => bot.transformers.role({ role, guildId })); - const roles = new Collection(roleStructures.map((role) => [role.id, role])); + const roles = new Collection(roleStructures.map((role: DiscordenoRole) => [role.id, role])); if (addToCache) { - const guild = await cacheHandlers.get("guilds", guildId); + const guild = await bot.cache.guilds.get(guildId); if (guild) { guild.roles = roles; - await cacheHandlers.set("guilds", guild.id, guild); + await bot.cache.guilds.set(guild.id, guild); } } diff --git a/src/helpers/roles/remove_role.ts b/src/helpers/roles/remove_role.ts index d97c78314..731d4002e 100644 --- a/src/helpers/roles/remove_role.ts +++ b/src/helpers/roles/remove_role.ts @@ -1,17 +1,13 @@ -import { botId } from "../../bot.ts"; -import { rest } from "../../rest/rest.ts"; -import { Errors } from "../../types/discordeno/errors.ts"; -import { endpoints } from "../../util/constants.ts"; -import { isHigherPosition, requireBotGuildPermissions } from "../../util/permissions.ts"; +import {Bot} from "../../bot.ts"; /** Remove a role from the member */ -export async function removeRole(guildId: bigint, memberId: bigint, roleId: bigint, reason?: string) { - const isHigherRolePosition = await isHigherPosition(guildId, botId, roleId); +export async function removeRole(bot: Bot, guildId: bigint, memberId: bigint, roleId: bigint, reason?: string) { + const isHigherRolePosition = await bot.utils.isHigherPosition(bot,guildId, bot.id, roleId); if (!isHigherRolePosition) { - throw new Error(Errors.BOTS_HIGHEST_ROLE_TOO_LOW); + throw new Error(bot.constants.Errors.BOTS_HIGHEST_ROLE_TOO_LOW); } - await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]); + await bot.utils.requireBotGuildPermissions(bot,guildId, ["MANAGE_ROLES"]); - return await rest.runMethod("delete", endpoints.GUILD_MEMBER_ROLE(guildId, memberId, roleId), { reason }); + return await bot.rest.runMethod(bot.rest,"delete", bot.constants.endpoints.GUILD_MEMBER_ROLE(guildId, memberId, roleId), { reason }); } From e6da05837be918d999ed11010667102ff0183966 Mon Sep 17 00:00:00 2001 From: TriForMine Date: Mon, 18 Oct 2021 21:48:25 +0200 Subject: [PATCH 17/34] Templates + Fixes --- src/helpers/integrations/get_integrations.ts | 2 +- src/helpers/messages/publish_message.ts | 2 +- src/helpers/messages/remove_all_reactions.ts | 2 +- src/helpers/messages/remove_reaction.ts | 2 +- src/helpers/messages/remove_reaction_emoji.ts | 2 +- src/helpers/messages/unpin_message.ts | 2 +- src/helpers/roles/add_role.ts | 2 +- src/helpers/roles/delete_role.ts | 2 +- src/helpers/roles/edit_role.ts | 2 +- src/helpers/roles/get_roles.ts | 2 +- src/helpers/roles/remove_role.ts | 2 +- .../templates/create_guild_from_template.ts | 19 +++++------- .../templates/create_guild_template.ts | 30 +++++++++++++------ .../templates/delete_guild_template.ts | 10 +++---- src/helpers/templates/edit_guild_template.ts | 13 ++++---- src/helpers/templates/get_guild_templates.ts | 10 +++---- src/helpers/templates/get_template.ts | 7 ++--- src/helpers/templates/sync_guild_template.ts | 10 +++---- 18 files changed, 61 insertions(+), 60 deletions(-) diff --git a/src/helpers/integrations/get_integrations.ts b/src/helpers/integrations/get_integrations.ts index 2944af268..d14ce7bd3 100644 --- a/src/helpers/integrations/get_integrations.ts +++ b/src/helpers/integrations/get_integrations.ts @@ -1,5 +1,5 @@ import type { Integration } from "../../types/integrations/integration.ts"; -import {Bot} from "../../bot.ts"; +import type {Bot} from "../../bot.ts"; import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Returns a list of integrations for the guild. Requires the MANAGE_GUILD permission. */ diff --git a/src/helpers/messages/publish_message.ts b/src/helpers/messages/publish_message.ts index 0e0a62e03..752a526db 100644 --- a/src/helpers/messages/publish_message.ts +++ b/src/helpers/messages/publish_message.ts @@ -1,5 +1,5 @@ import type { Message } from "../../types/messages/message.ts"; -import {Bot} from "../../bot.ts"; +import type {Bot} from "../../bot.ts"; import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Crosspost a message in a News Channel to following channels. */ diff --git a/src/helpers/messages/remove_all_reactions.ts b/src/helpers/messages/remove_all_reactions.ts index db45bac29..e8d2bd3c9 100644 --- a/src/helpers/messages/remove_all_reactions.ts +++ b/src/helpers/messages/remove_all_reactions.ts @@ -1,4 +1,4 @@ -import {Bot} from "../../bot.ts"; +import type {Bot} from "../../bot.ts"; /** Removes all reactions for all emojis on this message. */ export async function removeAllReactions(bot: Bot, channelId: bigint, messageId: bigint) { diff --git a/src/helpers/messages/remove_reaction.ts b/src/helpers/messages/remove_reaction.ts index 3eb7e5bb2..7dc0f5c69 100644 --- a/src/helpers/messages/remove_reaction.ts +++ b/src/helpers/messages/remove_reaction.ts @@ -1,4 +1,4 @@ -import {Bot} from "../../bot.ts"; +import type {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( diff --git a/src/helpers/messages/remove_reaction_emoji.ts b/src/helpers/messages/remove_reaction_emoji.ts index f9bac3de7..4f23f577e 100644 --- a/src/helpers/messages/remove_reaction_emoji.ts +++ b/src/helpers/messages/remove_reaction_emoji.ts @@ -1,5 +1,5 @@ /** Removes all reactions for a single emoji on this message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */ -import {Bot} from "../../bot.ts"; +import type {Bot} from "../../bot.ts"; export async function removeReactionEmoji(bot: Bot, channelId: bigint, messageId: bigint, reaction: string) { diff --git a/src/helpers/messages/unpin_message.ts b/src/helpers/messages/unpin_message.ts index b8a6e01e7..17a733a2d 100644 --- a/src/helpers/messages/unpin_message.ts +++ b/src/helpers/messages/unpin_message.ts @@ -1,5 +1,5 @@ /** Unpin a message in a channel. Requires MANAGE_MESSAGES. */ -import {Bot} from "../../bot.ts"; +import type {Bot} from "../../bot.ts"; export async function unpin(bot: Bot, channelId: bigint, messageId: bigint): Promise { await bot.utils.requireBotChannelPermissions(bot, channelId, ["MANAGE_MESSAGES"]); diff --git a/src/helpers/roles/add_role.ts b/src/helpers/roles/add_role.ts index f4a224f98..50dee4558 100644 --- a/src/helpers/roles/add_role.ts +++ b/src/helpers/roles/add_role.ts @@ -1,4 +1,4 @@ -import {Bot} from "../../bot.ts"; +import type {Bot} from "../../bot.ts"; /** Add a role to the member */ export async function addRole(bot: Bot, guildId: bigint, memberId: bigint, roleId: bigint, reason?: string) { diff --git a/src/helpers/roles/delete_role.ts b/src/helpers/roles/delete_role.ts index d4f082632..f5f9b77ea 100644 --- a/src/helpers/roles/delete_role.ts +++ b/src/helpers/roles/delete_role.ts @@ -1,4 +1,4 @@ -import {Bot} from "../../bot.ts"; +import type {Bot} from "../../bot.ts"; /** Delete a guild role. Requires the MANAGE_ROLES permission. */ export async function deleteRole(bot: Bot, guildId: bigint, id: bigint) { diff --git a/src/helpers/roles/edit_role.ts b/src/helpers/roles/edit_role.ts index 60a574b27..6f5fce6e9 100644 --- a/src/helpers/roles/edit_role.ts +++ b/src/helpers/roles/edit_role.ts @@ -1,6 +1,6 @@ import type { CreateGuildRole } from "../../types/guilds/create_guild_role.ts"; import type { Role } from "../../types/permissions/role.ts"; -import {Bot} from "../../bot.ts"; +import type {Bot} from "../../bot.ts"; import {SnakeCasedPropertiesDeep} from "../../types/util.ts"; /** Edit a guild role. Requires the MANAGE_ROLES permission. */ diff --git a/src/helpers/roles/get_roles.ts b/src/helpers/roles/get_roles.ts index 6a794325a..871ac99e8 100644 --- a/src/helpers/roles/get_roles.ts +++ b/src/helpers/roles/get_roles.ts @@ -1,5 +1,5 @@ import type { Role } from "../../types/permissions/role.ts"; -import {Bot} from "../../bot.ts"; +import type {Bot} from "../../bot.ts"; import {Collection} from "../../util/collection.ts"; import {DiscordenoRole} from "../../transformers/role.ts"; /** Returns a list of role objects for the guild. diff --git a/src/helpers/roles/remove_role.ts b/src/helpers/roles/remove_role.ts index 731d4002e..372db80ba 100644 --- a/src/helpers/roles/remove_role.ts +++ b/src/helpers/roles/remove_role.ts @@ -1,4 +1,4 @@ -import {Bot} from "../../bot.ts"; +import type {Bot} from "../../bot.ts"; /** Remove a role from the member */ export async function removeRole(bot: Bot, guildId: bigint, memberId: bigint, roleId: bigint, reason?: string) { diff --git a/src/helpers/templates/create_guild_from_template.ts b/src/helpers/templates/create_guild_from_template.ts index f3ef0d468..d0ccca323 100644 --- a/src/helpers/templates/create_guild_from_template.ts +++ b/src/helpers/templates/create_guild_from_template.ts @@ -1,29 +1,24 @@ -import { cacheHandlers } from "../../cache.ts"; -import { rest } from "../../rest/rest.ts"; -import { structures } from "../../structures/mod.ts"; import type { Guild } from "../../types/guilds/guild.ts"; import type { CreateGuildFromTemplate } from "../../types/templates/create_guild_from_template.ts"; -import { endpoints } from "../../util/constants.ts"; -import { urlToBase64 } from "../../util/utils.ts"; -import { ws } from "../../ws/ws.ts"; +import type {Bot} from "../../bot.ts"; /** * Create a new guild based on a template * NOTE: This endpoint can be used only by bots in less than 10 guilds. */ -export async function createGuildFromTemplate(templateCode: string, data: CreateGuildFromTemplate) { - if ((await cacheHandlers.size("guilds")) >= 10) { +export async function createGuildFromTemplate(bot: Bot, templateCode: string, data: CreateGuildFromTemplate) { + if ((await bot.cache.guilds.size()) >= 10) { throw new Error("This function can only be used by bots in less than 10 guilds."); } if (data.icon) { - data.icon = await urlToBase64(data.icon); + data.icon = await bot.utils.urlToBase64(data.icon); } - const createdGuild = await rest.runMethod("post", endpoints.GUILD_TEMPLATE(templateCode), data); + const createdGuild = await bot.rest.runMethod(bot.rest,"post", bot.constants.endpoints.GUILD_TEMPLATE(templateCode), data); - return await structures.createDiscordenoGuild( + return bot.transformers.guild( createdGuild, - Number((BigInt(createdGuild.id) >> 22n % BigInt(ws.botGatewayData.shards)).toString()) + Number((BigInt(createdGuild.id) >> 22n % BigInt(bot.ws.botGatewayData.shards)).toString()) ); } diff --git a/src/helpers/templates/create_guild_template.ts b/src/helpers/templates/create_guild_template.ts index bc09064b8..56d88881a 100644 --- a/src/helpers/templates/create_guild_template.ts +++ b/src/helpers/templates/create_guild_template.ts @@ -1,17 +1,17 @@ -import { rest } from "../../rest/rest.ts"; import type { Template } from "../../types/templates/template.ts"; -import { endpoints } from "../../util/constants.ts"; -import { requireBotGuildPermissions } from "../../util/permissions.ts"; -import { snakelize } from "../../util/utils.ts"; +import type {Bot} from "../../bot.ts"; +import {User} from "../../types/users/user.ts"; +import {Guild} from "../../types/guilds/guild.ts"; /** * Creates a template for the guild. * Requires the `MANAGE_GUILD` permission. - * @param name name of the template (1-100 characters) - * @param description description for the template (0-120 characters + * @param bot + * @param guildId + * @param data */ -export async function createGuildTemplate(guildId: bigint, data: Template) { - await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]); +export async function createGuildTemplate(bot: Bot, guildId: bigint, data: Template) { + await bot.utils.requireBotGuildPermissions(bot, guildId, ["MANAGE_GUILD"]); if (data.name.length < 1 || data.name.length > 100) { throw new Error("The name can only be in between 1-100 characters."); @@ -21,5 +21,17 @@ export async function createGuildTemplate(guildId: bigint, data: Template) { throw new Error("The description can only be in between 0-120 characters."); } - return await rest.runMethod