Search code examples
javascriptnode.jsdiscord.js

module.exports parameters in functions not getting detected by intellisense


So to showcase this problem, I wrote an example index.js and a commandExample.js that executes every time a message is sent by a user that isn't a bot. index.js contains the following code:

const { Client, IntentsBitField, Events} = require("discord.js");
const {token} = require("./config.json");
const cmd = require("./commandExample.js");

const client = new Client({intents: [IntentsBitField.Flags.GuildMembers, IntentsBitField.Flags.GuildMessages, IntentsBitField.Flags.Guilds, IntentsBitField.Flags.MessageContent]});
var prefix = ",";

client.on(Events.MessageCreate, (message) => {
    //checks if the message author is a bot or not
    if (message.author.bot) return;
    //passing the message parameter to the module.exports then executing the code
    cmd(message);

})



client.login(token);

as you can see we require the function inside the module.exports, but the problem is, for some reason intellisense doesn't autocomplete the message object's params, here's a screenshot of the problem.

it's supposed to give me the params and functions of the message object, but it doesn't (this has nothing to do with the param's name being "msg" in the commandExample.js and being "message" in the index.js file) so for example, if i do message. in the event listener, it gives me all the functions and params in the message object, but if i do msg. in the cmd function, it doesn't give me anything. making the event listener inside the cmd function is not practical at all because there is a max amount of them and manually setting the max amount of them sometimes just breaks the bot and makes it extremely slow. so yeah i'm wondering if somebody with more coding experience than me can give me a practical fix to this problem since it has been annoying me for a long time, thank you for reading this :D


Solution

  • Functions usually cannot directly infer parameter types, so you need to provide them using JSDoc or some other static type checker to get intellisense.

    /**
     * Your function description here
     * @param {import("discord.js").Message} msg
     */
    module.exports = (msg) => {
        // your code here
    }
    

    working example