简体   繁体   English

尝试通过 Discord 机器人删除所有频道时出现错误

[英]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:我正在执行清除命令删除所有通道并最终遇到错误:

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?有人可以帮我吗?

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.这可以很容易地限制您的机器人速率,并且它非常接近成为“核攻击”机器人,在某些情况下可以被视为 API 滥用并让您的 Discord 帐户和机器人被禁止。

In your event callback the message variable is defined as msg and not message .在您的事件回调中,消息变量被定义为msg而不是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.除此之外, GuildChannelManager class 没有#forEach function,为此您需要缓存通道。

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?另一个潜在的未来错误是:如果您删除了每个频道,机器人将如何回复最后一行的消息?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我在 discord.js-commando 中发出了一个命令来宣布我的机器人所在的所有频道。但我收到了一个错误 - I made a command in discord.js-commando to anounce all the channels my bot is in. but I got an error 运行 discord.js 机器人时出现此错误 - I got this error when I runned discord.js bot discord bot - 使用 discord.js v13 删除 Discord 服务器中的所有频道 - discord bot - delete all channels in a discord server using discord.js v13 如何删除公会 discord.js 中的所有频道 - How do I delete all channels in a guild discord.js Discord bot 获取所有渠道中的用户数量 - Discord bot get number of users in all channels 尝试为我的 discord 机器人发出禁令命令,但是当我运行命令时,我总是收到错误 - Trying to make a ban command for my discord bot but when i run the command i always get an error 当我尝试删除票证时,discord bot 崩溃 - discord bot crashing when i try to delete ticket 发送后 5 秒尝试在 discord.js 中删除 discord 机器人的消息,收到错误 - Trying to delete the message of a discord bot in discord.js 5 seconds after sending, getting an error 覆盖 Discord bot 的权限以将所有文本/语音频道中的角色静音 - Overwrite permissions for discord bot to mute a role in all text/voice channels discord.js BOT - 创建具有权限的频道时出错 - discord.js BOT - ERROR Creating Channels with Permissions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM