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());
}
4 Replies
d.js toolkit
d.js toolkit15mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
space
space15mo ago
This is not possible. Depending on what you want to do, there might be a way. It sounds like you want to look into autocomplete options I think.
Spam
Spam15mo ago
It's because you're doing data not data() Also, await new? ☠️ And why are you passing interaction in data? The interaction does not exist in the command data Physically impossible
Ju
JuOP15mo ago
Oh, I'll look back my code when i'm home, thanks 🙂
Want results from more Discord servers?
Add your server