Home > front end >  export and import discord.js event listeners
export and import discord.js event listeners

Time:12-10

I have this discord.js event listener:


client.on("ready", () => {

    const exampleEmbed = new EmbedBuilder()
        .setColor(0x0099FF)
        .setTitle(`${lib.welcome}`)
    client.channels.cache.get(someNumber).send({ embeds: [exampleEmbed] });

})

which is works fine. The question is how can I locate him in another file and export him into my main index.js file?

I had tried things like

module.exports = {
event: 
client.on("ready", () => {

    const exampleEmbed = new EmbedBuilder()
        .setColor(0x0099FF)
        .setTitle(`${lib.welcome}`)
    client.channels.cache.get(someNumber).send({ embeds: [exampleEmbed] });

})
}

and then require this file from the index.js module, but that didnt work

CodePudding user response:

In a JS file (i.e.: my_module.js), put the code below, together with the imports you need for EmbedBuilder:

exports.function_name = function (client){
  client.on("ready", () => {
    const exampleEmbed = new EmbedBuilder()
        .setColor(0x0099FF)
        .setTitle(`${lib.welcome}`)
    client.channels.cache.get(someNumber).send({ embeds: [exampleEmbed] });
  });
}

Then use:

var my_module = require('path to module')
const client = ...
my_module.function_name(client)

Where it says 'path to module', you need to give the path and the name of the file you created above. So if my_module.js is in the same folder as the file it using it then:

var my_module = require('./my_module.js')
  • Related