generated from sapphiredev/sapphire-template
-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
PluginPreCommandRun.ts
30 lines (25 loc) · 1.22 KB
/
PluginPreCommandRun.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { Listener, type PreconditionStore } from '@sapphire/framework';
import type { PieceContext } from '@sapphire/pieces';
import { PatternCommandEvents } from '../lib/utils/PaternCommandEvents';
import type { PatternPreCommandRunPayload } from '../lib/utils/PatternCommandInterfaces';
export class PreCommandRunListener extends Listener<typeof PatternCommandEvents.PreCommandRun> {
public constructor(context: PieceContext) {
super(context, { event: PatternCommandEvents.PreCommandRun });
}
public async run(payload: PatternPreCommandRunPayload) {
const { message, command } = payload;
// Run global preconditions:
const globalResult = await (this.container.stores.get('preconditions') as unknown as PreconditionStore).run(message, command, payload as any);
if (!globalResult.success) {
message.client.emit(PatternCommandEvents.CommandDenied, globalResult.error, payload);
return;
}
// Run command-specific preconditions:
const localResult = await command.preconditions.run(message, command, payload as any);
if (!localResult.success) {
message.client.emit(PatternCommandEvents.CommandDenied, localResult.error, payload);
return;
}
message.client.emit(PatternCommandEvents.CommandAccepted, payload);
}
}