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

320
Visualizações
Discord API v13 Error: ReferenceError: message is not defined

I was making a purge command delete all channels and ended up running into the error:

C:\Users\zaned\Desktop\bot\main.js:13
    message.guild.channels.forEach(channel => channel.delete())
    ^

ReferenceError: message is not defined
    at Client.<anonymous> (C:\Users\zaned\Desktop\bot\main.js:13:5)
    at Client.emit (node:events:390:28)
    at MessageCreateAction.handle (C:\Users\zaned\node_modules\discord.js\src\client\actions\MessageCreate.js:25:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\zaned\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\zaned\node_modules\discord.js\src\client\websocket\WebSocketManager.js:350:31)
    at WebSocketShard.onPacket (C:\Users\zaned\node_modules\discord.js\src\client\websocket\WebSocketShard.js:443:22)
    at WebSocketShard.onMessage (C:\Users\zaned\node_modules\discord.js\src\client\websocket\WebSocketShard.js:300:10)
    at WebSocket.onMessage (C:\Users\zaned\node_modules\ws\lib\event-target.js:199:18)
    at WebSocket.emit (node:events:390:28)
    at Receiver.receiverOnMessage (C:\Users\zaned\node_modules\ws\lib\websocket.js:1022:20)

Here's my code:

const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

client.on("ready", () => {
  console.log(`Logged in as ${client.user.tag}!`)
})

client.on("messageCreate", (msg) => {
  if (msg.content === "!test") {
    msg.reply("Hello world!");
  }
  if (msg.content === "!purgec") {
    message.guild.channels.forEach(channel => channel.delete())
    msg.reply("Deleting all channels...");
  }
})

Can somebody please help me with this?

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

0

This can easily get your bot rate limited and it is dangerously close to being a "nuking" bot which in some instances can be considered API abuse and get your Discord account and bot banned.

In your event callback the message variable is defined as msg and not message. Edit your code to reflect this.

Along with this the GuildChannelManager class doesnt have a #forEach function, for this you need to cache the channels.

client.on("messageCreate", (msg) => {
  if (msg.content === "!test") {
    msg.reply("Hello world!");
  }
  if (msg.content === "!purgec") {
    msg.guild.channels.cache.forEach(channel => channel.delete())
    msg.reply("Deleting all channels...");
  }
})

Another potential future bug is: If you've deleted every channel how will the bot reply to the message on the last line?

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