-
Notifications
You must be signed in to change notification settings - Fork 34
/
AppCommandRegister.ts
111 lines (105 loc) 路 3.48 KB
/
AppCommandRegister.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
import localize from '@i18n/localize';
import { ApplicationCommandManager, Message } from 'discord.js';
/**
* Manages application command used by the module.
*
* @author Utarwyn
* @since 2.2.0
*/
export default class AppCommandRegister {
/**
* Discord.js application command manager instance
* @private
*/
private readonly commandManager: ApplicationCommandManager;
/**
* Name of the application command to register
* @private
*/
private readonly name: string;
/**
* Name of the option to mention another user
* @private
*/
private readonly optionName: string;
/**
* Constructs application command registration handler.
*
* @param commandManager discord.js client instance
* @param name application name to register
* @param optionName name of the option to mention another user
*/
constructor(commandManager: ApplicationCommandManager, name: string, optionName: string) {
this.commandManager = commandManager;
this.name = name;
this.optionName = optionName;
}
/**
* Handles messages to deploy or delete
* the application command in a specific guild.
*
* @param message discord.js message object
*/
public async handleDeployMessage(message: Message): Promise<void> {
if (
message.guild &&
message.member &&
message.client.user &&
message.mentions.has(message.client.user) &&
message.member.permissions.has('ADMINISTRATOR')
) {
const words = message.content.split(' ');
if (words.length === 2) {
if (words.includes('tttdeploy')) {
await this.registerInGuild(message.guild.id);
await message.reply(`Command /${this.name} has been registered.`);
} else if (words.includes('tttdelete')) {
const executed = await this.deleteInGuild(message.guild.id);
if (executed) {
await message.reply(`Command /${this.name} has been unregistered.`);
} else {
await message.reply(`Command /${this.name} not found.`);
}
}
}
}
}
/**
* Registers the application command into a guild.
*
* @param guildId guild identifier which will receive the command
* @private
*/
private async registerInGuild(guildId: string): Promise<void> {
await this.commandManager.create(
{
name: this.name,
description: localize.__('command.description'),
options: [
{
type: 'USER',
name: this.optionName,
description: localize.__('command.option-user')
}
]
},
guildId
);
}
/**
* Deletes the application command from a guild.
*
* @param guildId guild identifier in which the command will be deleted
* @private
*/
private async deleteInGuild(guildId: string): Promise<boolean> {
const commands = await this.commandManager.fetch({ guildId });
const command = commands?.find(cmd => cmd.name === this.name);
if (command) {
await this.commandManager.delete(command.id, guildId);
return true;
} else {
return false;
}
}
}