diff --git a/src/helpers/channels/start_typing.ts b/src/helpers/channels/start_typing.ts index edcaa82a1..ca181dcbe 100644 --- a/src/helpers/channels/start_typing.ts +++ b/src/helpers/channels/start_typing.ts @@ -1,6 +1,7 @@ import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; -import { DiscordChannelTypes, Errors } from "../../types/mod.ts"; +import { DiscordChannelTypes } from "../../types/channels/channel_types.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { botHasChannelPermissions } from "../../util/permissions.ts"; diff --git a/src/helpers/commands/edit_slash_response.ts b/src/helpers/commands/edit_slash_response.ts index 94f82a003..fa81ac460 100644 --- a/src/helpers/commands/edit_slash_response.ts +++ b/src/helpers/commands/edit_slash_response.ts @@ -1,6 +1,7 @@ import { applicationId } from "../../bot.ts"; import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; /** To edit your response to a slash command. If a messageId is not provided it will default to editing the original response. */ diff --git a/src/helpers/guilds/get_prune_count.ts b/src/helpers/guilds/get_prune_count.ts index 3b16b6c9a..1d81987ba 100644 --- a/src/helpers/guilds/get_prune_count.ts +++ b/src/helpers/guilds/get_prune_count.ts @@ -1,4 +1,5 @@ import { rest } from "../../rest/rest.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts"; import { camelKeysToSnakeCase } from "../../util/utils.ts"; diff --git a/src/helpers/guilds/get_widget.ts b/src/helpers/guilds/get_widget.ts index 4583dcd98..f44197d37 100644 --- a/src/helpers/guilds/get_widget.ts +++ b/src/helpers/guilds/get_widget.ts @@ -1,5 +1,6 @@ import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; /** Returns the widget for the guild. */ diff --git a/src/helpers/guilds/get_widget_image_url.ts b/src/helpers/guilds/get_widget_image_url.ts index 10605f4d8..6e9eb01b8 100644 --- a/src/helpers/guilds/get_widget_image_url.ts +++ b/src/helpers/guilds/get_widget_image_url.ts @@ -1,4 +1,5 @@ import { cacheHandlers } from "../../cache.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; /** Returns the widget image URL for the guild. */ diff --git a/src/helpers/invites/delete_invite.ts b/src/helpers/invites/delete_invite.ts index de97f59c9..643cbdde9 100644 --- a/src/helpers/invites/delete_invite.ts +++ b/src/helpers/invites/delete_invite.ts @@ -1,5 +1,6 @@ import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { botHasChannelPermissions, diff --git a/src/helpers/members/edit_bot_profile.ts b/src/helpers/members/edit_bot_profile.ts index c010c9f67..062d0433f 100644 --- a/src/helpers/members/edit_bot_profile.ts +++ b/src/helpers/members/edit_bot_profile.ts @@ -1,4 +1,5 @@ import { rest } from "../../rest/rest.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { urlToBase64 } from "../../util/utils.ts"; diff --git a/src/helpers/members/edit_member.ts b/src/helpers/members/edit_member.ts index a7c1a9a9e..067590835 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 { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotChannelPermissions, diff --git a/src/helpers/members/fetch_members.ts b/src/helpers/members/fetch_members.ts index 448f13eac..f894a61dd 100644 --- a/src/helpers/members/fetch_members.ts +++ b/src/helpers/members/fetch_members.ts @@ -1,5 +1,7 @@ import { identifyPayload } from "../../bot.ts"; import { Member } from "../../structures/mod.ts"; +import { DiscordGatewayIntents } from "../../types/gateway/gateway_intents.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { Collection } from "../../util/collection.ts"; import { requestAllMembers } from "../../ws/shard_manager.ts"; @@ -19,7 +21,7 @@ export function fetchMembers( // You can request 1 member without the intent if ( (!options?.limit || options.limit > 1) && - !(identifyPayload.intents && Intents.GUILD_MEMBERS) + !(identifyPayload.intents && DiscordGatewayIntents.GUILD_MEMBERS) ) { throw new Error(Errors.MISSING_INTENT_GUILD_MEMBERS); } diff --git a/src/helpers/members/get_member.ts b/src/helpers/members/get_member.ts index bc332d57f..fe4d239c3 100644 --- a/src/helpers/members/get_member.ts +++ b/src/helpers/members/get_member.ts @@ -1,5 +1,4 @@ import { cacheHandlers } from "../../cache.ts"; -import { RequestManager } from "../../rest/request_manager.ts"; import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; import { endpoints } from "../../util/constants.ts"; diff --git a/src/helpers/members/get_members.ts b/src/helpers/members/get_members.ts index 18126948b..e2deb907c 100644 --- a/src/helpers/members/get_members.ts +++ b/src/helpers/members/get_members.ts @@ -2,6 +2,9 @@ import { identifyPayload } from "../../bot.ts"; import { cacheHandlers } from "../../cache.ts"; import { rest } from "../../rest/rest.ts"; import { Member, structures } from "../../structures/mod.ts"; +import { DiscordGatewayIntents } from "../../types/gateway/gateway_intents.ts"; +import { DiscordGuildMember } from "../../types/guilds/guild_member.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { Collection } from "../../util/collection.ts"; import { endpoints } from "../../util/constants.ts"; @@ -14,7 +17,7 @@ import { endpoints } from "../../util/constants.ts"; * GW(fetchMembers): 120/m(PER shard) rate limit. Meaning if you have 8 shards your limit is 960/m. */ export async function getMembers(guildId: string, options?: GetMemberOptions) { - if (!(identifyPayload.intents && Intents.GUILD_MEMBERS)) { + if (!(identifyPayload.intents && DiscordGatewayIntents.GUILD_MEMBERS)) { throw new Error(Errors.MISSING_INTENT_GUILD_MEMBERS); } @@ -51,7 +54,7 @@ export async function getMembers(guildId: string, options?: GetMemberOptions) { ? `&after=${options.after}` : "" }`, - )) as MemberCreatePayload[]; + )) as DiscordGuildMember[]; const memberStructures = await Promise.all( result.map(async (member) => { diff --git a/src/helpers/members/kick_member.ts b/src/helpers/members/kick_member.ts index fe2fdd2f1..3adee00f6 100644 --- a/src/helpers/members/kick_member.ts +++ b/src/helpers/members/kick_member.ts @@ -1,5 +1,6 @@ import { botId } from "../../bot.ts"; import { rest } from "../../rest/rest.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { highestRole, diff --git a/src/helpers/members/prune_members.ts b/src/helpers/members/prune_members.ts index 31e558946..74f48ed5b 100644 --- a/src/helpers/members/prune_members.ts +++ b/src/helpers/members/prune_members.ts @@ -1,4 +1,5 @@ import { rest } from "../../rest/rest.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotGuildPermissions } from "../../util/permissions.ts"; import { camelKeysToSnakeCase } from "../../util/utils.ts"; diff --git a/src/helpers/messages/delete_messages.ts b/src/helpers/messages/delete_messages.ts index 5c8cbd34e..ce7055d1a 100644 --- a/src/helpers/messages/delete_messages.ts +++ b/src/helpers/messages/delete_messages.ts @@ -1,4 +1,5 @@ import { rest } from "../../rest/rest.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts"; diff --git a/src/helpers/messages/edit_message.ts b/src/helpers/messages/edit_message.ts index 6e74e21c5..9f29f820e 100644 --- a/src/helpers/messages/edit_message.ts +++ b/src/helpers/messages/edit_message.ts @@ -1,6 +1,7 @@ import { botId } from "../../bot.ts"; import { rest } from "../../rest/rest.ts"; import { Message, structures } from "../../structures/mod.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts"; diff --git a/src/helpers/messages/send_message.ts b/src/helpers/messages/send_message.ts index c29b56c03..b7c628917 100644 --- a/src/helpers/messages/send_message.ts +++ b/src/helpers/messages/send_message.ts @@ -2,7 +2,8 @@ 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 { PermissionStrings } from "../../types/mod.ts"; +import { Errors } from "../../types/misc/errors.ts"; +import { PermissionStrings } from "../../types/permissions/permission_strings.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts"; diff --git a/src/helpers/roles/add_role.ts b/src/helpers/roles/add_role.ts index b8770fe9a..6213823c3 100644 --- a/src/helpers/roles/add_role.ts +++ b/src/helpers/roles/add_role.ts @@ -1,5 +1,6 @@ import { botId } from "../../bot.ts"; import { rest } from "../../rest/rest.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { isHigherPosition, diff --git a/src/helpers/roles/remove_role.ts b/src/helpers/roles/remove_role.ts index 96462916e..786b0136e 100644 --- a/src/helpers/roles/remove_role.ts +++ b/src/helpers/roles/remove_role.ts @@ -1,5 +1,6 @@ import { botId } from "../../bot.ts"; import { rest } from "../../rest/rest.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { isHigherPosition, diff --git a/src/helpers/webhooks/create_webhook.ts b/src/helpers/webhooks/create_webhook.ts index f94128ff8..bd771596f 100644 --- a/src/helpers/webhooks/create_webhook.ts +++ b/src/helpers/webhooks/create_webhook.ts @@ -1,4 +1,5 @@ import { rest } from "../../rest/rest.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; import { requireBotChannelPermissions } from "../../util/permissions.ts"; import { urlToBase64 } from "../../util/utils.ts"; diff --git a/src/helpers/webhooks/edit_webhook_message.ts b/src/helpers/webhooks/edit_webhook_message.ts index fd68d5e4b..1653f9a77 100644 --- a/src/helpers/webhooks/edit_webhook_message.ts +++ b/src/helpers/webhooks/edit_webhook_message.ts @@ -1,5 +1,6 @@ import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; export async function editWebhookMessage( diff --git a/src/helpers/webhooks/execute_webhook.ts b/src/helpers/webhooks/execute_webhook.ts index 2a378645c..cee2d2fa2 100644 --- a/src/helpers/webhooks/execute_webhook.ts +++ b/src/helpers/webhooks/execute_webhook.ts @@ -1,5 +1,6 @@ import { rest } from "../../rest/rest.ts"; import { structures } from "../../structures/mod.ts"; +import { Errors } from "../../types/misc/errors.ts"; import { endpoints } from "../../util/constants.ts"; /** Execute a webhook with webhook Id and webhook token */ diff --git a/src/rest/run_method.ts b/src/rest/run_method.ts index b8f69712c..da23c9639 100644 --- a/src/rest/run_method.ts +++ b/src/rest/run_method.ts @@ -1,3 +1,7 @@ +import { Errors } from "../types/misc/errors.ts"; +import { IMAGE_BASE_URL } from "../util/constants.ts"; +import { API_VERSION } from "../util/constants.ts"; +import { BASE_URL } from "../util/constants.ts"; import { rest } from "./rest.ts"; export function runMethod( diff --git a/src/util/permissions.ts b/src/util/permissions.ts index 9fa068d87..0d111aff3 100644 --- a/src/util/permissions.ts +++ b/src/util/permissions.ts @@ -1,34 +1,31 @@ import { botId } from "../bot.ts"; import { cacheHandlers } from "../cache.ts"; import { Channel, Guild, Member, Role } from "../structures/mod.ts"; -import { - DiscordBitwisePermissionFlags, - PermissionStrings, -} from "../types/mod.ts"; +import { Errors } from "../types/misc/errors.ts"; +import { DiscordBitwisePermissionFlags } from "../types/permissions/bitwise_permission_flags.ts"; +import { PermissionStrings } from "../types/permissions/permission_strings.ts"; -async function getCached( - table: "guilds", - key: string | Guild, -): Promise; +async function getCached(table: "guilds", key: string | Guild): Promise; async function getCached( table: "channels", - key: string | Channel, + key: string | Channel ): Promise; async function getCached( table: "members", - key: string | Member, + key: string | Member ): Promise; async function getCached( table: "guilds" | "channels" | "members", - key: string | Guild | Channel | Member, + key: string | Guild | Channel | Member ) { - const cached = typeof key === "string" - ? // @ts-ignore TS is wrong here - (await cacheHandlers.get(table, key)) - : key; + const cached = + typeof key === "string" + ? // @ts-ignore TS is wrong here + await cacheHandlers.get(table, key) + : key; if (!cached || typeof cached === "string") { throw new Error( - Errors[`${table.slice(0, -1).toUpperCase()}_NOT_FOUND` as Errors], + Errors[`${table.slice(0, -1).toUpperCase()}_NOT_FOUND` as Errors] ); } @@ -38,7 +35,7 @@ async function getCached( /** Calculates the permissions this member has in the given guild */ export async function calculateBasePermissions( guild: string | Guild, - member: string | Member, + member: string | Member ) { guild = await getCached("guilds", guild); member = await getCached("members", member); @@ -63,7 +60,7 @@ export async function calculateBasePermissions( /** Calculates the permissions this member has for the given Channel */ export async function calculateChannelOverwrites( channel: string | Channel, - member: string | Member, + member: string | Member ) { channel = await getCached("channels", channel); @@ -74,12 +71,12 @@ export async function calculateChannelOverwrites( // Get all the role permissions this member already has let permissions = BigInt( - await calculateBasePermissions(channel.guildId, member), + await calculateBasePermissions(channel.guildId, member) ); // First calculate @everyone overwrites since these have the lowest priority const overwriteEveryone = channel?.permissionOverwrites.find( - (overwrite) => overwrite.id === (channel as Channel).guildId, + (overwrite) => overwrite.id === (channel as Channel).guildId ); if (overwriteEveryone) { // First remove denied permissions since denied < allowed @@ -106,7 +103,7 @@ export async function calculateChannelOverwrites( // Third calculate member specific overwrites since these have the highest priority const overwriteMember = overwrites.find( - (overwrite) => overwrite.id === (member as Member).id, + (overwrite) => overwrite.id === (member as Member).id ); if (overwriteMember) { permissions &= ~BigInt(overwriteMember.deny); @@ -119,14 +116,14 @@ export async function calculateChannelOverwrites( /** Checks if the given permission bits are matching the given permissions. `ADMINISTRATOR` always returns `true` */ export function validatePermissions( permissionBits: string, - permissions: Permission[], + permissions: PermissionStrings[] ) { if (BigInt(permissionBits) & 8n) return true; return permissions.every( (permission) => // Check if permission is in permissionBits - BigInt(permissionBits) & BigInt(Permissions[permission]), + BigInt(permissionBits) & BigInt(DiscordBitwisePermissionFlags[permission]) ); } @@ -134,7 +131,7 @@ export function validatePermissions( export async function hasGuildPermissions( guild: string | Guild, member: string | Member, - permissions: Permission[], + permissions: PermissionStrings[] ) { // First we need the role permission bits this member has const basePermissions = await calculateBasePermissions(guild, member); @@ -145,7 +142,7 @@ export async function hasGuildPermissions( /** Checks if the bot has these permissions in the given guild */ export function botHasGuildPermissions( guild: string | Guild, - permissions: Permission[], + permissions: PermissionStrings[] ) { // Since Bot is a normal member we can use the hasRolePermissions() function return hasGuildPermissions(guild, botId, permissions); @@ -155,13 +152,10 @@ export function botHasGuildPermissions( export async function hasChannelPermissions( channel: string | Channel, member: string | Member, - permissions: Permission[], + permissions: PermissionStrings[] ) { // First we need the overwrite bits this member has - const channelOverwrites = await calculateChannelOverwrites( - channel, - member, - ); + const channelOverwrites = await calculateChannelOverwrites(channel, member); // Second use the validatePermissions function to check if the member has every permission return validatePermissions(channelOverwrites, permissions); } @@ -169,7 +163,7 @@ export async function hasChannelPermissions( /** Checks if the bot has these permissions f0r the given channel */ export function botHasChannelPermissions( channel: string | Channel, - permissions: Permission[], + permissions: PermissionStrings[] ) { // Since Bot is a normal member we can use the hasRolePermissions() function return hasChannelPermissions(channel, botId, permissions); @@ -178,14 +172,16 @@ export function botHasChannelPermissions( /** Returns the permissions that are not in the given permissionBits */ export function missingPermissions( permissionBits: string, - permissions: PermissionStrings[], + permissions: PermissionStrings[] ) { if (BigInt(permissionBits) & 8n) return []; return permissions.filter( (permission) => - !(BigInt(permissionBits) & - BigInt(DiscordBitwisePermissionFlags[permission])), + !( + BigInt(permissionBits) & + BigInt(DiscordBitwisePermissionFlags[permission]) + ) ); } @@ -193,7 +189,7 @@ export function missingPermissions( export async function getMissingGuildPermissions( guild: string | Guild, member: string | Member, - permissions: Permission[], + permissions: PermissionStrings[] ) { // First we need the role permissino bits this member has const permissionBits = await calculateBasePermissions(guild, member); @@ -205,7 +201,7 @@ export async function getMissingGuildPermissions( export async function getMissingChannelPermissions( channel: string | Channel, member: string | Member, - permissions: Permission[], + permissions: PermissionStrings[] ) { // First we need the role permissino bits this member has const permissionBits = await calculateChannelOverwrites(channel, member); @@ -217,7 +213,7 @@ export async function getMissingChannelPermissions( export async function requireGuildPermissions( guild: string | Guild, member: string | Member, - permissions: Permission[], + permissions: PermissionStrings[] ) { const missing = await getMissingGuildPermissions(guild, member, permissions); if (missing.length) { @@ -229,7 +225,7 @@ export async function requireGuildPermissions( /** Throws an error if the bot does not have all permissions */ export function requireBotGuildPermissions( guild: string | Guild, - permissions: Permission[], + permissions: PermissionStrings[] ) { // Since Bot is a normal member we can use the throwOnMissingGuildPermission() function return requireGuildPermissions(guild, botId, permissions); @@ -239,12 +235,12 @@ export function requireBotGuildPermissions( export async function requireChannelPermissions( channel: string | Channel, member: string | Member, - permissions: Permission[], + permissions: PermissionStrings[] ) { const missing = await getMissingChannelPermissions( channel, member, - permissions, + permissions ); if (missing.length) { // If the member is missing a permission throw an Error @@ -255,7 +251,7 @@ export async function requireChannelPermissions( /** Throws an error if the bot has not all of the given channel permissions */ export function requireBotChannelPermissions( channel: string | Channel, - permissions: Permission[], + permissions: PermissionStrings[] ) { // Since Bot is a normal member we can use the throwOnMissingChannelPermission() function return requireChannelPermissions(channel, botId, permissions); @@ -263,19 +259,19 @@ export function requireBotChannelPermissions( /** This function converts a bitwise string to permission strings */ export function calculatePermissions(permissionBits: bigint) { - return Object.keys(Permissions).filter((permission) => { + return Object.keys(DiscordBitwisePermissionFlags).filter((permission) => { // Since Object.keys() not only returns the permission names but also the bit values we need to return false if it is a Number if (Number(permission)) return false; // Check if permissionBits has this permission - return permissionBits & BigInt(Permissions[permission as Permission]); - }) as Permission[]; + return permissionBits & BigInt(DiscordBitwisePermissionFlags[permission as PermissionStrings]); + }) as PermissionStrings[]; } /** This function converts an array of permissions into the bitwise string. */ -export function calculateBits(permissions: Permission[]) { +export function calculateBits(permissions: PermissionStrings[]) { return permissions .reduce((bits, perm) => { - bits |= BigInt(Permissions[perm]); + bits |= BigInt(DiscordBitwisePermissionFlags[perm]); return bits; }, 0n) .toString(); @@ -284,14 +280,13 @@ export function calculateBits(permissions: Permission[]) { /** Gets the highest role from the member in this guild */ export async function highestRole( guild: string | Guild, - member: string | Member, + member: string | Member ) { guild = await getCached("guilds", guild); // Get the roles from the member - const memberRoles = ( - await getCached("members", member) - ).guilds.get(guild.id)?.roles; + const memberRoles = (await getCached("members", member)).guilds.get(guild.id) + ?.roles; // This member has no roles so the highest one is the @everyone role if (!memberRoles) return guild.roles.get(guild.id) as Role; @@ -321,7 +316,7 @@ export async function highestRole( export async function higherRolePosition( guild: string | Guild, roleId: string, - otherRoleId: string, + otherRoleId: string ) { guild = await getCached("guilds", guild); @@ -341,7 +336,7 @@ export async function higherRolePosition( export async function isHigherPosition( guild: string | Guild, memberId: string, - compareRoleId: string, + compareRoleId: string ) { guild = await getCached("guilds", guild); diff --git a/src/util/utils.ts b/src/util/utils.ts index 2e3abec21..18041d85b 100644 --- a/src/util/utils.ts +++ b/src/util/utils.ts @@ -1,5 +1,6 @@ import { encode } from "../../deps.ts"; -import { DiscordGatewayOpcodes } from "../types/mod.ts"; +import { DiscordGatewayOpcodes } from "../types/codes/gateway_opcodes.ts"; +import { Errors } from "../types/misc/errors.ts"; import { basicShards, sendWS } from "../ws/shard.ts"; import { SLASH_COMMANDS_NAME_REGEX } from "./constants.ts";