-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_manager.js
More file actions
122 lines (114 loc) · 4.45 KB
/
command_manager.js
File metadata and controls
122 lines (114 loc) · 4.45 KB
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
const {
Client,
Interaction,
} = require('discord.js');
const DebugManager = require('./beta_modules/DebugManager');
const fs = require('fs');
const path = require('path');
module.exports = class CommandManager {
#global_cmd_dic;
#guild_cmd_dic;
constructor () {
this.#global_cmd_dic = {};
this.#guild_cmd_dic = {};
}
/**
* コマンドのモジュールが書かれたファイルを指定のフォルダから取得
* @param {string} commandDirName コマンド格納フォルダ
* @param {string} guildInfoDirName サーバー情報格納フォルダ
*/
read_from_dir(commandDirName, guildInfoDirName) {
const filesOfProject = fs.readdirSync(commandDirName)
const pubCommandFiles = filesOfProject.filter(file => file.endsWith('.js'));
for (const file of pubCommandFiles) {
const command = require(`${commandDirName}/${file}`);
this.#global_cmd_dic[command.data.name] = command;
}
const infoFileName = "info.js";
const commandDirList = filesOfProject.filter(file => fs.statSync(path.join(commandDirName, file)).isDirectory())
for (const dir of commandDirList) {
const gldCommandFiles = fs.readdirSync(`${commandDirName}/${dir}`).filter(file => file.endsWith('.js') && file !== infoFileName);
if (fs.existsSync(`${guildInfoDirName}/${dir}.js`)) {
const info = require(`${guildInfoDirName}/${dir}.js`);
const gldCommands = {};
for (const file of gldCommandFiles) {
const command = require(`${commandDirName}/${dir}/${file}`);
gldCommands[command.data.name] = command;
}
this.#guild_cmd_dic[info.gldID] = gldCommands;
}
else {
console.log(`warning: The guild command is not set because there is no “${infoFileName}.js” in the ${guildInfoDirName} directory.`)
return false;
}
}
return true;
}
/**
* @param {Client} client クライアント
*/
async set(client) {
const glbCmdData = Object.values(this.#global_cmd_dic).map((command) => command.data);
await client.application.commands.set(glbCmdData);
//client.guilds.cache.forEach(async g => await client.application.commands.set(pubData, g.id));
for (const gldID in this.#guild_cmd_dic) {
const gldCommands = this.#guild_cmd_dic[gldID];
const gldCmdData = Object.values(gldCommands).map((command) => command.data);
await client.application.commands.set(gldCmdData, gldID);
}
console.log(client.application.commands);
}
/**
*
* @param {Client} client
* @param {Interaction} interaction
* @param {DebugManager} dbg_mnger
* @returns
*/
async execute(client, interaction, dbg_mnger) {
const commandName = interaction.commandName;
let command = null;
if (commandName in this.#global_cmd_dic) {
command = this.#global_cmd_dic[commandName];
}
else {
for (const [gldID, gldCmd] of Object.entries(this.#guild_cmd_dic)) {
if (commandName in gldCmd) {
command = this.#guild_cmd_dic[gldID][commandName];
break;
}
}
if (command == null) {
if (!dbg_mnger.isFrozen()){
await interaction.reply({
content: "The Command doesn't exit.",
ephemeral: true,
});
}
return false;
}
}
try {
if (command.meta) {
if (!dbg_mnger.isDebugging()) {
await interaction.reply({
content: "Trying The Meta Command.",
ephemeral: true,
});
}
await command.execute(client, interaction, dbg_mnger);
} else {
if (dbg_mnger.isFrozen()) return;
await command.execute(client, interaction);
}
} catch (error) {
console.error(error);
await interaction.reply({
content: 'There was an error while executing this command!',
ephemeral: true,
});
return false;
}
return true;
}
}