Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

328
Visualizações
Discord JS - importing a variable from another file's method

I have two commands: join and disconnect. Join function makes bot to join voice channel using method joinVoiceChannel, whilst disconnect command removes bot from the channel by using getVoiceConnection method:
join.js

const Command = require('../handlers/Command.js');
const { joinVoiceChannel } = require('@discordjs/voice');

module.exports = new Command({
    name: "join",
    description: "Joins voice channel",

    async run(message, args, client) {
        const channel = message.member.voice.channel;
        if (!channel) return message.channel.send('You need to be in a voice channel.');
        const permissions = channel.permissionsFor(message.client.user);
        if (!permissions.has('CONNECT') || !permissions.has('SPEAK')) return message.channel.send("You don't have the right permission.");

        const connection = joinVoiceChannel({
            channelId: channel.id,
            guildId: channel.guild.id,
            adapterCreator: channel.guild.voiceAdapterCreator,
        });
    }
});

disconnect.js

const Command = require('../handlers/Command.js');
const { getVoiceConnection } = require('@discordjs/voice');

module.exports = new Command({
    name: "disconnect",
    description: "Disconnects from the voice channel",

    async run(message, args, client) {
        try {
            const channel = message.member.voice.channel;
            const connection = getVoiceConnection(channel.guild.id);
            connection.destroy();
        } catch (error) { }
    }
});

Can I somehow import the connection constant from join.js to disconnect.js to avoid using other methods?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

2 simple ways to access connection without the need to export and require:

  1. Hoist to the Discord Client object. This way you can access connection as long as you have access to your Discord Client (typically by client or message.client). Of course this will only be available once the join.js command is executed atleast once, before that happens client.connection will be undefined
const connection = joinVoiceChannel({
   channelId: channel.id,
   guildId: channel.guild.id,
   adapterCreator: channel.guild.voiceAdapterCreator,
});

client.connection = connection;

// You can now use client.connection in any file that has client defined
  1. Hoist to the global object. Similar to #1, however the connection variable gets hoisted with the enviroment's global object which is accessible to all JavaScript files in the node enviroment.
const connection = joinVoiceChannel({
   channelId: channel.id,
   guildId: channel.guild.id,
   adapterCreator: channel.guild.voiceAdapterCreator,
});

global.connection = connection;
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda