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

109
Visualizações
Validating a channel ID

So I have this strange problem. I am trying to validate that the channel ID exist in the guild and if it does then I want it to do something. But I just can't get it to work.

Normally bot.channels.cache.get(args[0]) would return all the channel info, but for whatever reason it always returns undefined and I don't understand why. (This was written in Discord.JS 1.12.5 not the latest version)

const mongoDB = require("../../utility/mongodbFramework");
const Discord = require("discord.js");

const bot = new Discord.Client();

module.exports = {
    name: "welcome",
    aliases: ["setwelcome", "welcome"],
    description: "Server config",
    args: true,
    maxArgs: 2,
    minArgs: 2,
    cooldown: 1,
    permissions: "ADMINISTRATOR",
    usage: "<channelId> <message>",
    async execute(message, args) {
        const { guild } = message;

        if (!bot.channels.cache.get(args[0]))
            return message.channel.send(
                "Send a valid channel ID. (You can use the `.id` command. Or you could use the Discord developer tools)"
            );

        const channelId = args[0];

        args.shift();
        welcomeMessage = args.join(" ");

        await mongoDB.setWelcome(guild.id, channelId, welcomeMessage);

        message.channel.send(`Channel ID: ${guild.id}, message: ${welcomeMessage}`);
    },
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem is your bot is not the one you've logged with so its cache will always be empty. Try to get the client from your message:

const mongoDB = require('../../utility/mongodbFramework');

module.exports = {
  name: 'welcome',
  aliases: ['setwelcome', 'welcome'],
  description: 'Server config',
  args: true,
  maxArgs: 2,
  minArgs: 2,
  cooldown: 1,
  permissions: 'ADMINISTRATOR',
  usage: '<channelId> <message>',
  async execute(message, args) {
    const { client, guild } = message;
    const channelId = args[0];

    if (!client.channels.cache.get(channelId))
      return message.channel.send(
        'Send a valid channel ID. (You can use the `.id` command. Or you could use the Discord developer tools)',
      );

    args.shift();
    
    const welcomeMessage = args.join(' ');
    await mongoDB.setWelcome(guild.id, channelId, welcomeMessage);

    message.channel.send(`Channel ID: ${guild.id}, message: ${welcomeMessage}`);
  },
};

PS: you could also try to fetch the channel by its ID instead of relying on the cache:

const channelId = args[0];
const channel = await client.channels.fetch(channelId);

if (!channel)
  return message.channel.send( /* ... */)

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