eritbh / yuuko

🔰 No-frills Discord command framework for Eris
http://eritbh.me/yuuko
MIT License
17 stars 2 forks source link

Add class to encapsulate event listeners #29

Closed eritbh closed 3 years ago

eritbh commented 5 years ago

This will be easy for JS consumers but relies on https://github.com/microsoft/TypeScript/issues/32164 for typings to be right for TS consumers. Will also need client changes to accomodate registering event listeners in this manner.

This branch also includes the fix for #32, namely that there is now an option to disable the default command handler, allowing you to define your own message handling flow and choose whether or not to execute commands on a per-message basis separately from command requirements. This is useful for implementing blacklists or command cooldowns.

eritbh commented 4 years ago

No longer relies on that TS issue, because I just copy-pasted Eris's event typings into the EventListener definition and converted them to constructor signatures.

Methods for loading commands from files have been expanded to work with event listeners as well, and have also been renamed to reflect that (though the old names are still there as deprecated aliases, to be removed in the next semver-major release).

All in all, this should be usable as:

// someListener.ts
import {EventListener} from 'yuuko';
export default new EventListener('messageReactionAdd', (message, emoji, userID) => {
    // All parameters have their types properly inferred
});

// client.ts
import {Client} from 'yuuko';
const bot = new Client({...});
bot.addFile('/path/to/someListener.ts');
// alternatively: `bot.addCommandFile('/path/to/someListener.ts');` will also work
bot.connect();

Install via github:Geo1088/yuuko#builds/event-listener-class to test. This is built on the latest dev which requires eris@0.13.0 or greater because of typings updates in that release, so you'll also have to update Eris to use this branch.

eritbh commented 4 years ago

Last few commits make the EventListener class pass a context object as an additional parameter to every event type. This applies only to EventListener instances; listeners registered via client.on() will not receive the context parameter.

These commits also split the command handling section of the code into its own function, and provides an option to disable all default message handling so you can define a completely custom process and choose which messages you want to process commands from. For example:

import {Client, EventListener} from 'yuuko';

const bot = new Client({
    disableDefaultMessageListener: true, // new option
    ...
});

bot.on('messageCreate', message => {
    // Filter out some messages
    if (someBlacklist.includes(message.author.id)) return;
    // Look for commands in messages that didn't get filtered - this call will execute any commands
    bot.processCommand(message);
});

bot.connect();

Note that when disableDefaultMessageListener is true, the ignoreBots client option will have no effect. You can instead ignore bots yourself from within your custom message listener.