2

I am trying to call the constant musicSettings stored in my ready.js file however, cannot seem to export it. In my ready.js file (this needs to be in there as this is one of the only places I can access this.client)

const { Listener } = require('discord-akairo');
const { Client: Lavaqueue } = require('lavaqueue');
const { ClientID } = require('../config');

class ReadyListener extends Listener {
    constructor() {
        super('ready', {
            emitter: 'client',
            eventName: 'ready'
        });
    }
    exec() {
        process.stdout.write(`[>] Running in ${this.client.guilds.size} servers.\n`);
    }
}

const musicSettings = new Lavaqueue({
    userID: ClientID,
    password: 'g6Z0xRLbiTHq',
    hosts: {
        rest: 'http://127.0.0.1:2333',
        ws: 'ws://127.0.0.1:2333',
        redis: { host: 'localhost' },
    },
    send(guildID, packet) {
        this.client.ws.send(packet)
    },
});

module.exports = ReadyListener;

Code located in the play.js file (does not work because I cannot import musicSettings from the ready.js file)

    async exec(message, args) {
        var channel = message.member.voiceChannel;
        if (!channel) {
            return message.reply('you need to be in a voice channel to play music.')
        } else if (!args.video) {
            return message.reply('you need to provide a link or search for a video.')
        }
        const song = await musicSettings.load(args.video);
        const queue = musicSettings.queues.get(`${message.guild.id}`);
        await queue.player.join(`${message.member.voiceChannel.id}`);
        await queue.add(...song.tracks.map(s => s.track));
        await queue.start();
    }
}
  • Looks like you just need to export `musicSettings` and then import it – CertainPerformance Aug 12 '19 at 23:52
  • Any idea how I can do this? I have tried using Object.freeze but seemed to have not worked –  Aug 12 '19 at 23:55
  • Possible duplicate of [How do I include a JavaScript file in another JavaScript file?](https://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file) – slothiful Aug 13 '19 at 13:59

1 Answers1

1

Export the musicSettings instance from ready.js:

module.exports = { ReadyListener, musicSettings };

And then import it in play.js:

const { musicSettings } = require('./ready');

And then you'll be able to reference musicSettings in play.js.

Note that since ready.js is now exporting two things, to import ReadyListener, you'll have to use the same sort of syntax as used to import musicSettings, eg:

const { ReadyListener } = require('./ready');

Or, if you want to import both at once:

const { ReadyListener, musicSettings } = require('./ready');
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320