Skip to content

Commit

Permalink
chore(guards): styling & release new guards
Browse files Browse the repository at this point in the history
  • Loading branch information
DuroCodes committed Feb 3, 2023
1 parent 3c7e513 commit b4522ef
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-masks-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@spark.ts/guards": minor
---

Add new helpers: `ByUser`, `IsBot`, `InServer`. Adds utility helpers: `Or` and `Not`
18 changes: 10 additions & 8 deletions packages/guards/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,26 @@ export const Helpers = {
* @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 ({ command, message, interaction }) => users.includes((command.type === 'slash' ? interaction : message).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 ({ command, message, interaction }) => (command.type === 'slash' ? interaction : message).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 ({ command, message, interaction }) => (command.type === 'slash' ? interaction : message).guild !== null;
},

/**
* Accepts a helper function and returns true if the condition doesn't pass. Mimics `!` operator.
* Accepts a helper function and returns true if the condition doesn't pass.
* Mimics `!` operator.
* @param condition Helper function to test.
* @example
* export default new SparkCommand({
Expand All @@ -124,7 +125,8 @@ export const Helpers = {
},

/**
* Accepts 2 helper functions and if one turns out to be true then the condition passes. Mimics `||` operator.
* Accepts 2 helper functions and if one turns out to be true then the condition passes.
* Mimics `||` operator.
* @param x First helper function to test.
* @param y Second helper function to test.
* @example
Expand All @@ -139,7 +141,7 @@ export const Helpers = {
* ]
* });
*/
Or(x: Helper,y: Helper): Helper {
Or(x: Helper, y: Helper): Helper {
return (o) => x(o) || y(o);
}
}
},
};

0 comments on commit b4522ef

Please sign in to comment.