diff --git a/src/bot.ts b/src/bot.ts index b531b1334..132d2d41b 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -1,7 +1,8 @@ import { getGatewayBot } from "./helpers/misc/get_gateway_bot.ts"; import { rest } from "./rest/rest.ts"; +import { EventHandlers } from "./types/discordeno/eventHandlers.ts"; import { DiscordGatewayIntents } from "./types/gateway/gateway_intents.ts"; -import { DiscordGetGatewayBot } from "./types/gateway/get_gateway_bot.ts"; +import { GetGatewayBot } from "./types/gateway/get_gateway_bot.ts"; import { baseEndpoints, GATEWAY_VERSION } from "./util/constants.ts"; import { ws } from "./ws/ws.ts"; @@ -12,7 +13,7 @@ export let applicationId = ""; export let eventHandlers: EventHandlers = {}; -export let botGatewayData: DiscordGetGatewayBot; +export let botGatewayData: GetGatewayBot; export let proxyWSURL = `wss://gateway.discord.gg`; export const identifyPayload = { diff --git a/src/handlers/messages/MESSAGE_REACTION_REMOVE_ALL.ts b/src/handlers/messages/MESSAGE_REACTION_REMOVE_ALL.ts index 29ff5b3ed..80c70e9a5 100644 --- a/src/handlers/messages/MESSAGE_REACTION_REMOVE_ALL.ts +++ b/src/handlers/messages/MESSAGE_REACTION_REMOVE_ALL.ts @@ -15,5 +15,5 @@ export async function handleMessageReactionRemoveAll( await cacheHandlers.set("messages", payload.message_id, message); } - eventHandlers.reactionRemoveAll?.(data.d); + eventHandlers.reactionRemoveAll?.(payload); } diff --git a/src/handlers/misc/USER_UPDATE.ts b/src/handlers/misc/USER_UPDATE.ts index ab7ee0f8b..76d2eade2 100644 --- a/src/handlers/misc/USER_UPDATE.ts +++ b/src/handlers/misc/USER_UPDATE.ts @@ -1,10 +1,11 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; -import { DiscordUser } from "../../types/users/user.ts"; +import { DiscordUser, User } from "../../types/users/user.ts"; +import { camelKeysToSnakeCase } from "../../util/utils.ts"; export async function handleUserUpdate(data: DiscordGatewayPayload) { - const userData = data.d as DiscordUser; + const userData = camelKeysToSnakeCase(data.d as DiscordUser) as User; const member = await cacheHandlers.get("members", userData.id); if (!member) return; diff --git a/src/handlers/voice/VOICE_SERVER_UPDATE.ts b/src/handlers/voice/VOICE_SERVER_UPDATE.ts index 24e738b65..066456d78 100644 --- a/src/handlers/voice/VOICE_SERVER_UPDATE.ts +++ b/src/handlers/voice/VOICE_SERVER_UPDATE.ts @@ -1,11 +1,14 @@ import { eventHandlers } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; +import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; +import { DiscordVoiceServerUpdate, VoiceServerUpdate } from "../../types/voice/voice_server_update.ts"; +import { snakeKeysToCamelCase } from "../../util/utils.ts"; -export async function handleVoiceServerUpdate(data: DiscordPayload) { - const payload = data.d as DiscordVoiceServerUpdate; +export async function handleVoiceServerUpdate(data: DiscordGatewayPayload) { + const payload = snakeKeysToCamelCase(data.d as DiscordVoiceServerUpdate) as VoiceServerUpdate; - const guild = await cacheHandlers.get("guilds", payload.guild_id); + const guild = await cacheHandlers.get("guilds", payload.guildId); if (!guild) return; - eventHandlers.voiceServerUpdate?.(payload.token, guild, payload.endpoint); + eventHandlers.voiceServerUpdate?.(payload, guild); } diff --git a/src/handlers/webhooks/WEBHOOKS_UPDATE.ts b/src/handlers/webhooks/WEBHOOKS_UPDATE.ts index 86e10a68e..1f53d23cf 100644 --- a/src/handlers/webhooks/WEBHOOKS_UPDATE.ts +++ b/src/handlers/webhooks/WEBHOOKS_UPDATE.ts @@ -1,9 +1,9 @@ import { eventHandlers } from "../../bot.ts"; import { DiscordGatewayPayload } from "../../types/gateway/gateway_payload.ts"; -import { DiscordWebhooksUpdate } from "../../types/webhooks/webhooks_update.ts"; +import { DiscordWebhookUpdate } from "../../types/webhooks/webhooks_update.ts"; export function handleWebhooksUpdate(data: DiscordGatewayPayload) { - const options = data.d as DiscordWebhooksUpdate; + const options = data.d as DiscordWebhookUpdate; eventHandlers.webhooksUpdate?.( options.channel_id, options.guild_id, diff --git a/src/helpers/members/edit_member.ts b/src/helpers/members/edit_member.ts index 5e91ada36..a6a093892 100644 --- a/src/helpers/members/edit_member.ts +++ b/src/helpers/members/edit_member.ts @@ -1,6 +1,7 @@ import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; +import { DiscordGuildMember } from "../../types/guilds/guild_member.ts"; import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { @@ -69,7 +70,7 @@ export async function editMember( "patch", endpoints.GUILD_MEMBER(guildId, memberId), options, - ) as MemberCreatePayload; + ) as DiscordGuildMember; const member = await structures.createDiscordenoMember(result, guildId); return member; diff --git a/src/helpers/members/get_member.ts b/src/helpers/members/get_member.ts index bb98ec7cb..406337aa8 100644 --- a/src/helpers/members/get_member.ts +++ b/src/helpers/members/get_member.ts @@ -1,6 +1,7 @@ import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; +import { DiscordGuildMember } from "../../types/guilds/guild_member.ts"; import { endpoints } from "../../util/constants.ts"; /** Returns a guild member object for the specified user. @@ -18,7 +19,7 @@ export async function getMember( const data = (await rest.runMethod( "get", endpoints.GUILD_MEMBER(guildId, id), - )) as MemberCreatePayload; + )) as DiscordGuildMember; const discordenoMember = await structures.createDiscordenoMember( data, diff --git a/src/helpers/members/get_members_by_query.ts b/src/helpers/members/get_members_by_query.ts index 0750ca694..5020f5e26 100644 --- a/src/helpers/members/get_members_by_query.ts +++ b/src/helpers/members/get_members_by_query.ts @@ -1,5 +1,5 @@ import { cacheHandlers } from "../../cache.ts"; -import { Member } from "../../structures/mod.ts"; +import { DiscordenoMember } from "../../structures/member.ts"; import { Collection } from "../../util/collection.ts"; /** Returns guild member objects for the specified user by their nickname/username. @@ -19,5 +19,5 @@ export async function getMembersByQuery( query: name, limit, }); - }) as Promise>; + }) as Promise>; } diff --git a/src/helpers/messages/edit_message.ts b/src/helpers/messages/edit_message.ts index c22e96695..51848ff6f 100644 --- a/src/helpers/messages/edit_message.ts +++ b/src/helpers/messages/edit_message.ts @@ -9,7 +9,7 @@ import { requireBotChannelPermissions } from "../../util/permissions.ts"; /** Edit the message. */ export async function editMessage( message: Message, - content: string | MessageContent, + content: string | MessageContent ) { if (message.author.id !== botId) { throw "You can only edit a message that was sent by the bot."; @@ -30,8 +30,8 @@ export async function editMessage( const result = await rest.runMethod( "patch", endpoints.CHANNEL_MESSAGE(message.channelId, message.id), - content, + content ); - return structures.createDiscordenoMessage(result as MessageCreateOptions); + return structures.createDiscordenoMessage(result as DiscordMessage); } diff --git a/src/helpers/messages/get_message.ts b/src/helpers/messages/get_message.ts index cbb240739..5b660b0cd 100644 --- a/src/helpers/messages/get_message.ts +++ b/src/helpers/messages/get_message.ts @@ -1,6 +1,7 @@ import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; +import { DiscordMessage } from "../../types/messages/message.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts"; @@ -15,8 +16,8 @@ export async function getMessage(channelId: string, id: string) { const result = (await rest.runMethod( "get", - endpoints.CHANNEL_MESSAGE(channelId, id), - )) as MessageCreateOptions; + endpoints.CHANNEL_MESSAGE(channelId, id) + )) as DiscordMessage; return structures.createDiscordenoMessage(result); } diff --git a/src/helpers/messages/get_messages.ts b/src/helpers/messages/get_messages.ts index 6931676bf..f5e3c411d 100644 --- a/src/helpers/messages/get_messages.ts +++ b/src/helpers/messages/get_messages.ts @@ -1,5 +1,6 @@ import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; +import { DiscordMessage } from "../../types/messages/message.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts"; @@ -10,7 +11,7 @@ export async function getMessages( | GetMessagesAfter | GetMessagesBefore | GetMessagesAround - | GetMessages, + | GetMessages ) { await requireBotChannelPermissions(channelId, [ "VIEW_CHANNEL", @@ -22,10 +23,10 @@ export async function getMessages( const result = (await rest.runMethod( "get", endpoints.CHANNEL_MESSAGES(channelId), - options, - )) as MessageCreateOptions[]; + options + )) as DiscordMessage[]; return Promise.all( - result.map((res) => structures.createDiscordenoMessage(res)), + result.map((res) => structures.createDiscordenoMessage(res)) ); } diff --git a/src/helpers/messages/publish_message.ts b/src/helpers/messages/publish_message.ts index 61f8c41e6..4359954e2 100644 --- a/src/helpers/messages/publish_message.ts +++ b/src/helpers/messages/publish_message.ts @@ -1,13 +1,14 @@ import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; +import { DiscordMessage } from "../../types/messages/message.ts"; import { endpoints } from "../../util/constants.ts"; /** Crosspost a message in a News Channel to following channels. */ export async function publishMessage(channelId: string, messageId: string) { const data = (await rest.runMethod( "post", - endpoints.CHANNEL_MESSAGE_CROSSPOST(channelId, messageId), - )) as MessageCreateOptions; + endpoints.CHANNEL_MESSAGE_CROSSPOST(channelId, messageId) + )) as DiscordMessage; return structures.createDiscordenoMessage(data); } diff --git a/src/helpers/messages/send_message.ts b/src/helpers/messages/send_message.ts index 901416ced..9b0f4274d 100644 --- a/src/helpers/messages/send_message.ts +++ b/src/helpers/messages/send_message.ts @@ -3,6 +3,7 @@ import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; import { DiscordChannelTypes } from "../../types/channels/channel_types.ts"; import { DiscordAllowedMentionsTypes } from "../../types/messages/allowed_mentions_types.ts"; +import { CreateMessage } from "../../types/messages/create_message.ts"; import { DiscordMessage } from "../../types/messages/message.ts"; import { Errors } from "../../types/misc/errors.ts"; import { PermissionStrings } from "../../types/permissions/permission_strings.ts"; @@ -13,7 +14,7 @@ import { camelKeysToSnakeCase } from "../../util/utils.ts"; /** Send a message to the channel. Requires SEND_MESSAGES permission. */ export async function sendMessage( channelId: string, - content: string | DiscordenoCreateMessage, + content: string | CreateMessage ) { if (typeof content === "string") content = { content }; @@ -55,18 +56,18 @@ export async function sendMessage( if (content.allowedMentions.users?.length) { if ( content.allowedMentions.parse?.includes( - DiscordAllowedMentionsTypes.UserMentions, + DiscordAllowedMentionsTypes.UserMentions ) ) { content.allowedMentions.parse = content.allowedMentions.parse.filter( - (p) => p !== "users", + (p) => p !== "users" ); } if (content.allowedMentions.users.length > 100) { content.allowedMentions.users = content.allowedMentions.users.slice( 0, - 100, + 100 ); } } @@ -74,18 +75,18 @@ export async function sendMessage( if (content.allowedMentions.roles?.length) { if ( content.allowedMentions.parse?.includes( - DiscordAllowedMentionsTypes.RoleMentions, + DiscordAllowedMentionsTypes.RoleMentions ) ) { content.allowedMentions.parse = content.allowedMentions.parse.filter( - (p) => p !== "roles", + (p) => p !== "roles" ); } if (content.allowedMentions.roles.length > 100) { content.allowedMentions.roles = content.allowedMentions.roles.slice( 0, - 100, + 100 ); } } @@ -98,13 +99,14 @@ export async function sendMessage( ...content, ...(content.messageReference?.messageId ? { - messageReference: { - ...content.messageReference, - failIfNotExists: content.messageReference.failIfNotExists === true, - }, - } + messageReference: { + ...content.messageReference, + failIfNotExists: + content.messageReference.failIfNotExists === true, + }, + } : {}), - }), + }) )) as DiscordMessage; return structures.createDiscordenoMessage(result); diff --git a/src/helpers/misc/get_gateway_bot.ts b/src/helpers/misc/get_gateway_bot.ts index 1e1c94935..46e0cdc7b 100644 --- a/src/helpers/misc/get_gateway_bot.ts +++ b/src/helpers/misc/get_gateway_bot.ts @@ -1,9 +1,11 @@ import { rest } from "../../rest/rest.ts"; +import { DiscordGetGatewayBot, GetGatewayBot } from "../../types/gateway/get_gateway_bot.ts"; import { endpoints } from "../../util/constants.ts"; +import { camelKeysToSnakeCase } from "../../util/utils.ts"; /** Get the bots Gateway metadata that can help during the operation of large or sharded bots. */ export async function getGatewayBot() { const result = await rest.runMethod("get", endpoints.GATEWAY_BOT); - return result as DiscordBotGatewayData; + return camelKeysToSnakeCase(result as DiscordGetGatewayBot) as GetGatewayBot; } diff --git a/src/helpers/templates/create_guild_template.ts b/src/helpers/templates/create_guild_template.ts index 54a6936e9..aafd4970d 100644 --- a/src/helpers/templates/create_guild_template.ts +++ b/src/helpers/templates/create_guild_template.ts @@ -1,5 +1,6 @@ import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; +import { DiscordTemplate } from "../../types/templates/template.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts"; @@ -27,7 +28,7 @@ export async function createGuildTemplate( "post", endpoints.GUILD_TEMPLATES(guildId), data, - )) as GuildTemplate; + )) as DiscordTemplate; return structures.createTemplateStruct(template); } diff --git a/src/helpers/templates/delete_guild_template.ts b/src/helpers/templates/delete_guild_template.ts index 26fd9ad7f..171350d60 100644 --- a/src/helpers/templates/delete_guild_template.ts +++ b/src/helpers/templates/delete_guild_template.ts @@ -1,5 +1,6 @@ import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; +import { DiscordTemplate } from "../../types/templates/template.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts"; @@ -16,7 +17,7 @@ export async function deleteGuildTemplate( const deletedTemplate = (await rest.runMethod( "delete", `${endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`, - )) as GuildTemplate; + )) as DiscordTemplate; return structures.createTemplateStruct(deletedTemplate); } diff --git a/src/helpers/templates/edit_guild_template.ts b/src/helpers/templates/edit_guild_template.ts index 72c1934c6..7ff88fabe 100644 --- a/src/helpers/templates/edit_guild_template.ts +++ b/src/helpers/templates/edit_guild_template.ts @@ -1,5 +1,6 @@ import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; +import { DiscordTemplate } from "../../types/templates/template.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts"; @@ -26,7 +27,7 @@ export async function editGuildTemplate( "patch", `${endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`, data, - )) as GuildTemplate; + )) as DiscordTemplate; return structures.createTemplateStruct(template); } diff --git a/src/helpers/templates/get_guild_templates.ts b/src/helpers/templates/get_guild_templates.ts index 02290900e..7972ccf63 100644 --- a/src/helpers/templates/get_guild_templates.ts +++ b/src/helpers/templates/get_guild_templates.ts @@ -2,6 +2,7 @@ import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts"; +import { DiscordTemplate } from "../../types/templates/template.ts"; /** * Returns an array of templates. @@ -12,8 +13,8 @@ export async function getGuildTemplates(guildId: string) { const templates = (await rest.runMethod( "get", - endpoints.GUILD_TEMPLATES(guildId), - )) as GuildTemplate[]; + endpoints.GUILD_TEMPLATES(guildId) + )) as DiscordTemplate[]; return templates.map((template) => structures.createTemplateStruct(template)); } diff --git a/src/helpers/templates/get_template.ts b/src/helpers/templates/get_template.ts index 9c209123b..1c41af8a1 100644 --- a/src/helpers/templates/get_template.ts +++ b/src/helpers/templates/get_template.ts @@ -1,13 +1,14 @@ import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; import { endpoints } from "../../util/constants.ts"; +import { DiscordTemplate } from "../../types/templates/template.ts"; /** Returns the guild template if it exists */ export async function getTemplate(templateCode: string) { const result = (await rest.runMethod( "get", endpoints.GUILD_TEMPLATE(templateCode), - ) as GuildTemplate); + ) as DiscordTemplate); const template = await structures.createTemplateStruct(result); return template; diff --git a/src/helpers/templates/sync_guild_template.ts b/src/helpers/templates/sync_guild_template.ts index e657bb905..a0c84495e 100644 --- a/src/helpers/templates/sync_guild_template.ts +++ b/src/helpers/templates/sync_guild_template.ts @@ -2,6 +2,7 @@ import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts"; +import { DiscordTemplate } from "../../types/templates/template.ts"; /** * Syncs the template to the guild's current state. @@ -13,7 +14,7 @@ export async function syncGuildTemplate(guildId: string, templateCode: string) { const template = (await rest.runMethod( "put", `${endpoints.GUILD_TEMPLATES(guildId)}/${templateCode}`, - )) as GuildTemplate; + )) as DiscordTemplate; return structures.createTemplateStruct(template); } diff --git a/src/helpers/webhooks/edit_webhook_message.ts b/src/helpers/webhooks/edit_webhook_message.ts index 63ccd3024..1d3d99d39 100644 --- a/src/helpers/webhooks/edit_webhook_message.ts +++ b/src/helpers/webhooks/edit_webhook_message.ts @@ -1,13 +1,16 @@ import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; +import { DiscordAllowedMentionsTypes } from "../../types/messages/allowed_mentions_types.ts"; +import { DiscordMessage } from "../../types/messages/message.ts"; import { Errors } from "../../types/misc/errors.ts"; +import { EditWebhookMessage } from "../../types/webhooks/edit_webhook_message.ts"; import { endpoints } from "../../util/constants.ts"; export async function editWebhookMessage( webhookId: string, webhookToken: string, messageId: string, - options: EditWebhookMessageOptions, + options: EditWebhookMessage ) { if (options.content && options.content.length > 2000) { throw Error(Errors.MESSAGE_MAX_LENGTH); @@ -17,43 +20,43 @@ export async function editWebhookMessage( options.embeds.splice(10); } - if (options.allowed_mentions) { - if (options.allowed_mentions.users?.length) { - if (options.allowed_mentions.parse.includes("users")) { - options.allowed_mentions.parse = options.allowed_mentions.parse.filter( - (p) => p !== "users", + if (options.allowedMentions) { + if (options.allowedMentions.users?.length) { + if (options.allowedMentions.parse.includes("users")) { + options.allowedMentions.parse = options.allowedMentions.parse.filter( + (p) => p !== "users" ); } - if (options.allowed_mentions.users.length > 100) { - options.allowed_mentions.users = options.allowed_mentions.users.slice( + if (options.allowedMentions.users.length > 100) { + options.allowedMentions.users = options.allowedMentions.users.slice( 0, - 100, + 100 ); } } - if (options.allowed_mentions.roles?.length) { - if (options.allowed_mentions.parse.includes("roles")) { - options.allowed_mentions.parse = options.allowed_mentions.parse.filter( - (p) => p !== "roles", + if (options.allowedMentions.roles?.length) { + if (options.allowedMentions.parse.includes(DiscordAllowedMentionsTypes.RoleMentions)) { + options.allowedMentions.parse = options.allowedMentions.parse.filter( + (p) => p !== "roles" ); } - if (options.allowed_mentions.roles.length > 100) { - options.allowed_mentions.roles = options.allowed_mentions.roles.slice( + if (options.allowedMentions.roles.length > 100) { + options.allowedMentions.roles = options.allowedMentions.roles.slice( 0, - 100, + 100 ); } } } - const result = await rest.runMethod( + const result = (await rest.runMethod( "patch", endpoints.WEBHOOK_MESSAGE(webhookId, webhookToken, messageId), - { ...options, allowed_mentions: options.allowed_mentions }, - ) as MessageCreateOptions; + { ...options, allowedMentions: options.allowedMentions } + )) as DiscordMessage; const message = await structures.createDiscordenoMessage(result); return message; diff --git a/src/helpers/webhooks/execute_webhook.ts b/src/helpers/webhooks/execute_webhook.ts index f71de9405..a26e14f4e 100644 --- a/src/helpers/webhooks/execute_webhook.ts +++ b/src/helpers/webhooks/execute_webhook.ts @@ -1,13 +1,16 @@ import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; +import { DiscordAllowedMentionsTypes } from "../../types/messages/allowed_mentions_types.ts"; +import { DiscordMessage } from "../../types/messages/message.ts"; import { Errors } from "../../types/misc/errors.ts"; +import { ExecuteWebhook } from "../../types/webhooks/execute_webhook.ts"; import { endpoints } from "../../util/constants.ts"; /** Execute a webhook with webhook Id and webhook token */ export async function executeWebhook( webhookId: string, webhookToken: string, - options: ExecuteWebhookOptions, + options: ExecuteWebhook ) { if (!options.content && !options.file && !options.embeds) { throw new Error(Errors.INVALID_WEBHOOK_OPTIONS); @@ -21,28 +24,42 @@ export async function executeWebhook( options.embeds.splice(10); } - if (options.mentions) { - if (options.mentions.users?.length) { - if (options.mentions.parse.includes("users")) { - options.mentions.parse = options.mentions.parse.filter( - (p) => p !== "users", + if (options.allowedMentions) { + if (options.allowedMentions.users?.length) { + if ( + options.allowedMentions.parse.includes( + DiscordAllowedMentionsTypes.UserMentions + ) + ) { + options.allowedMentions.parse = options.allowedMentions.parse.filter( + (p) => p !== "users" ); } - if (options.mentions.users.length > 100) { - options.mentions.users = options.mentions.users.slice(0, 100); + if (options.allowedMentions.users.length > 100) { + options.allowedMentions.users = options.allowedMentions.users.slice( + 0, + 100 + ); } } - if (options.mentions.roles?.length) { - if (options.mentions.parse.includes("roles")) { - options.mentions.parse = options.mentions.parse.filter( - (p) => p !== "roles", + if (options.allowedMentions.roles?.length) { + if ( + options.allowedMentions.parse.includes( + DiscordAllowedMentionsTypes.RoleMentions + ) + ) { + options.allowedMentions.parse = options.allowedMentions.parse.filter( + (p) => p !== "roles" ); } - if (options.mentions.roles.length > 100) { - options.mentions.roles = options.mentions.roles.slice(0, 100); + if (options.allowedMentions.roles.length > 100) { + options.allowedMentions.roles = options.allowedMentions.roles.slice( + 0, + 100 + ); } } } @@ -54,11 +71,15 @@ export async function executeWebhook( }`, { ...options, - allowed_mentions: options.mentions, - avatar_url: options.avatar_url, - }, + allowed_mentions: options.allowedMentions, + avatar_url: options.avatarUrl, + } ); if (!options.wait) return; - return structures.createDiscordenoMessage(result as MessageCreateOptions); + return structures.createDiscordenoMessage(result as DiscordMessage); +} + +function DiscordAllowedMentionTypes(DiscordAllowedMentionTypes: any) { + throw new Error("Function not implemented."); } diff --git a/src/rest/rest.ts b/src/rest/rest.ts index a680f447c..e18e45d3f 100644 --- a/src/rest/rest.ts +++ b/src/rest/rest.ts @@ -24,15 +24,15 @@ export const rest = { ratelimitedPaths: new Map(), eventHandlers: { // BY DEFAULT WE WILL LOG ALL ERRORS TO CONSOLE. USER CAN CHOOSE TO OVERRIDE - error: console.error, + error: function (...args: unknown[]) {}, // PLACEHOLDERS TO ALLOW USERS TO CUSTOMIZE - debug: function (_type, error, ...args) {}, - fetching() {}, - fetched() {}, - fetchSuccess() {}, - fetchFailed() {}, - globallyRateLimited() {}, - retriesMaxed() {}, + debug: function (type: string, error: string | Record) {}, + fetching(payload: Record) {}, + fetched(payload: Record) {}, + fetchSuccess(payload: Record) {}, + fetchFailed(payload: Record, error: any) {}, + globallyRateLimited(url: string, resetsAt: number) {}, + retriesMaxed(payload: Record) {}, }, /** Handler function for every request. Converts to json, verified authorization & requirements and begins processing the request */ handlePayload, diff --git a/src/structures/message.ts b/src/structures/message.ts index 7bd6e485b..feb586614 100644 --- a/src/structures/message.ts +++ b/src/structures/message.ts @@ -11,6 +11,7 @@ import { removeReaction } from "../helpers/messages/remove_reaction.ts"; import { removeReactionEmoji } from "../helpers/messages/remove_reaction_emoji.ts"; import { sendMessage } from "../helpers/messages/send_message.ts"; import { GuildMember } from "../types/guilds/guild_member.ts"; +import { CreateMessage } from "../types/messages/create_message.ts"; import { EditMessage } from "../types/messages/edit_message.ts"; import { DiscordMessage, Message } from "../types/messages/message.ts"; import { CHANNEL_MENTION_REGEX } from "../util/constants.ts"; @@ -38,8 +39,9 @@ const baseMessage: Partial = { return this.member?.guilds.get(this.guildId); }, get link() { - return `https://discord.com/channels/${this.guildId || - "@me"}/${this.channelId}/${this.id}`; + return `https://discord.com/channels/${this.guildId || "@me"}/${ + this.channelId + }/${this.id}`; }, get mentionedRoles() { return this.mentionedRoleIds?.map((id) => this.guild?.roles.get(id)) || []; @@ -68,20 +70,25 @@ const baseMessage: Partial = { return addReactions(this.channelId!, this.id!, reactions, ordered); }, reply(content) { - const contentWithMention = typeof content === "string" - ? { - content, - mentions: { repliedUser: true }, - messageReference: { messageId: this.id }, - failReplyIfNotExists: false, - } - : { - ...content, - mentions: { ...(content.allowedMentions || {}), repliedUser: true }, - messageReference: { messageId: this.id }, - failReplyIfNotExists: - content.messageReference?.failIfNotExists === true, - }; + const contentWithMention = + typeof content === "string" + ? { + content, + mentions: { repliedUser: true }, + messageReference: { + messageId: this.id, + failReplyIfNotExists: false, + }, + } + : { + ...content, + mentions: { ...(content.allowedMentions || {}), repliedUser: true }, + messageReference: { + messageId: this.id, + failReplyIfNotExists: + content.messageReference?.failIfNotExists === true, + }, + }; if (this.guildId) return sendMessage(this.channelId!, contentWithMention); return sendDirectMessage(this.author!.id, contentWithMention); @@ -124,7 +131,7 @@ export async function createDiscordenoMessage(data: DiscordMessage) { mentionChannels = [], mentions, mentionRoles, - edited_timestamp: editedTimestamp, + editedTimestamp, ...rest } = snakeKeysToCamelCase(data) as Message; @@ -132,15 +139,15 @@ export async function createDiscordenoMessage(data: DiscordMessage) { for (const key of Object.keys(rest)) { eventHandlers.debug?.( "loop", - `Running for of loop in createDiscordenoMessage function.`, + `Running for of loop in createDiscordenoMessage function.` ); // @ts-ignore index signature props[key] = createNewProp(rest[key]); } // Discord doesnt give guild id for getMessage() so this will fill it in - const guildIdFinal = guildId || - (await cacheHandlers.get("channels", channelId))?.guildId || ""; + const guildIdFinal = + guildId || (await cacheHandlers.get("channels", channelId))?.guildId || ""; const message: DiscordenoMessage = Object.create(baseMessage, { ...props, @@ -149,20 +156,18 @@ export async function createDiscordenoMessage(data: DiscordMessage) { guildId: createNewProp(guildIdFinal), mentionedUserIds: createNewProp(mentions.map((m) => m.id)), mentionedRoleIds: createNewProp(mentionRoles), - mentionedChannelIds: createNewProp( - [ - // Keep any ids that discord sends - ...mentionChannels.map((m) => m.id), - // Add any other ids that can be validated in a channel mention format - ...(rest.content.match(CHANNEL_MENTION_REGEX) || []).map((text) => - // converts the <#123> into 123 - text.substring(2, text.length - 1) - ), - ], - ), + mentionedChannelIds: createNewProp([ + // Keep any ids that discord sends + ...mentionChannels.map((m) => m.id), + // Add any other ids that can be validated in a channel mention format + ...(rest.content.match(CHANNEL_MENTION_REGEX) || []).map((text) => + // converts the <#123> into 123 + text.substring(2, text.length - 1) + ), + ]), timestamp: createNewProp(Date.parse(data.timestamp)), editedTimestamp: createNewProp( - editedTimestamp ? Date.parse(editedTimestamp) : undefined, + editedTimestamp ? Date.parse(editedTimestamp) : undefined ), }); @@ -204,7 +209,7 @@ export interface DiscordenoMessage extends Message { /** Delete the message */ delete( reason?: string, - delayMilliseconds?: number, + delayMilliseconds?: number ): ReturnType; /** Edit the message */ edit(content: string | EditMessage): ReturnType; @@ -215,27 +220,23 @@ export interface DiscordenoMessage extends Message { /** Add multiple reactions to the message without or without order. */ addReactions( reactions: string[], - ordered?: boolean, + ordered?: boolean ): ReturnType; /** Send a inline reply to this message */ - reply( - content: string | DiscordenoCreateMessage, - ): ReturnType; + reply(content: string | CreateMessage): ReturnType; /** Send a message to this channel where this message is */ - send( - content: string | DiscordenoCreateMessage, - ): ReturnType; + send(content: string | CreateMessage): ReturnType; /** Send a message to this channel and then delete it after a bit. By default it will delete after 10 seconds with no reason provided. */ alert( - content: string | DiscordenoCreateMessage, + content: string | CreateMessage, timeout?: number, - reason?: string, + reason?: string ): Promise; /** Send a inline reply to this message but then delete it after a bit. By default it will delete after 10 seconds with no reason provided. */ alertReply( - content: string | DiscordenoCreateMessage, + content: string | CreateMessage, timeout?: number, - reason?: string, + reason?: string ): Promise; /** Remove all reactions */ removeAllReactions(): ReturnType; diff --git a/src/structures/template.ts b/src/structures/template.ts index dc801de2f..b4930e80e 100644 --- a/src/structures/template.ts +++ b/src/structures/template.ts @@ -1,8 +1,10 @@ import { eventHandlers } from "../bot.ts"; import { cache } from "../cache.ts"; +import { DiscordTemplate, Template } from "../types/templates/template.ts"; import { createNewProp } from "../util/utils.ts"; +import { DiscordenoGuild } from "./guild.ts"; -const baseTemplate: Partial