Skip to content

Commit e7ce612

Browse files
authored
refactor: rename Event to Listener (#218)
BREAKING CHANGE: Renamed `Event` to `Listener` BREAKING CHANGE: Renamed `EventStore` to `ListenerStore` BREAKING CHANGE: Changed events directory from `events` to `listeners` BREAKING CHANGE: Renamed `SapphireClientOptions.loadDefaultErrorEvents` to `loadDefaultErrorListeners` BREAKING CHANGE: Renamed `StoreRegistryEntries.events` to `StoreRegistryEntries.listeners` BREAKING CHANGE: Refactored `Events` enum to be an object, so we can use discord.js's constants BREAKING CHANGE: Renamed `Events.EventError` to `Events.ListenerError` BREAKING CHANGE: Renamed `EventErrorPayload` to `ListenerErrorPayload` BREAKING CHANGE: Renamed `Events.Ready` to `Events.ClientReady` BREAKING CHANGE: Renamed `Events.Message` to `Events.MessageCreate`
1 parent 49b463e commit e7ce612

21 files changed

+159
-157
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Sapphire is a Discord bot framework built on top of [discord.js] for advanced an
2424
## Features
2525

2626
- Written in TypeScript
27-
- Command Handler, Arguments, Pre-conditions and Events Store
27+
- Command Handler, Arguments, Pre-conditions and Listeners Store
2828
- Completely Modular and Extendable
2929
- Advanced Plugins Support
3030
- Supports many [plugins](https://github.com/sapphiredev/plugins)

guides/getting-started/CreatingEvents.md renamed to guides/getting-started/CreatingListeners.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
# Creating events
1+
# Creating listeners
22

33
## Setting up the file structure
44

5-
Sapphire loads events the same way as it loads command, by automatically reading files in the `events` folder. You
6-
should now create such a folder, and add your first event in it.
5+
Sapphire loads events the same way as it loads command, by automatically reading files in the `listeners` folder. You
6+
should now create such a folder, and add your first listeners in it.
77

8-
### Ready event
8+
### Ready listener
99

10-
To create an event you simply need to create a file with the name of the event, in our case `ready.js`.
10+
To create an listener you simply need to create a file with the name of the listener, in our case `ready.js`.
1111

12-
Each event is a class that extends from the base `Event` class from Sapphire. You need to extend it, and add a `run`
13-
method which works the same way as for commands.
12+
Each listener is a class that extends from the base `Listener` class from Sapphire. You need to extend it, and add a
13+
`run` method which works the same way as for commands.
1414

1515
```javascript
16-
const { Event } = require('@sapphire/framework');
16+
const { Listener } = require('@sapphire/framework');
1717

18-
module.exports = class extends Event {
18+
module.exports = class extends Listener {
1919
constructor(context) {
2020
super(context, {
2121
once: true
@@ -29,14 +29,14 @@ module.exports = class extends Event {
2929
```
3030

3131
The first parameter of `super` is the context, that is given in the constructor. You can then specify an
32-
{@link EventOptions} object, containing properties such as the emitter's, the event's name, and
33-
whether it should only be run once (`once`). The emitter defaults to the the client, and the event name default to the
34-
file name.
32+
{@link ListenerOptions} object, containing properties such as the emitter's, the listener's name, and
33+
whether it should only be run once (`once`). The emitter defaults to the the client, and the listener name default to
34+
the file name.
3535

3636
The `run` method is the method that gets called when the event occurs. It takes whatever the events gives as argument.
3737
In our case, the ready events gives no information so we don't need any parameter.
3838

39-
Every piece (events, commands etc) in sapphire has a `context` which can be accessed via
39+
Every piece (listeners, commands etc) in sapphire has a `container` which can be accessed via
4040
`this.container`. It is this container that contains the logger, the client and other properties. Here we access the
4141
logger via `this.container.logger` and call its `log` method to print a nicely formatted message in the console.
4242

guides/getting-started/GettingStarted.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ You won't have any feedback on your console, and the bot won't respond to any ev
5656
## Preparing your file structure
5757

5858
You can now organize your project correctly by grouping files together in folders. This will allow it to scale more
59-
easily as you add commands, events and other features.
59+
easily as you add commands, listeners, and other features.
6060
The main folder, at the root of your project and containing all your code can be named `src` (for "source"). You can put
6161
your `main.js` file inside this `src` directory and check that your bot still boots when you run
6262

src/errorEvents/CoreEventError.ts

-14
This file was deleted.

src/errorEvents/CoreCommandError.ts renamed to src/errorListeners/CoreCommandError.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { PieceContext } from '@sapphire/pieces';
2-
import { Event } from '../lib/structures/Event';
2+
import { Listener } from '../lib/structures/Listener';
33
import { CommandErrorPayload, Events } from '../lib/types/Events';
44

5-
export class CoreEvent extends Event<Events.CommandError> {
5+
export class CoreEvent extends Listener<typeof Events.CommandError> {
66
public constructor(context: PieceContext) {
77
super(context, { event: Events.CommandError });
88
}

src/errorListeners/CoreEventError.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { PieceContext } from '@sapphire/pieces';
2+
import { Listener } from '../lib/structures/Listener';
3+
import { ListenerErrorPayload, Events } from '../lib/types/Events';
4+
5+
export class CoreEvent extends Listener<typeof Events.ListenerError> {
6+
public constructor(context: PieceContext) {
7+
super(context, { event: Events.ListenerError });
8+
}
9+
10+
public run(error: Error, context: ListenerErrorPayload) {
11+
const { name, event, path } = context.piece;
12+
this.container.logger.error(`Encountered error on event listener "${name}" for event "${event}" at path "${path}"`, error);
13+
}
14+
}

src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ export * from './lib/structures/Argument';
2727
export * from './lib/structures/ArgumentStore';
2828
export * from './lib/structures/Command';
2929
export * from './lib/structures/CommandStore';
30-
export * from './lib/structures/Event';
31-
export * from './lib/structures/EventStore';
30+
export * from './lib/structures/Listener';
31+
export * from './lib/structures/ListenerStore';
3232
export * from './lib/structures/ExtendedArgument';
3333
export * from './lib/structures/Precondition';
3434
export * from './lib/structures/PreconditionStore';

src/lib/SapphireClient.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { Plugin } from './plugins/Plugin';
66
import { PluginManager } from './plugins/PluginManager';
77
import { ArgumentStore } from './structures/ArgumentStore';
88
import { CommandStore } from './structures/CommandStore';
9-
import { EventStore } from './structures/EventStore';
9+
import { ListenerStore } from './structures/ListenerStore';
1010
import { PreconditionStore } from './structures/PreconditionStore';
1111
import { StoreRegistry } from './structures/StoreRegistry';
1212
import { PluginHook } from './types/Enums';
@@ -108,7 +108,7 @@ export interface SapphireClientOptions {
108108
* @since 1.0.0
109109
* @default true
110110
*/
111-
loadDefaultErrorEvents?: boolean;
111+
loadDefaultErrorListeners?: boolean;
112112
}
113113

114114
/**
@@ -237,9 +237,9 @@ export class SapphireClient extends Client {
237237
this.stores
238238
.register(new ArgumentStore().registerPath(join(__dirname, '..', 'arguments'))) //
239239
.register(new CommandStore())
240-
.register(new EventStore().registerPath(join(__dirname, '..', 'events')))
240+
.register(new ListenerStore().registerPath(join(__dirname, '..', 'listeners')))
241241
.register(new PreconditionStore().registerPath(join(__dirname, '..', 'preconditions')));
242-
if (options.loadDefaultErrorEvents !== false) this.stores.get('events').registerPath(join(__dirname, '..', 'errorEvents'));
242+
if (options.loadDefaultErrorListeners !== false) this.stores.get('listeners').registerPath(join(__dirname, '..', 'errorListeners'));
243243

244244
for (const plugin of SapphireClient.plugins.values(PluginHook.PostInitialization)) {
245245
plugin.hook.call(this, options);

src/lib/structures/EventStore.ts

-8
This file was deleted.

src/lib/structures/Event.ts renamed to src/lib/structures/Listener.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,44 @@ import { Events } from '../types/Events';
55

66
/**
77
* The base event class. This class is abstract and is to be extended by subclasses, which should implement the methods. In
8-
* Sapphire's workflow, events are called when the emitter they listen on emits a new message with the same event name.
8+
* Sapphire's workflow, listeners are called when the emitter they listen on emits a new message with the same event name.
99
*
1010
* @example
1111
* ```typescript
1212
* // TypeScript:
13-
* import { Event, Events, PieceContext } from '@sapphire/framework';
13+
* import { Events, Listener, PieceContext } from '@sapphire/framework';
1414
*
15-
* // Define a class extending `CoreEvent`, then export it.
15+
* // Define a class extending `Listener`, then export it.
1616
* // NOTE: You can use `export default` or `export =` too.
17-
* export class CoreEvent extends Event<Events.Ready> {
17+
* export class CoreListener extends Listener<typeof Events.Ready> {
1818
* public constructor(context: PieceContext) {
1919
* super(context, { event: Events.Ready, once: true });
2020
* }
2121
*
2222
* public run() {
23-
* if (!this.container.client.id) this.container.client.id = this.container.client.user?.id ?? null;
23+
* this.container.client.id ??= this.container.client.user?.id ?? null;
2424
* }
2525
* }
2626
* ```
2727
*
2828
* @example
2929
* ```javascript
3030
* // JavaScript:
31-
* const { Event, Events } = require('@sapphire/framework');
31+
* const { Events, Listener } = require('@sapphire/framework');
3232
*
33-
* // Define a class extending `CoreEvent`, then export it.
34-
* module.exports = class CoreEvent extends Event {
33+
* // Define a class extending `Listener`, then export it.
34+
* module.exports = class CoreListener extends Listener {
3535
* constructor(context) {
3636
* super(context, { event: Events.Ready, once: true });
3737
* }
3838
*
3939
* run() {
40-
* if (!this.container.client.id) this.container.client.id = this.container.client.user?.id ?? null;
40+
* this.container.client.id ??= this.container.client.user?.id ?? null;
4141
* }
4242
* }
4343
* ```
4444
*/
45-
export abstract class Event<E extends keyof ClientEvents | symbol = ''> extends Piece {
45+
export abstract class Listener<E extends keyof ClientEvents | symbol = ''> extends Piece {
4646
public readonly emitter: EventEmitter | null;
4747
public readonly event: string;
4848
public readonly once: boolean;
@@ -85,7 +85,7 @@ export abstract class Event<E extends keyof ClientEvents | symbol = ''> extends
8585
try {
8686
await this.run(...args);
8787
} catch (error) {
88-
this.container.client.emit(Events.EventError, error, { piece: this });
88+
this.container.client.emit(Events.ListenerError, error, { piece: this });
8989
}
9090
}
9191

src/lib/structures/ListenerStore.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Store } from '@sapphire/pieces';
2+
import { Listener } from './Listener';
3+
4+
export class ListenerStore extends Store<Listener> {
5+
public constructor() {
6+
super(Listener as any, { name: 'listeners' });
7+
}
8+
}

src/lib/structures/StoreRegistry.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getRootData, Piece, Store } from '@sapphire/pieces';
33
import { join } from 'path';
44
import type { ArgumentStore } from './ArgumentStore';
55
import type { CommandStore } from './CommandStore';
6-
import type { EventStore } from './EventStore';
6+
import type { ListenerStore } from './ListenerStore';
77
import type { PreconditionStore } from './PreconditionStore';
88

99
type Key = keyof StoreRegistryEntries;
@@ -40,14 +40,14 @@ export class StoreRegistry extends Collection<Key, Value> {
4040
* /home/me/my-bot
4141
* ├─ src
4242
* │ ├─ commands
43-
* │ ├─ events
43+
* │ ├─ listeners
4444
* │ └─ main.js
4545
* └─ package.json
4646
* ```
4747
*
48-
* And you run `node src/main.js`, the directories `/home/me/my-bot/src/commands` and `/home/me/my-bot/src/events` will
49-
* be registered for the commands and events stores respectively, since both directories are located in the same
50-
* directory as your main file.
48+
* And you run `node src/main.js`, the directories `/home/me/my-bot/src/commands` and `/home/me/my-bot/src/listeners`
49+
* will be registered for the commands and listeners stores respectively, since both directories are located in the
50+
* same directory as your main file.
5151
*
5252
* **Note**: this also registers directories for all other stores, even if they don't have a folder, this allows you
5353
* to create new pieces and hot-load them later anytime.
@@ -95,6 +95,6 @@ export interface StoreRegistry {
9595
export interface StoreRegistryEntries {
9696
arguments: ArgumentStore;
9797
commands: CommandStore;
98-
events: EventStore;
98+
listeners: ListenerStore;
9999
preconditions: PreconditionStore;
100100
}

0 commit comments

Comments
 (0)