mirror of
https://github.com/sern-handler/awesome-plugins
synced 2026-06-11 18:32:24 +00:00
chore: Update JavaScript plugins (#69)
chore: update JavaScript plugins Co-authored-by: EvolutionX-10 <EvolutionX-10@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
98ab449203
commit
b2ee0996f1
61
JavaScript/assertFields.js
Normal file
61
JavaScript/assertFields.js
Normal file
@@ -0,0 +1,61 @@
|
||||
//@ts-nocheck
|
||||
|
||||
/**
|
||||
* This plugin checks the fields of a ModalSubmitInteraction
|
||||
* with regex or a custom callback
|
||||
*
|
||||
* @author @jacoobes [<@182326315813306368>]
|
||||
* @version 1.0.0
|
||||
* @example
|
||||
* ```ts
|
||||
* export default commandModule({
|
||||
* type: CommandType.Modal,
|
||||
* plugins: [
|
||||
* assertFields({
|
||||
* fields: {
|
||||
* // check the modal field "mcUsernameInput" with the regex /a+b+c/
|
||||
* mcUsernameInput: /a+b+c+/
|
||||
* },
|
||||
* failure: (errors, interaction) => {
|
||||
* interaction.reply(errors.join("\n"))
|
||||
* }
|
||||
* }),
|
||||
* ],
|
||||
* execute: ctx => {
|
||||
* ctx.reply("nice!")
|
||||
* }
|
||||
* })
|
||||
* ```
|
||||
*/
|
||||
import { CommandControlPlugin, controller } from "@sern/handler";
|
||||
export function assertFields(config) {
|
||||
return CommandControlPlugin((modal) => {
|
||||
const pairs = Object.entries(config.fields);
|
||||
const errors = [];
|
||||
|
||||
for (const [field, assertion] of pairs) {
|
||||
// Keep in mind this doesn't check for typos!
|
||||
// feel free to add more checks.
|
||||
const input = modal.fields.getTextInputValue(field);
|
||||
const resolvedAssertion =
|
||||
assertion instanceof RegExp
|
||||
? (value) => assertion.test(value)
|
||||
: assertion;
|
||||
|
||||
if (!resolvedAssertion(input)) {
|
||||
errors.push(
|
||||
input +
|
||||
" failed to pass assertion " +
|
||||
resolvedAssertion.toString()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (errors.length > 0) {
|
||||
config.failure(errors, modal);
|
||||
return controller.stop();
|
||||
}
|
||||
|
||||
return controller.next();
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user