Search code examples
javascriptdiscorddiscord.jsconstants

DiscordJS | How do I create a "responses" page?


I am VERY new to DiscordJS (and JS in general).

How exactly do I call a new .js page, and execute from it?

For example, here is code which will eventually have a HUGE list of potential responses. I'd like to put these responses (or the whole module?) on a separate page, for organization sake. How would I do this?

Currently, this is all just in my index:

client.on("messageCreate", async (message) => {
  let args = message.content.trim().split(" ");
  if (message.author.id === "1234567890") {
    const member = await message.guild.members.fetch(message.author.id);
    const responses = [
      "Response 1",
      "Response 2",
      "Response 3",
      "Response 4",
    ];
    const reply = responses[Math.floor(Math.random() * responses.length)];
    if (Math.random() < 0.15) {
      //.50 = 50% chance

      message.reply(`${reply}`);
    }
  }
});

Thank you for any help/pointers!


Solution

  • Create your file and export your array from it. Then require that array in your main file.

    File responses.js

    const responses = [
       "Response 1",
       "Response 2",
       "Response 3",
       "Response 4",
    ];
    
    export default responses;
    

    Main file

    import responses from "path to responses.js"
    

    If you're not using ESM, use module.exports and require:

    module.exports = {
       responses
    };
    
    //...
    
    const { responses } = require("path to responses.js");