Skip to content

Commit eedd7fc

Browse files
committed
fix: require() => import()
1 parent 57d277d commit eedd7fc

2 files changed

Lines changed: 78 additions & 79 deletions

File tree

index.js

Lines changed: 77 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import { REST } from "@discordjs/rest";
22
import { readdirSync } from "node:fs";
33
import path from "node:path";
44
const Routes = {
5-
commands: (appId) => {
6-
return `/applications/${appId}/commands`;
7-
},
8-
command: (appId, cmdId) => {
9-
return `/applications/${appId}/commands/${cmdId}`;
10-
},
11-
guildCommands: (appId, guildId) => {
12-
return `/applications/${appId}/guilds/${guildId}/commands`;
13-
},
14-
guildCommand: (appId, guildId, cmdId) => {
15-
return `/applications/${appId}/guilds/${guildId}/commands/${cmdId}`;
16-
},
5+
commands: (appId) => {
6+
return `/applications/${appId}/commands`;
7+
},
8+
command: (appId, cmdId) => {
9+
return `/applications/${appId}/commands/${cmdId}`;
10+
},
11+
guildCommands: (appId, guildId) => {
12+
return `/applications/${appId}/guilds/${guildId}/commands`;
13+
},
14+
guildCommand: (appId, guildId, cmdId) => {
15+
return `/applications/${appId}/guilds/${guildId}/commands/${cmdId}`;
16+
},
1717
};
1818
/**
1919
* Create, update and delete global and guild application commands.
@@ -22,78 +22,77 @@ const Routes = {
2222
* Otherwise the check for a guild ID is omitted, and you could make pointless requests which can also result in an error
2323
*/
2424
export async function deployCommands(folderPath, opts) {
25-
var _a;
26-
if (!opts.appToken || !opts.appId) {
27-
throw new Error("Missing 'appToken' or 'appId' in 'opts'!");
28-
}
29-
let commands = [];
30-
let privateCommands = [];
31-
const commandFiles = readdirSync(folderPath).filter((file) =>
32-
file.endsWith(".js")
33-
);
34-
if (opts.logs)
35-
console.log(`🔁 Started refreshing global and guild commands.`);
36-
try {
37-
const rest = new REST().setToken(opts.appToken);
38-
for (const file of commandFiles) {
39-
const filePath = path.join(folderPath, file);
40-
const command = require(filePath);
41-
if (!("data" in command)) {
42-
console.error(
43-
`- Command '${command.name}' is missing the 'data' property!`
44-
);
45-
continue;
46-
} else if (
47-
"data" in command &&
48-
Boolean((_a = command.ignore) !== null && _a !== void 0 ? _a : false)
49-
) {
50-
if (opts.logs)
51-
console.log(`- Command '${command.data.name}' is ignored!`);
52-
continue;
53-
}
54-
if ((command.guildIds || []).length > 0) {
55-
privateCommands.push({
56-
data: command.data,
57-
guildIds: command.guildIds,
58-
});
59-
} else {
60-
commands.push(command.data);
61-
}
25+
var _a;
26+
if (!opts.appToken || !opts.appId) {
27+
throw new Error("Missing 'appToken' or 'appId' in 'opts'!");
6228
}
63-
let data = await rest.put(Routes.commands(opts.appId), {
64-
body: commands,
65-
});
66-
if (opts.logs) console.log(`✅ ${data.length} global commands refreshed`);
67-
for (let cmd of privateCommands) {
68-
for (let gid of cmd.guildIds) {
69-
data = null;
70-
data = await rest.post(Routes.guildCommands(opts.appId, gid), {
71-
body: cmd.data,
29+
let commands = [];
30+
let privateCommands = [];
31+
const commandFiles = readdirSync(folderPath).filter((file) => file.endsWith(".js"));
32+
if (opts.logs)
33+
console.log(`🔁 Started refreshing global and guild commands.`);
34+
try {
35+
const rest = new REST().setToken(opts.appToken);
36+
for (const file of commandFiles) {
37+
const filePath = path.join(folderPath, file);
38+
const command = (await import(filePath)).default;
39+
if (!("data" in command)) {
40+
console.error(`- Command '${command.name}' is missing the 'data' property!`);
41+
continue;
42+
}
43+
else if ("data" in command && Boolean((_a = command.ignore) !== null && _a !== void 0 ? _a : false)) {
44+
if (opts.logs)
45+
console.log(`- Command '${command.data.name}' is ignored!`);
46+
continue;
47+
}
48+
if ((command.guildIds || []).length > 0) {
49+
privateCommands.push({
50+
data: command.data,
51+
guildIds: command.guildIds,
52+
});
53+
}
54+
else {
55+
commands.push(command.data);
56+
}
57+
}
58+
let data = await rest.put(Routes.commands(opts.appId), {
59+
body: commands,
7260
});
73-
if (opts.logs) console.log(`✅ Guild command '${data.name}' refreshed`);
74-
}
61+
if (opts.logs)
62+
console.log(`✅ ${data.length} global commands refreshed`);
63+
for (let cmd of privateCommands) {
64+
for (let gid of cmd.guildIds) {
65+
data = null;
66+
data = await rest.post(Routes.guildCommands(opts.appId, gid), {
67+
body: cmd.data,
68+
});
69+
if (opts.logs)
70+
console.log(`✅ Guild command '${data.name}' refreshed`);
71+
}
72+
}
73+
return true;
74+
}
75+
catch (err) {
76+
console.error("❌ Error while refreshing commands:", err);
77+
return false;
7578
}
76-
return true;
77-
} catch (err) {
78-
console.error("❌ Error while refreshing commands:", err);
79-
return false;
80-
}
8179
}
8280
/**
8381
* Shortcut method to delete an application command by its ID. **The client needs to be logged in!**
8482
*/
8583
export async function deleteCommand(commandId, opts) {
86-
var _a;
87-
const guildId = (_a = opts.guildId) !== null && _a !== void 0 ? _a : null;
88-
const commandPath = guildId
89-
? Routes.guildCommand(opts.appId, guildId, commandId)
90-
: Routes.command(opts.appId, commandId);
91-
if (commandId.match(/^\d+$/i)) {
92-
await new REST({ version: "10" })
93-
.setToken(opts.appToken)
94-
.delete(commandPath);
95-
} else {
96-
throw new Error("'commandId' is not a only-number-string!");
97-
}
98-
return;
84+
var _a;
85+
const guildId = (_a = opts.guildId) !== null && _a !== void 0 ? _a : null;
86+
const commandPath = guildId
87+
? Routes.guildCommand(opts.appId, guildId, commandId)
88+
: Routes.command(opts.appId, commandId);
89+
if (commandId.match(/^\d+$/i)) {
90+
await new REST({ version: "10" })
91+
.setToken(opts.appToken)
92+
.delete(commandPath);
93+
}
94+
else {
95+
throw new Error("'commandId' is not a only-number-string!");
96+
}
97+
return;
9998
}

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export async function deployCommands(
5050

5151
for (const file of commandFiles) {
5252
const filePath = path.join(folderPath, file);
53-
const command = require(filePath);
53+
const command = (await import(filePath)).default;
5454
if (!("data" in command)) {
5555
console.error(
5656
`- Command '${command.name}' is missing the 'data' property!`

0 commit comments

Comments
 (0)