Ju
Ju
DIAdiscord.js - Imagine an app
Created by Ju on 10/21/2023 in #djs-questions
Async datas in command.data (SlashCommand)
Hey everyone ! I'm trying to register that new one command, actually that I want is to pass "interaction" in data with async method (code bellow). For example, I want to get guild ID (then from interaction) to do some Prisma requests with that guild ID parameter and then update one dropdown select with results (from database). But I get errors when reload my bot (error bellow too), any informations ? Thanks 🙂
// Works
module.exports = {
data: new SlashCommandBuilder()
.setName("test")
.setDescription("Show test page with commands"),
async execute(interaction) {
return interaction.reply("coucou");
},
};

// ----------------------------

// Not working
module.exports = {
data: async (interaction) => {
// Do something "interaction"..
return await new SlashCommandBuilder()
.setName("test")
.setDescription("Show test page with commands");
},
async execute(interaction) {
return interaction.reply("coucou");
},
};
// Works
module.exports = {
data: new SlashCommandBuilder()
.setName("test")
.setDescription("Show test page with commands"),
async execute(interaction) {
return interaction.reply("coucou");
},
};

// ----------------------------

// Not working
module.exports = {
data: async (interaction) => {
// Do something "interaction"..
return await new SlashCommandBuilder()
.setName("test")
.setDescription("Show test page with commands");
},
async execute(interaction) {
return interaction.reply("coucou");
},
};
G:\AAA\BBB\CCCC\DDD\index.js:48
commands.push(command.data.toJSON());
^

TypeError: command.data.toJSON is not a function
at Object.<anonymous> (G:\AAA\BBB\CCC\DDD\index.js:48:30)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain]
...
G:\AAA\BBB\CCCC\DDD\index.js:48
commands.push(command.data.toJSON());
^

TypeError: command.data.toJSON is not a function
at Object.<anonymous> (G:\AAA\BBB\CCC\DDD\index.js:48:30)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain]
...
There is my index.js file (with command register) :
client.commands = new Collection();
const commands = [];

// Commands
const commandFiles = fs
.readdirSync("./commands")
.filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.data.name, command);
commands.push(command.data.toJSON());
}
client.commands = new Collection();
const commands = [];

// Commands
const commandFiles = fs
.readdirSync("./commands")
.filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.data.name, command);
commands.push(command.data.toJSON());
}
9 replies