mirror of
https://github.com/sern-handler/awesome-plugins
synced 2026-06-27 18:22:17 +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
@@ -29,30 +29,35 @@
|
||||
import { CommandControlPlugin, CommandType, controller } from "@sern/handler";
|
||||
import type { ModalSubmitInteraction } from "discord.js";
|
||||
|
||||
type Assertion =
|
||||
| RegExp
|
||||
| ((value : string) => boolean);
|
||||
type Assertion = RegExp | ((value: string) => boolean);
|
||||
|
||||
export function assertFields(config: {
|
||||
fields: Record<string, Assertion>,
|
||||
failure: (errors: string[], interaction: ModalSubmitInteraction) => any
|
||||
fields: Record<string, Assertion>;
|
||||
failure: (errors: string[], interaction: ModalSubmitInteraction) => any;
|
||||
}) {
|
||||
return CommandControlPlugin<CommandType.Modal>(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: string) => 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();
|
||||
})
|
||||
}
|
||||
return CommandControlPlugin<CommandType.Modal>((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: string) => 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