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

Add class to encapsulate event listeners #29

Merged
merged 25 commits into from
Feb 22, 2021
Merged

Add class to encapsulate event listeners #29

merged 25 commits into from
Feb 22, 2021

Conversation

eritbh
Copy link
Owner

@eritbh eritbh commented Oct 23, 2019

This will be easy for JS consumers but relies on microsoft/TypeScript#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 eritbh added enhancement something doesn't happen that should changes: api modifies behavior of the public API version: minor semver-minor, involves feature addition labels Oct 23, 2019
@eritbh eritbh changed the base branch from master to dev October 26, 2019 02:18
@eritbh eritbh added this to the future milestone Oct 26, 2019
@eritbh eritbh force-pushed the event-listener-class branch from 862fae0 to bc8cd1b Compare May 17, 2020 22:31
@eritbh
Copy link
Owner Author

eritbh commented May 17, 2020

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 [email protected] or greater because of typings updates in that release, so you'll also have to update Eris to use this branch.

@eritbh
Copy link
Owner Author

eritbh commented May 18, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changes: api modifies behavior of the public API enhancement something doesn't happen that should version: minor semver-minor, involves feature addition
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant