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:
github-actions[bot]
2023-01-28 23:47:35 +05:30
committed by GitHub
parent 98ab449203
commit b2ee0996f1
24 changed files with 876 additions and 917 deletions

View File

@@ -15,14 +15,14 @@
* type : CommandType.Both
* plugins: [confirmation()],
* execute: (ctx, args) => {
* ctx.reply('hola');
* ctx.interaction.followUp('Hello welcome to the secret club')
* }
* })
* ```
*/
import { PluginType } from "@sern/handler";
import { CommandControlPlugin, controller } from "@sern/handler";
const defaultOptions = {
timeout: 1000,
timeout: 5000,
message: "Are you sure you want to proceed?",
onTimeout: "confirmation timed out",
onCancel: "confirmation cancelled",
@@ -38,83 +38,78 @@ const defaultOptions = {
};
export function confirmation(raw = {}) {
const options = Object.assign({}, defaultOptions, raw);
return {
name: "confirmation",
type: PluginType.Event,
return CommandControlPlugin(async (context, _) => {
if (typeof options.message === "function") {
options.message = await options.message(context);
}
async execute([context], controller) {
if (typeof options.message === "function") {
options.message = await options.message(context);
const response = await context.reply(await options.message);
let { yes, no } = options.emojis;
if (typeof yes === "function") {
yes = await yes(context);
}
if (typeof no === "function") {
no = await no(context);
}
await response.react(await yes);
await response.react(await no);
function filter(reaction, user) {
return (
([yes, no].includes(reaction.emoji.name) ||
[yes, no].includes(reaction.emoji.identifier)) &&
user.id === context.user.id
);
}
const recieved = await response.awaitReactions({
filter,
max: 1,
time: options.timeout,
});
if (recieved.size === 0) {
if (typeof options.onTimeout === "function") {
await options.onTimeout(context, response);
} else {
await response.edit(await options.onTimeout);
await response.reactions.removeAll();
}
const response = await context.reply(await options.message);
let { yes, no } = options.emojis;
return controller.stop();
}
if (typeof yes === "function") {
yes = await yes(context);
}
const reaction = recieved.first();
if (typeof no === "function") {
no = await no(context);
}
if (!reaction) {
return controller.stop();
}
await response.react(await yes);
await response.react(await no);
function filter(reaction, user) {
return (
([yes, no].includes(reaction.emoji.name) ||
[yes, no].includes(reaction.emoji.identifier)) &&
user.id === context.user.id
);
}
const recieved = await response.awaitReactions({
filter,
max: 1,
time: options.timeout,
});
if (recieved.size === 0) {
if (typeof options.onTimeout === "function") {
await options.onTimeout(context, response);
switch (reaction.emoji.name) {
case await yes:
if (typeof options.onConfirm === "function") {
await options.onConfirm(context, response);
} else {
await response.edit(await options.onTimeout);
await response.edit(await options.onConfirm);
await response.reactions.removeAll();
}
return controller.next();
case await no:
if (typeof options.onCancel === "function") {
await options.onCancel(context, response);
} else {
await response.edit(await options.onCancel);
await response.reactions.removeAll();
}
return controller.stop();
}
}
const reaction = recieved.first();
if (!reaction) {
return controller.stop();
}
switch (reaction.emoji.name) {
case await yes:
if (typeof options.onConfirm === "function") {
await options.onConfirm(context, response);
} else {
await response.edit(await options.onConfirm);
await response.reactions.removeAll();
}
return controller.next();
case await no:
if (typeof options.onCancel === "function") {
await options.onCancel(context, response);
} else {
await response.edit(await options.onCancel);
await response.reactions.removeAll();
}
return controller.stop();
}
return controller.next();
},
};
return controller.next();
});
}