Skip to content

Commit

Permalink
edit: Update permCheck.ts
Browse files Browse the repository at this point in the history
Fixed overall formatting
simplified ephemeral booleans
No longer destructuring interaction from ctx
fixed in guild from not fully returning controller.stop()
remove `no_guild` function in favor of hard coding
imported type CommandType
  • Loading branch information
Peter-MJ-Parker authored Jul 8, 2024
1 parent e1cd29d commit 17205d7
Showing 1 changed file with 72 additions and 89 deletions.
161 changes: 72 additions & 89 deletions plugins/permCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,90 +37,79 @@ import {
} from "discord.js";
import {
CommandControlPlugin,
CommandType,
type CommandType,
controller,
Service,
} from "@sern/handler";

function command(perm: PermissionResolvable, response?: string) {
return CommandControlPlugin<CommandType.Both>(async (ctx) => {
if (ctx.guild === null) {
await ctx.reply("This command cannot be used in DM's!");
await ctx.reply({
content: "This command cannot be used in DM's!",
ephemeral: !ctx.isMessage()
});
return controller.stop();
}
if (
!(ctx.member! as GuildMember)
.permissionsIn(ctx.channel as TextChannel)
.has(perm)
) {
const _perms = (ctx.member as GuildMember).permissionsIn(ctx.channel as TextChannel);
if (!_perms.has(perm)) {
await ctx.reply({
embeds: [
sendEmbed(
response ??
`You are missing required permissions to run this command:\n${permsToString(
perm
)}`
),
sendEmbed(response ?? `You are missing required permissions to run this command:\n${permsToString(perm)}`),
],
ephemeral: !ctx.isMessage() ? true : false,
ephemeral: !ctx.isMessage(),
});
return controller.stop();
}
if (!(ctx.member! as GuildMember).permissionsIn(ctx.channel as TextChannel).any(perm)) {
if (!_perms.any(perm)) {
await ctx.reply({
embeds: [
sendEmbed(response ?? `You need at least one of the following permissions to run this command:\n${permsToString(perm)}`),
],
ephemeral: ctx.isMessage() ? false : true,
ephemeral: !ctx.isMessage(),
});
return controller.stop();
}
return controller.next();
});
}
function subGroups(opts: BaseOptions[]) {
return CommandControlPlugin<CommandType.Slash>(async ({ interaction }) => {
await no_guild(interaction);
const member = interaction.member as GuildMember;
const group = interaction.options.getSubcommandGroup();
return CommandControlPlugin<CommandType.Slash>(async (ctx) => {
if (ctx.guild === null) {
await ctx.reply({
content: "This command cannot be used in DM's!",
ephemeral: !ctx.isMessage(),
});
return controller.stop();
}
const member = ctx.member as GuildMember;
const group = ctx.options.getSubcommandGroup();
for (const opt of opts) {
if (group !== opt.name) {
await interaction.reply({
await ctx.reply({
embeds: [
sendEmbed(`[PLUGIN_permCheck.subGroups]: Failed to find specified subcommandGroup \`${opt.name}\`!`),
],
ephemeral: true,
ephemeral: !ctx.isMessage(),
});
return controller.stop();
}
const _perms = member.permissionsIn(
interaction.channel as TextChannel
);
const _perms = member.permissionsIn(ctx.channel as TextChannel);
if (opt.needAllPerms && !_perms.has(opt.perms)) {
await interaction.reply({
await ctx.reply({
embeds: [
sendEmbed(
opt.response ??
`You cannot use this group due to missing permissions: ${permsToString(
opt.perms
)}`
),
sendEmbed(opt.response ?? `You cannot use this group due to missing permissions: ${permsToString(opt.perms)}`),
],
ephemeral: true,
ephemeral: !ctx.isMessage(),
});
return controller.stop();
}
if (!opt.needAllPerms && !_perms.any(opt.perms)) {
await interaction.reply({
await ctx.reply({
embeds: [
sendEmbed(
opt.response ??
`You cannot use this group because you need at least one of the following permissions: ${permsToString(
opt.perms
)}`
),
sendEmbed(opt.response ?? `You cannot use this group because you need at least one of the following permissions: ${permsToString(opt.perms)}`),
],
ephemeral: true,
ephemeral: !ctx.isMessage(),
});
return controller.stop();
}
Expand All @@ -130,36 +119,42 @@ function subGroups(opts: BaseOptions[]) {
}

function subcommands(opts: BaseOptions[]) {
return CommandControlPlugin<CommandType.Slash>(async ({ interaction }) => {
await no_guild(interaction);
const member = interaction.member as GuildMember;
const sub = interaction.options.getSubcommand();
return CommandControlPlugin<CommandType.Slash>(async ({ ctx }) => {
if (ctx.guild === null) {
await ctx.reply({
content: "This command cannot be used in DM's!",
ephemeral: !ctx.isMessage(),
});
return controller.stop();
}
const member = ctx.member as GuildMember;
const sub = ctx.options.getSubcommand();
for (const { name, needAllPerms, perms, response } of opts) {
if (sub !== name) {
await interaction.reply({
await ctx.reply({
embeds: [
sendEmbed(`[PLUGIN_permCheck.subcommands]: Failed to find specified subcommand \`${name}\`!`),
],
ephemeral: true,
ephemeral: !ctx.isMessage(),
});
return controller.stop();
}
const _perms = member.permissionsIn(interaction.channel as TextChannel);
const _perms = member.permissionsIn(ctx.channel as TextChannel);
if (needAllPerms && !_perms.has(perms)) {
await interaction.reply({
await ctx.reply({
embeds: [
sendEmbed(response ?? `You cannot use this subcommand due to missing permissions: ${permsToString(perms)}`),
],
ephemeral: true,
ephemeral: !ctx.isMessage(),
});
return controller.stop();
}
if (!needAllPerms && !_perms.any(perms)) {
await interaction.reply({
await ctx.reply({
embeds: [
sendEmbed(response ?? `You cannot use this subcommand because you need at least the following permissions: ${permsToString(perms)}`),
],
ephemeral: true,
ephemeral: !ctx.isMessage(),
});
return controller.stop();
}
Expand All @@ -168,22 +163,26 @@ function subcommands(opts: BaseOptions[]) {
});
}
function options(opts: BaseOptions[]) {
return CommandControlPlugin<CommandType.Slash>(async ({ interaction }) => {
await no_guild(interaction);
const member = interaction.member as GuildMember;
const channel = interaction.channel as TextChannel;
return CommandControlPlugin<CommandType.Slash>(async ({ ctx }) => {
if (ctx.guild === null) {
await ctx.reply({
content: "This command cannot be used in DM's!",
ephemeral: !ctx.isMessage(),
});
return controller.stop();
}
const member = ctx.member as GuildMember;
const channel = ctx.channel as TextChannel;

for (const { name, needAllPerms, perms, response } of opts) {
const option = interaction.options.get(name);
const option = ctx.options.get(name);

if (option && option.name !== name) {
await interaction.reply({
embeds: [
sendEmbed(
`[PLUGIN_permCheck.options]: Could not find supplied option: \`${name}\``
),
await ctx.reply({
embeds: [
sendEmbed(`[PLUGIN_permCheck.options]: Could not find supplied option: \`${name}\``),
],
ephemeral: true,
ephemeral: !ctx.isMessage(),
});
return controller.stop();
}
Expand All @@ -192,27 +191,21 @@ function options(opts: BaseOptions[]) {

if (needAllPerms) {
if (!permissions.has(perms)) {
await interaction.reply({
await ctx.reply({
embeds: [
sendEmbed(
response ??
`You need all the following permissions for option \`${name}\`:\n ${permsToString(...perms)}`
),
sendEmbed(response ?? `You need all the following permissions for option \`${name}\`:\n ${permsToString(...perms)}`),
],
ephemeral: true,
ephemeral: !ctx.isMessage(),
});
return controller.stop();
}
} else {
if (!permissions.any(perms)) {
await interaction.reply({
await ctx.reply({
embeds: [
sendEmbed(
response ??
`You need at least one of the following permissions for option \`${name}\`: \n${permsToString(...perms)}`
),
sendEmbed(response ?? `You need at least one of the following permissions for option \`${name}\`: \n${permsToString(...perms)}`),
],
ephemeral: true,
ephemeral: !ctx.isMessage(),
});
return controller.stop();
}
Expand All @@ -229,16 +222,6 @@ interface BaseOptions {
response?: string;
}

const no_guild = async (interaction: ChatInputCommandInteraction) => {
if (interaction.guild === null) {
await interaction.reply({
content: "This command cannot be used in DM's!",
ephemeral: true,
});
return controller.stop();
}
};

const sendEmbed = (description: string) => {
const client = Service("@sern/client");
return new EmbedBuilder({
Expand All @@ -264,13 +247,13 @@ export function permCheck(
perm: PermissionResolvable,
response?: string
): ReturnType<typeof command>;

export function permCheck(
type: "subGroups" | "subcommands" | "options",
opts: BaseOptions[]
):
| ReturnType<typeof subGroups>
| ReturnType<typeof subcommands>
| ReturnType<typeof options>;
): | ReturnType<typeof subGroups>
| ReturnType<typeof subcommands>
| ReturnType<typeof options>;

export function permCheck(
type: "command" | "subGroups" | "subcommands" | "options",
Expand Down

0 comments on commit 17205d7

Please sign in to comment.