Skip to content
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

feat: fromCallback.ts #109

Merged
merged 2 commits into from
Jan 4, 2024
Merged
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
37 changes: 37 additions & 0 deletions plugins/fromCallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//@ts-nocheck
/**
* @plugin
* fromCallback turns a callback into a plugin result.
* if the callback returns truthy value, plugin continues.
* This control plugin works for every command type. The arguments of the callback
* mirror the execute method on the current module.
* @author @jacoobes [<@182326315813306368>]
* @version 1.0.0
* @example
* ```ts
* const myServer = "941002690211766332";
* export default commandModule({
* type: CommandType.Both,
* plugins: [
* //This plugin prevents this command module from executing in other servers except myServer
* fromCallback((ctx, args) => ctx.guildId == myServer)
* ],
* execute: ctx => {
* ctx.reply("I only respond in myServer!");
* }
* })
* ```
* @end
*/


import { PluginType, makePlugin, controller } from "@sern/handler";

export const fromCallback = (cb: (...args: any[]) => boolean) =>
makePlugin(PluginType.Control, (...args) => {
//console.log(args)
if(cb.apply(null, args)) {
return controller.next();
}
return controller.stop();
});