From 871e771c73b8f463dd7fe452a3218a6398ff8487 Mon Sep 17 00:00:00 2001 From: suneettipirneni Date: Sun, 18 Jul 2021 00:55:15 -0400 Subject: [PATCH 1/5] add ID permission handlers --- package-lock.json | 14 +- package.json | 2 +- src/utils/permissions.ts | 348 ++++++++++++++++++++++++++------------- 3 files changed, 241 insertions(+), 123 deletions(-) diff --git a/package-lock.json b/package-lock.json index 706ca2d..b2919ec 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,7 +21,7 @@ "@typescript-eslint/eslint-plugin": "^4.28.2", "@typescript-eslint/parser": "^4.28.2", "babel-jest": "^27.0.6", - "discord.js": "^13.0.0-dev.5ca97c9.1626480265", + "discord.js": "^13.0.0-dev.4d53d0f.1626566655", "eslint": "^7.30.0", "jest": "^27.0.6", "nodemon": "^2.0.12", @@ -3962,9 +3962,9 @@ } }, "node_modules/discord.js": { - "version": "13.0.0-dev.5ca97c9.1626480265", - "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-13.0.0-dev.5ca97c9.1626480265.tgz", - "integrity": "sha512-icB9UE1yLwp0HpNyrXw9GUBs7x7mY0h+ZxHBxA93wSE9jjmle/Bf6OpCagdco5YDIBSBmnYF33Kysw3fCF7N8w==", + "version": "13.0.0-dev.4d53d0f.1626566655", + "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-13.0.0-dev.4d53d0f.1626566655.tgz", + "integrity": "sha512-TXq6Q16X8QVt4A81q0eaBZd+hfvThIvx76FLd8eRiB4H9Hid3BnVwzbOAucwZ3Xr+koPcaw16YVoV+XL9ZwknA==", "dev": true, "dependencies": { "@discordjs/builders": "^0.2.0", @@ -12695,9 +12695,9 @@ "dev": true }, "discord.js": { - "version": "13.0.0-dev.5ca97c9.1626480265", - "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-13.0.0-dev.5ca97c9.1626480265.tgz", - "integrity": "sha512-icB9UE1yLwp0HpNyrXw9GUBs7x7mY0h+ZxHBxA93wSE9jjmle/Bf6OpCagdco5YDIBSBmnYF33Kysw3fCF7N8w==", + "version": "13.0.0-dev.4d53d0f.1626566655", + "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-13.0.0-dev.4d53d0f.1626566655.tgz", + "integrity": "sha512-TXq6Q16X8QVt4A81q0eaBZd+hfvThIvx76FLd8eRiB4H9Hid3BnVwzbOAucwZ3Xr+koPcaw16YVoV+XL9ZwknA==", "dev": true, "requires": { "@discordjs/builders": "^0.2.0", diff --git a/package.json b/package.json index 3c09625..46f2b12 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ "@typescript-eslint/eslint-plugin": "^4.28.2", "@typescript-eslint/parser": "^4.28.2", "babel-jest": "^27.0.6", - "discord.js": "^13.0.0-dev.5ca97c9.1626480265", + "discord.js": "^13.0.0-dev.4d53d0f.1626566655", "eslint": "^7.30.0", "jest": "^27.0.6", "nodemon": "^2.0.12", diff --git a/src/utils/permissions.ts b/src/utils/permissions.ts index 9f40942..9eb0808 100644 --- a/src/utils/permissions.ts +++ b/src/utils/permissions.ts @@ -1,7 +1,135 @@ -import { GuildMember, TextChannel, ThreadChannel } from 'discord.js'; +import { GuildMember, Snowflake, TextChannel, ThreadChannel } from 'discord.js'; import { PermissionHandler } from '../Command'; import { resolveRoleID } from './role'; +const hasID = (member: GuildMember, id: Snowflake) => member.roles.cache.has(id); + +export const RolePermissions = { + + /** + * A helper function to check if the given roles are present from an interaction. + * @param roles The roles names to check for. + * @returns A permission handler function. + */ + allNames(...roles: string[]): PermissionHandler { + return async (interaction) => { + let allowed = true; + // FIXME is this cast safe? + const member = interaction.member as GuildMember; + if (!member) { + return false; + } + + roles.forEach( + (roleName) => + (allowed &&= member.roles.cache.some((role) => role.name === roleName)) + ); + + // Iterate and check if roles are present. + if (!allowed) { + const guild = interaction.guild; + if (!guild) { + console.log(new Error('Could not find guild')); + return false; + } + const roleIDs = roles.map((role) => resolveRoleID(guild, role)); + const errMsg = + 'You must have the following roles to run this command:\n'.concat( + ...roleIDs.map((role) => `- <@&${role}>\n`) + ); + await interaction.reply({ content: errMsg, ephemeral: true }); + } + + return allowed; + }; + }, + + /** + * A helper function to check if the sender has one of the given roles. + * @param roles The roles names to check for. + * @returns A permission handler function. + */ + oneName(...roles: string[]): PermissionHandler { + return async (interaction) => { + // FIXME is this cast safe? + const member = interaction.member as GuildMember; + const allowed = roles.some((roleName) => + member.roles.cache.find((role) => role.name === roleName) + ); + + if (!allowed) { + const guild = interaction.guild; + if (!guild) { + console.log(new Error('Could not find guild!')); + return false; + } + const roleIDs = roles.map((role) => resolveRoleID(guild, role)); + const errMsg = + 'You must have one of the following roles to run this command:\n'.concat( + ...roleIDs.map((role) => `- <@&${role}>\n`) + ); + await interaction.reply({ content: errMsg, ephemeral: true }); + } + + return allowed; + }; + }, + + /** + * A helper function to check if the given roles are present from an interaction. + * @param roles The roles IDs to check for. + * @returns A permission handler function. + */ + allIDs(...roleIDs: Snowflake[]): PermissionHandler { + return async (interaction) => { + const member = interaction.member as GuildMember; + let allowed = true; + if (!member) { + return false; + } + + roleIDs.forEach(roleID => allowed &&= hasID(member, roleID)); + + if (!allowed) { + const errMsg = + 'You must have the following roles to run this command:\n'.concat( + ...roleIDs.map((role) => `- <@&${role}>\n`) + ); + await interaction.reply({ content: errMsg, ephemeral: true }); + } + + return allowed; + }; + }, + + /** + * A helper function to check if the sender has one of the given roles. + * @param roles The roles IDs to check for. + * @returns A permission handler function. + */ + oneID(...roleIDs: Snowflake[]): PermissionHandler { + return async (interaction) => { + const member = interaction.member as GuildMember; + if (!member) { + return false; + } + + const allowed = member.roles.cache.some(role => roleIDs.includes(role.id)); + + if (!allowed) { + const errMsg = + 'You must have one of the following roles to run this command:\n'.concat( + ...roleIDs.map((role) => `- <@&${role}>\n`) + ); + + await interaction.reply({ content: errMsg, ephemeral: true }); + } + + return allowed; + }; + } +}; + /** * A helper function used to aggregate permission handlers. * @param handlers The handlers to aggregate. @@ -22,136 +150,126 @@ export function checkAll(...handlers: PermissionHandler[]): PermissionHandler { }; } -/** - * A helper function to check if the given roles are present from an interaction. - * @param roles The roles to check for. - * @returns A permission handler function. - */ -export function allRoles(...roles: string[]): PermissionHandler { - return async (interaction) => { - let allowed = true; - // FIXME is this cast safe? - const member = interaction.member as GuildMember; - if (!member) { - return false; - } - - roles.forEach( - (roleName) => - (allowed &&= member.roles.cache.some((role) => role.name === roleName)) - ); - - // Iterate and check if roles are present. - if (!allowed) { - const guild = interaction.guild; - if (!guild) { - console.log(new Error('Could not find guild')); +export const ChannelPermissions = { + /** + * A helper function to check if the interaction was sent in the given channel(s). + * @param channels The channels names to check for. + * @returns A permission handler function. + */ + inNames(...channels: string[]): PermissionHandler { + return async (interaction) => { + // Resolve each of the channel names + const channelIDs = channels.map( + (channelName) => + interaction.client.channels.cache.find( + (channel) => + (channel).name === channelName + )?.id + ); + if (!channelIDs || !interaction.channel) { + console.log('No channels were found!'); return false; } - const roleIDs = roles.map((role) => resolveRoleID(guild, role)); - const errMsg = - 'You must have the following roles to run this command:\n'.concat( - ...roleIDs.map((role) => `- <@&${role}>\n`) - ); - await interaction.reply({ content: errMsg, ephemeral: true }); - } - return allowed; - }; -} + const valid = channelIDs.includes(interaction.channelId); -/** - * A helper function to check if the sender has one of the given roles. - * @param roles The roles to check for. - * @returns A permission handler function. - */ -export function oneOfRoles(...roles: string[]): PermissionHandler { - return async (interaction) => { - // FIXME is this cast safe? - const member = interaction.member as GuildMember; - const allowed = roles.some((roleName) => - member.roles.cache.find((role) => role.name === roleName) - ); - - if (!allowed) { - const guild = interaction.guild; - if (!guild) { - console.log(new Error('Could not find guild!')); - return false; - } - const roleIDs = roles.map((role) => resolveRoleID(guild, role)); - const errMsg = - 'You must have one of the following roles to run this command:\n'.concat( - ...roleIDs.map((role) => `- <@&${role}>\n`) + if (!valid) { + const errMsg = 'Please use this command in an allowed channel:\n'.concat( + ...channelIDs.map((channel) => `- <#${channel}>\n`) ); - await interaction.reply({ content: errMsg, ephemeral: true }); - } - return allowed; - }; -} + // Send error message. + await interaction.reply({ content: errMsg, ephemeral: true }); + } -/** - * A helper function to check if the interaction was sent in the given channel(s). - * @param channels The channels to check for. - * @returns A permission handler function. - */ -export function inChannels(...channels: string[]): PermissionHandler { - return async (interaction) => { - // Resolve each of the channel names - const channelIDs = channels.map( - (channelName) => - interaction.client.channels.cache.find( - (channel) => - (channel).name === channelName - )?.id - ); - if (!channelIDs || !interaction.channel) { - console.log('No channels were found!'); - return false; - } + return valid; + }; + }, - const valid = channelIDs.includes(interaction.channelId); + /** + * A helper function to check if the interaction was sent in the given channel(s). + * @param channels The channels IDs to check for. + * @returns A permission handler function. + */ + inIDs(...channelIDs: Snowflake[]): PermissionHandler { + return async (interaction) => { - if (!valid) { - const errMsg = 'Please use this command in an allowed channel:\n'.concat( - ...channelIDs.map((channel) => `- <#${channel}>\n`) - ); + if (!interaction.channel) { + console.log('No channels found'); + } - // Send error message. - await interaction.reply({ content: errMsg, ephemeral: true }); - } + const allowed = channelIDs.includes(interaction.channelId); - return valid; - }; -} + if (!allowed) { + const errMsg = 'Please use this command in an allowed channel:\n'.concat( + ...channelIDs.map((channel) => `- <#${channel}>\n`) + ); -/** - * Checks if a given interaction is created in an allowed category. - * @param categories The categories to check for - * @returns A permission handler. - */ -export function inCategories(...categories: string[]): PermissionHandler { - return async (interaction) => { - if (!(interaction.channel instanceof TextChannel)) { - return false; - } + // Send error message. + await interaction.reply({ content: errMsg, ephemeral: true }); + } - const category = interaction.channel.parent?.name; - if (!category) { - return false; - } + return allowed; + }; + } +}; - const allowed = categories.includes(category); +export const CategoryPermissions = { + /** + * Checks if a given interaction is created in an allowed category. + * @param categories The categories names to check for + * @returns A permission handler. + */ + inNames(...categories: string[]): PermissionHandler { + return async (interaction) => { + if (!(interaction.channel instanceof TextChannel)) { + return false; + } - if (!allowed) { - const content = + const category = interaction.channel.parent?.name; + if (!category) { + return false; + } + + const allowed = categories.includes(category); + + if (!allowed) { + const content = 'Please use the command in the following categories:\n'.concat( ...categories.map((cur) => `- ${cur}\n`) ); - await interaction.reply({ content, ephemeral: true }); - } + await interaction.reply({ content, ephemeral: true }); + } - return allowed; - }; -} + return allowed; + }; + }, + + /** + * Checks if a given interaction is created in an allowed category. + * @param categories The categories IDs to check for + * @returns A permission handler. + */ + inIDs(...categoryIDs: Snowflake[]): PermissionHandler { + return async (interaction) => { + if (!(interaction.channel instanceof TextChannel)) { + return false; + } + + const category = interaction.channel.parent; + + if (!category) { + return false; + } + + const allowed = categoryIDs.includes(category.id); + + if (!allowed) { + const content = 'This command is not allowed in this category'; + await interaction.reply({ content, ephemeral: true }); + } + + return allowed; + }; + } +}; From b283cf7d91b304bc60c386e91c2ae5b1b4217271 Mon Sep 17 00:00:00 2001 From: suneettipirneni Date: Sun, 18 Jul 2021 17:45:04 -0400 Subject: [PATCH 2/5] change method names --- src/utils/permissions.ts | 423 +++++++++++++++++++-------------------- 1 file changed, 208 insertions(+), 215 deletions(-) diff --git a/src/utils/permissions.ts b/src/utils/permissions.ts index 9eb0808..8014163 100644 --- a/src/utils/permissions.ts +++ b/src/utils/permissions.ts @@ -4,131 +4,128 @@ import { resolveRoleID } from './role'; const hasID = (member: GuildMember, id: Snowflake) => member.roles.cache.has(id); -export const RolePermissions = { - - /** - * A helper function to check if the given roles are present from an interaction. - * @param roles The roles names to check for. - * @returns A permission handler function. - */ - allNames(...roles: string[]): PermissionHandler { - return async (interaction) => { - let allowed = true; - // FIXME is this cast safe? - const member = interaction.member as GuildMember; - if (!member) { - return false; - } +/** + * A helper function to check if the given roles are present from an interaction. + * @param roles The roles names to check for. + * @returns A permission handler function. + */ +export function allRoleNames(...roles: string[]): PermissionHandler { + return async (interaction) => { + let allowed = true; + // FIXME is this cast safe? + const member = interaction.member as GuildMember; + if (!member) { + return false; + } - roles.forEach( - (roleName) => - (allowed &&= member.roles.cache.some((role) => role.name === roleName)) - ); + roles.forEach( + (roleName) => + (allowed &&= member.roles.cache.some((role) => role.name === roleName)) + ); - // Iterate and check if roles are present. - if (!allowed) { - const guild = interaction.guild; - if (!guild) { - console.log(new Error('Could not find guild')); - return false; - } - const roleIDs = roles.map((role) => resolveRoleID(guild, role)); - const errMsg = - 'You must have the following roles to run this command:\n'.concat( - ...roleIDs.map((role) => `- <@&${role}>\n`) - ); - await interaction.reply({ content: errMsg, ephemeral: true }); + // Iterate and check if roles are present. + if (!allowed) { + const guild = interaction.guild; + if (!guild) { + console.log(new Error('Could not find guild')); + return false; } - - return allowed; - }; - }, - - /** - * A helper function to check if the sender has one of the given roles. - * @param roles The roles names to check for. - * @returns A permission handler function. - */ - oneName(...roles: string[]): PermissionHandler { - return async (interaction) => { - // FIXME is this cast safe? - const member = interaction.member as GuildMember; - const allowed = roles.some((roleName) => - member.roles.cache.find((role) => role.name === roleName) + const roleIDs = roles.map((role) => resolveRoleID(guild, role)); + const errMsg = + 'You must have the following roles to run this command:\n'.concat( + ...roleIDs.map((role) => `- <@&${role}>\n`) ); + await interaction.reply({ content: errMsg, ephemeral: true }); + } - if (!allowed) { - const guild = interaction.guild; - if (!guild) { - console.log(new Error('Could not find guild!')); - return false; - } - const roleIDs = roles.map((role) => resolveRoleID(guild, role)); - const errMsg = - 'You must have one of the following roles to run this command:\n'.concat( - ...roleIDs.map((role) => `- <@&${role}>\n`) - ); - await interaction.reply({ content: errMsg, ephemeral: true }); - } + return allowed; + }; +} - return allowed; - }; - }, - - /** - * A helper function to check if the given roles are present from an interaction. - * @param roles The roles IDs to check for. - * @returns A permission handler function. - */ - allIDs(...roleIDs: Snowflake[]): PermissionHandler { - return async (interaction) => { - const member = interaction.member as GuildMember; - let allowed = true; - if (!member) { +/** + * A helper function to check if the sender has one of the given roles. + * @param roles The roles names to check for. + * @returns A permission handler function. + */ +export function oneRoleName(...roles: string[]): PermissionHandler { + return async (interaction) => { + // FIXME is this cast safe? + const member = interaction.member as GuildMember; + const allowed = roles.some((roleName) => + member.roles.cache.find((role) => role.name === roleName) + ); + + if (!allowed) { + const guild = interaction.guild; + if (!guild) { + console.log(new Error('Could not find guild!')); return false; } + const roleIDs = roles.map((role) => resolveRoleID(guild, role)); + const errMsg = + 'You must have one of the following roles to run this command:\n'.concat( + ...roleIDs.map((role) => `- <@&${role}>\n`) + ); + await interaction.reply({ content: errMsg, ephemeral: true }); + } - roleIDs.forEach(roleID => allowed &&= hasID(member, roleID)); + return allowed; + }; +} - if (!allowed) { - const errMsg = - 'You must have the following roles to run this command:\n'.concat( - ...roleIDs.map((role) => `- <@&${role}>\n`) - ); - await interaction.reply({ content: errMsg, ephemeral: true }); - } - - return allowed; - }; - }, - - /** - * A helper function to check if the sender has one of the given roles. - * @param roles The roles IDs to check for. - * @returns A permission handler function. - */ - oneID(...roleIDs: Snowflake[]): PermissionHandler { - return async (interaction) => { - const member = interaction.member as GuildMember; - if (!member) { - return false; - } +/** + * A helper function to check if the given roles are present from an interaction. + * @param roles The roles IDs to check for. + * @returns A permission handler function. + */ +export function allRoles(...roleIDs: Snowflake[]): PermissionHandler { + return async (interaction) => { + const member = interaction.member as GuildMember; + let allowed = true; + if (!member) { + return false; + } - const allowed = member.roles.cache.some(role => roleIDs.includes(role.id)); + roleIDs.forEach(roleID => allowed &&= hasID(member, roleID)); - if (!allowed) { - const errMsg = - 'You must have one of the following roles to run this command:\n'.concat( - ...roleIDs.map((role) => `- <@&${role}>\n`) - ); - - await interaction.reply({ content: errMsg, ephemeral: true }); - } + if (!allowed) { + const errMsg = + 'You must have the following roles to run this command:\n'.concat( + ...roleIDs.map((role) => `- <@&${role}>\n`) + ); + await interaction.reply({ content: errMsg, ephemeral: true }); + } + + return allowed; + }; +} - return allowed; - }; - } -}; +/** + * A helper function to check if the sender has one of the given roles. + * @param roles The roles IDs to check for. + * @returns A permission handler function. + */ +export function oneRole(...roleIDs: Snowflake[]): PermissionHandler { + return async (interaction) => { + const member = interaction.member as GuildMember; + if (!member) { + return false; + } + + const allowed = member.roles.cache.some(role => roleIDs.includes(role.id)); + + if (!allowed) { + const errMsg = + 'You must have one of the following roles to run this command:\n'.concat( + ...roleIDs.map((role) => `- <@&${role}>\n`) + ); + + await interaction.reply({ content: errMsg, ephemeral: true }); + } + + return allowed; + }; +} /** * A helper function used to aggregate permission handlers. @@ -150,126 +147,122 @@ export function checkAll(...handlers: PermissionHandler[]): PermissionHandler { }; } -export const ChannelPermissions = { - /** - * A helper function to check if the interaction was sent in the given channel(s). - * @param channels The channels names to check for. - * @returns A permission handler function. - */ - inNames(...channels: string[]): PermissionHandler { - return async (interaction) => { - // Resolve each of the channel names - const channelIDs = channels.map( - (channelName) => - interaction.client.channels.cache.find( - (channel) => - (channel).name === channelName - )?.id +/** + * A helper function to check if the interaction was sent in the given channel(s). + * @param channels The channels names to check for. + * @returns A permission handler function. + */ +export function inChannelNames(...channels: string[]): PermissionHandler { + return async (interaction) => { + // Resolve each of the channel names + const channelIDs = channels.map( + (channelName) => + interaction.client.channels.cache.find( + (channel) => + (channel).name === channelName + )?.id + ); + if (!channelIDs || !interaction.channel) { + console.log('No channels were found!'); + return false; + } + + const valid = channelIDs.includes(interaction.channelId); + + if (!valid) { + const errMsg = 'Please use this command in an allowed channel:\n'.concat( + ...channelIDs.map((channel) => `- <#${channel}>\n`) ); - if (!channelIDs || !interaction.channel) { - console.log('No channels were found!'); - return false; - } - const valid = channelIDs.includes(interaction.channelId); + // Send error message. + await interaction.reply({ content: errMsg, ephemeral: true }); + } - if (!valid) { - const errMsg = 'Please use this command in an allowed channel:\n'.concat( - ...channelIDs.map((channel) => `- <#${channel}>\n`) - ); + return valid; + }; +} - // Send error message. - await interaction.reply({ content: errMsg, ephemeral: true }); - } +/** + * A helper function to check if the interaction was sent in the given channel(s). + * @param channels The channels IDs to check for. + * @returns A permission handler function. + */ +export function inChannels(...channelIDs: Snowflake[]): PermissionHandler { + return async (interaction) => { - return valid; - }; - }, + if (!interaction.channel) { + console.log('No channels found'); + } - /** - * A helper function to check if the interaction was sent in the given channel(s). - * @param channels The channels IDs to check for. - * @returns A permission handler function. - */ - inIDs(...channelIDs: Snowflake[]): PermissionHandler { - return async (interaction) => { + const allowed = channelIDs.includes(interaction.channelId); - if (!interaction.channel) { - console.log('No channels found'); - } + if (!allowed) { + const errMsg = 'Please use this command in an allowed channel:\n'.concat( + ...channelIDs.map((channel) => `- <#${channel}>\n`) + ); - const allowed = channelIDs.includes(interaction.channelId); + // Send error message. + await interaction.reply({ content: errMsg, ephemeral: true }); + } - if (!allowed) { - const errMsg = 'Please use this command in an allowed channel:\n'.concat( - ...channelIDs.map((channel) => `- <#${channel}>\n`) - ); + return allowed; + }; +} - // Send error message. - await interaction.reply({ content: errMsg, ephemeral: true }); - } +/** + * Checks if a given interaction is created in an allowed category. + * @param categories The categories names to check for + * @returns A permission handler. + */ +export function inCategoryNames(...categories: string[]): PermissionHandler { + return async (interaction) => { + if (!(interaction.channel instanceof TextChannel)) { + return false; + } - return allowed; - }; - } -}; - -export const CategoryPermissions = { - /** - * Checks if a given interaction is created in an allowed category. - * @param categories The categories names to check for - * @returns A permission handler. - */ - inNames(...categories: string[]): PermissionHandler { - return async (interaction) => { - if (!(interaction.channel instanceof TextChannel)) { - return false; - } + const category = interaction.channel.parent?.name; + if (!category) { + return false; + } - const category = interaction.channel.parent?.name; - if (!category) { - return false; - } + const allowed = categories.includes(category); - const allowed = categories.includes(category); + if (!allowed) { + const content = + 'Please use the command in the following categories:\n'.concat( + ...categories.map((cur) => `- ${cur}\n`) + ); + await interaction.reply({ content, ephemeral: true }); + } - if (!allowed) { - const content = - 'Please use the command in the following categories:\n'.concat( - ...categories.map((cur) => `- ${cur}\n`) - ); - await interaction.reply({ content, ephemeral: true }); - } + return allowed; + }; +} - return allowed; - }; - }, - - /** - * Checks if a given interaction is created in an allowed category. - * @param categories The categories IDs to check for - * @returns A permission handler. - */ - inIDs(...categoryIDs: Snowflake[]): PermissionHandler { - return async (interaction) => { - if (!(interaction.channel instanceof TextChannel)) { - return false; - } +/** + * Checks if a given interaction is created in an allowed category. + * @param categories The categories IDs to check for + * @returns A permission handler. + */ +export function inCategories(...categoryIDs: Snowflake[]): PermissionHandler { + return async (interaction) => { + if (!(interaction.channel instanceof TextChannel)) { + return false; + } - const category = interaction.channel.parent; + const category = interaction.channel.parent; - if (!category) { - return false; - } + if (!category) { + return false; + } - const allowed = categoryIDs.includes(category.id); + const allowed = categoryIDs.includes(category.id); - if (!allowed) { - const content = 'This command is not allowed in this category'; - await interaction.reply({ content, ephemeral: true }); - } + if (!allowed) { + const content = 'This command is not allowed in this category'; + await interaction.reply({ content, ephemeral: true }); + } - return allowed; - }; - } -}; + return allowed; + }; +} From bd22fbb9cab0d144e9ceefca3cd2e1eaaacbbeae Mon Sep 17 00:00:00 2001 From: suneettipirneni Date: Sun, 18 Jul 2021 17:55:35 -0400 Subject: [PATCH 3/5] changed to `inRoleNames` --- src/utils/permissions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/permissions.ts b/src/utils/permissions.ts index 8014163..83a1fb1 100644 --- a/src/utils/permissions.ts +++ b/src/utils/permissions.ts @@ -47,7 +47,7 @@ export function allRoleNames(...roles: string[]): PermissionHandler { * @param roles The roles names to check for. * @returns A permission handler function. */ -export function oneRoleName(...roles: string[]): PermissionHandler { +export function inRoleNames(...roles: string[]): PermissionHandler { return async (interaction) => { // FIXME is this cast safe? const member = interaction.member as GuildMember; From ab26b5110c1a4416eeb454f576b966e4fea5f363 Mon Sep 17 00:00:00 2001 From: Robert Boyd III Date: Sun, 18 Jul 2021 18:03:05 -0400 Subject: [PATCH 4/5] Rename oneRole to inRoles --- src/utils/permissions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/permissions.ts b/src/utils/permissions.ts index 83a1fb1..5ac8eee 100644 --- a/src/utils/permissions.ts +++ b/src/utils/permissions.ts @@ -105,7 +105,7 @@ export function allRoles(...roleIDs: Snowflake[]): PermissionHandler { * @param roles The roles IDs to check for. * @returns A permission handler function. */ -export function oneRole(...roleIDs: Snowflake[]): PermissionHandler { +export function inRoles(...roleIDs: Snowflake[]): PermissionHandler { return async (interaction) => { const member = interaction.member as GuildMember; if (!member) { From aff341d94d301565ce7d23e65ee86570eef06f6c Mon Sep 17 00:00:00 2001 From: Robert Boyd III Date: Sun, 18 Jul 2021 18:03:38 -0400 Subject: [PATCH 5/5] Move checkAll to top of file --- src/utils/permissions.ts | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/utils/permissions.ts b/src/utils/permissions.ts index 5ac8eee..85defe2 100644 --- a/src/utils/permissions.ts +++ b/src/utils/permissions.ts @@ -2,6 +2,26 @@ import { GuildMember, Snowflake, TextChannel, ThreadChannel } from 'discord.js'; import { PermissionHandler } from '../Command'; import { resolveRoleID } from './role'; +/** + * A helper function used to aggregate permission handlers. + * @param handlers The handlers to aggregate. + * @returns A permission handler created from all of the inputs. + */ +export function checkAll(...handlers: PermissionHandler[]): PermissionHandler { + return async (interaction) => { + // Await all of the promises, and complete each of the partial applications. + // This is a for-of because we need the functions to execute in series, rather + // than concurrently. + for (const func of handlers) { + if (!(await func(interaction))) { + return false; + } + } + + return true; + }; +} + const hasID = (member: GuildMember, id: Snowflake) => member.roles.cache.has(id); /** @@ -127,26 +147,6 @@ export function inRoles(...roleIDs: Snowflake[]): PermissionHandler { }; } -/** - * A helper function used to aggregate permission handlers. - * @param handlers The handlers to aggregate. - * @returns A permission handler created from all of the inputs. - */ -export function checkAll(...handlers: PermissionHandler[]): PermissionHandler { - return async (interaction) => { - // Await all of the promises, and complete each of the partial applications. - // This is a for-of because we need the functions to execute in series, rather - // than concurrently. - for (const func of handlers) { - if (!(await func(interaction))) { - return false; - } - } - - return true; - }; -} - /** * A helper function to check if the interaction was sent in the given channel(s). * @param channels The channels names to check for.