Skip to content

Guards Update #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/guards/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@spark.ts/guards",
"version": "1.1.0",
"version": "2.0.0",
"description": "A plugin for @spark.ts/handler which adds conditions to commands that have to be passed in order for the command to run.",
"main": "dist/index.js",
"scripts": {
Expand Down
106 changes: 88 additions & 18 deletions packages/guards/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { CommandPlugin, CommandType, Command } from '@spark.ts/handler';
import { ChatInputCommandInteraction, Message } from 'discord.js';
import { Interaction, Message, MessageReplyOptions, InteractionReplyOptions, MessagePayload, Collection } from 'discord.js';
import type { ReadonlyCollection } from "@discordjs/collection";

type Immutable<T> = T extends {} ? {
readonly [P in keyof T as T[P] extends (...args: any[]) => unknown ? never : P]: Immutable<T[P]>;
} : T;
type Immutable<T> = T extends Map<infer K,infer V>
? ReadonlyMap<K,Immutable<V>>
: T extends Array<infer U>
? ReadonlyArray<Immutable<U>>
: T extends Collection<infer K,infer V>
? ReadonlyCollection<K,Immutable<V>>
: T extends object ? {
readonly [P in keyof T as T[P] extends (...args: any[]) => unknown ? never : P]: Immutable<T[P]>;
} : T;

export type Helper = (args: {
command: Command,
interaction: Immutable<ChatInputCommandInteraction>,
interaction: Immutable<Interaction>,
message: Immutable<Message>;
like: Immutable<Interaction | Message>;
}) => unknown;

interface Conditional {
Expand All @@ -20,7 +28,12 @@ interface Conditional {
/**
* How to reply to the user if the condition fails.
*/
onFalse?: string;
onFalse?: string | (<Slash extends boolean>(isSlash: Slash) => (Slash extends true ? InteractionReplyOptions : MessageReplyOptions | MessagePayload) | string);

/**
* If `onFalse` is specified and the command is an interaction than it makes the response ephemeral automatically. Overrides `onFalse` ephemeral.
*/
ephemeral?: boolean;
}

/**
Expand Down Expand Up @@ -48,20 +61,33 @@ export function Guard(...conditionals: Conditional[]): CommandPlugin<CommandType
for (const conditional of conditionals) {
let { condition } = conditional;
if (typeof conditional.condition === 'function') {
condition = conditional.condition({ command, message, interaction });
condition = conditional.condition({ command, message, interaction, like: {...message, ...interaction} });
}

if (!condition) {
if ('onFalse' in conditional) {
if (command.type === CommandType.Slash) interaction?.reply(conditional.onFalse!);
else if (command.type === CommandType.Text) message?.reply(conditional.onFalse!);
if(typeof conditional.onFalse === "string")
(command.type === CommandType.Slash ? interaction : message)?.reply(conditional.onFalse);
else if (command.type === CommandType.Slash){
const result = conditional.onFalse<true>(true);

if(typeof result === "string"){
interaction?.reply({
content: result,
ephemeral: conditional.ephemeral ? true : false
});
} else interaction?.reply({
...result,
ephemeral: conditional.ephemeral ? true : false
});
} else if (command.type === CommandType.Text) message?.reply(conditional.onFalse<false>(false));
}
return controller.stop();
}
}

return controller.next();
},
}
};
}

Expand All @@ -71,37 +97,61 @@ export const Helpers = {
* @param guilds Provided list of guild ids.
*/
InGuild(guilds: string[]): Helper {
return ({ command, message, interaction }) => guilds.includes((command.type === 'slash' ? interaction : message).guildId!);
return ({ like }) => guilds.includes(like.guildId!);
},

/**
* Checks if the id of the channel the command is used in is in a provided list of channel ids.
* @param channels Provided list of channel ids.
*/
InChannel(channels: string[]): Helper {
return ({ command, message, interaction }) => channels.includes((command.type === 'slash' ? interaction : message).channelId!);
return ({ like }) => channels.includes(like.guildId!);
},

/**
* Checks if the id of the user who used the command is in a provided list of user ids.
* @param users Provided list of user ids.
*/
ByUser(users: string[]): Helper {
return ({ command, message, interaction }) => users.includes((command.type === "slash" ? interaction : message).member?.user.id!);
return ({ like }) => users.includes(like.member?.user.id!);
},

/**
* Checks if the user who used the command is a discord bot or not.
*/
IsBot(): Helper {
return ({ command, message, interaction }) => (command.type === "slash" ? interaction : message).member?.user.bot!;
return ({ like }) => like.member?.user.bot!;
},

/**
* Checks if the command was used in a discord server or not.
*/
InServer(): Helper {
return ({ command, message, interaction }) => (command.type === "slash" ? interaction : message).guild !== null;
return ({ like }) => like.guild !== null;
},

/**
* Checks if the command is an application command or not.
*/
IsApplicationCommand(): Helper {
return ({ command }) => command.type === CommandType.Slash;
},

/**
* Checks the type of the interaction.
* @deprecated This helper is an implementation for a future version of `@sparkts/handler`
* @param type The type of interaction to check for. Is converted to InteractionType.
*/
InteractionIsTypeOf(type: "ping" | "slash" | "component" | "autocomplete" | "modal"): Helper {
const conversions = {
ping: 1,
slash: 2,
component: 3,
autocomplete: 4,
modal: 5
}

return ({ interaction }) => interaction.type === conversions[type];
},

/**
Expand Down Expand Up @@ -133,13 +183,33 @@ export const Helpers = {
* plugins: [
* Guard(
* {
* condition: Helpers.Or(Helpers.IsBot(),Helpers.InServer())
* condition: Helpers.Or(Helpers.IsBot(),Helpers.InServer()) // dont use `Helpers.IsBot() || Helpers.InServer()`
* }
* )
* ]
* });
*/
Or(helper: Helper,val: Helper | unknown): Helper {
return (o) => helper(o) || (typeof val === "function" ? val(o) : val);
},

/**
* Accepts a helper function and another argument that can also be a helper or any other type and compares them.
* @param helper Helper function to compare.
* @param val Either a value or a helper function.
* @example
* export default new SparkCommand({
* description: "...",
* plugins: [
* Guard(
* {
* condition: Helpers.EqualTo(Helpers.IsBot(),Helpers.InServer())
* }
* )
* ]
* });
*/
Or(x: Helper,y: Helper): Helper {
return (o) => x(o) || y(o);
EqualTo(helper: Helper,val: Helper | unknown): Helper {
return (o) => helper(o) === (typeof val === "function" ? val(o) : val);
}
}