I got an error when trying to delete all channels via a Discord bot

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

    message.guild.channels.forEach(channel => channel.delete())

ReferenceError: message is not defined
    at Client.<anonymous> (C:UserszanedDesktopbotmain.js:13:5)
    at Client.emit (node:events:390:28)
    at MessageCreateAction.handle (C:Userszanednode_modulesdiscord.jssrcclientactionsMessageCreate.js:25:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:Userszanednode_modulesdiscord.jssrcclientwebsockethandlersMESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:Userszanednode_modulesdiscord.jssrcclientwebsocketWebSocketManager.js:350:31)
    at WebSocketShard.onPacket (C:Userszanednode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:443:22)
    at WebSocketShard.onMessage (C:Userszanednode_modulesdiscord.jssrcclientwebsocketWebSocketShard.js:300:10)
    at WebSocket.onMessage (C:Userszanednode_moduleswslibevent-target.js:199:18)
    at WebSocket.emit (node:events:390:28)
    at Receiver.receiverOnMessage (C:Userszanednode_moduleswslibwebsocket.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?

Source: JavaSript – Stack Overflow

November 28, 2021
Category : News
Tags: discord | discord.js | javascript | node.js

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.