/
ExtraClientDefaultHandlers.ts
134 lines (118 loc) · 4.88 KB
/
ExtraClientDefaultHandlers.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { Guild, Message, GuildMember } from 'discord.js';
import logger from '@greencoast/logger';
import { Command } from '../command/Command';
import { CommandGroup } from '../command/CommandGroup';
import { CommandTrigger } from '../../types';
import { SlashCommand } from '../command/SlashCommand';
/**
* The default event handlers for the custom events of {@link ExtendedClient}.
*/
export class ExtraClientDefaultHandlers {
/**
* Log that a data provider has been added to the client.
*/
public static onDataProviderAdd(): void {
logger.info('Added data provider to client.');
}
/**
* Log that the data provider has cleared the data for a guild or on the global scope.
* @param guild The [guild](https://old.discordjs.dev/#/docs/discord.js/main/class/Guild) for which
* the data has been cleared.
*/
public static onDataProviderClear(guild: Guild | null): void {
logger.warn(`Cleared data in data provider for ${guild?.name || 'global keys'}.`);
}
/**
* Log that the data provider has been initialized.
*/
public static onDataProviderInit(): void {
logger.info('DataProvider has been initialized.');
}
/**
* Log that the data provider has been destroyed.
*/
public static onDataProviderDestroy(): void {
logger.warn('DataProvider has been destroyed.');
}
/**
* Logged who executed a command and in which guild.
* @param command The command that was executed.
* @param trigger The [message](https://old.discordjs.dev/#/docs/discord.js/main/class/Message) or
* [interaction](https://old.discordjs.dev/#/docs/discord.js/main/class/ChatInputCommandInteraction) that
* triggered the command execution.
*/
public static onCommandExecute(command: Command<CommandTrigger>, trigger: CommandTrigger): void {
if (trigger instanceof Message) {
logger.info(`User ${trigger.member?.displayName || trigger.author.username} issued command ${command.name} in ${trigger.guild?.name || 'DM'}`);
} else {
const authorDisplayName = trigger.member instanceof GuildMember ?
trigger.member.displayName :
trigger.member?.nick;
logger.info(`User ${authorDisplayName || trigger.user.username} issued command ${command.name} in ${trigger.guild?.name || 'DM'}`);
}
}
/**
* Log that a command has thrown an error.
* @param error The error that was thrown in the command's run method.
* @param command The command that was executed.
* @param trigger The [message](https://old.discordjs.dev/#/docs/discord.js/main/class/Message) or
* [interaction](https://old.discordjs.dev/#/docs/discord.js/main/class/ChatInputCommandInteraction) that
* triggered the command execution.
*/
public static onCommandError(error: unknown, command: Command<CommandTrigger>, trigger: CommandTrigger): void {
logger.error(`Something happened when executing ${command.name} in ${trigger.guild?.name || 'DM'}.`);
if (trigger instanceof Message) {
logger.error(`Triggering message: ${trigger.content}`);
} else {
logger.error(`Triggering interaction had an ID of ${trigger.id}`);
}
logger.error(error);
}
/**
* Log that a group has been registered to the client.
* @param group The command group that was registered.
*/
public static onGroupRegistered(group: CommandGroup): void {
logger.info(`Registered ${group.name} (id: ${group.id}) command group.`);
}
/**
* Log that a command has been registered to the client.
* @param command The command that was registered.
*/
public static onCommandRegistered(command: Command<CommandTrigger>): void {
logger.info(`Registered ${command.name} in ${command.group?.name}.`);
}
/**
* Log that the client's presence has been updated.
* @param status The presence status.
*/
public static onPresenceUpdated(status: string): void {
logger.info(`Presence updated to: ${status}`);
}
/**
* Log the error that was thrown while updating the client's presence.
* @param error The error that was thrown.
*/
public static onPresenceUpdateError(error: unknown): void {
logger.error('Could not update presence!');
logger.error(error);
}
/**
* Log that the presence manager's refresh interval has been updated.
* @param interval The new presence manager's refresh interval.
*/
public static onPresenceRefreshInterval(interval: number | null): void {
if (!interval) {
logger.info('Refresh interval has been disabled.');
return;
}
logger.info(`Refresh interval updated, presence will be updated every ${interval}ms.`);
}
public static onCommandsDeployed(commands: SlashCommand[], guildID: string | null): void {
if (guildID) {
logger.info(`Successfully deployed ${commands.length} slash commands to ${guildID}`);
} else {
logger.info(`Successfully deployed ${commands.length} slash commands globally. This change may take up to 1 hour to take effect.`);
}
}
}