So my problem is that despite using async functions, my code will still behave like synchronous code. And so some commands are not passing through or are greatly delayed which they clearly shouldn't.
This is the code being executed on the "interactionCreate" event:
const Discord = require("discord.js");
/**
*
* @param {Discord.Client} client
* @param {Discord.Interaction} interaction
* @returns
*/
module.exports = async (client, interaction) => {
if (interaction.isCommand()) {
const { commandName } = interaction;
const command = client.commands.get(commandName);
if (command) command.execute(client, interaction);
}
}
And here is a command:
const Discord = require('discord.js');
const util = require('util');
const search = util.promisify(require('g-i-s'));
const Colors = require("../src/colors");
module.exports = {
name: "salmon",
description: "Sends a random picture of a salmon",
/**
*
* @param {Discord.Client} client
* @param {Discord.CommandInteraction} interaction
*/
async execute(client, interaction) {
await interaction.deferReply();
const results = await search("salmon");
const number = Math.floor(Math.random() * results.length)
const embed= new Discord.MessageEmbed()
.setColor(Colors.PINK)
.setTitle(`Salmon number ${number}.`)
.setImage(results[number].url);
interaction.editReply({ embeds: [embed] });
}
}
Executing the code above will create "lag" on other commands. Which would not really be an issue for me normally but this it impacting the entire code, thus using a "Play music" command very unstable as each time someone will use a command being a little load heavy, an audible lag will be heard.
I had done test with a more heavy load to make the lag stay longer and the bot was not proceeding new commands. Although I feel like I did not do anything wrong. Why is my async execute function not behaving like an async function? Or am I missing something ?
Maybe you should try to use execute async instead of async execute