我正在尝试发出公告命令,但出现错误

时间:2020-06-22 01:31:39

标签: bots discord discord.js

因此,基本上,我正在尝试使用message.mentions.channels将通知发送到特定频道
!announce #announcements Today is a great day!

错误:

(node:5516) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'channels' of undefined
    at Object.module.exports.execute (C:\Users\Zarko\Desktop\Stackoverflow\commands\Answers\announce.js:3:36)
    at Client.<anonymous> (C:\Users\Zarko\Desktop\Stackoverflow\events\message.js:20:30)
    at Client.emit (events.js:223:5)
    at MessageCreateAction.handle (C:\Users\Zarko\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\Zarko\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\Zarko\node_modules\discord.js\src\client\websocket\WebSocketManager.js:386:31)
    at WebSocketShard.onPacket (C:\Users\Zarko\node_modules\discord.js\src\client\websocket\WebSocketShard.js:436:22)
    at WebSocketShard.onMessage (C:\Users\Zarko\node_modules\discord.js\src\client\websocket\WebSocketShard.js:293:10)
    at WebSocket.onMessage (C:\Users\Zarko\node_modules\discord.js\node_modules\ws\lib\event-target.js:125:16)
    at WebSocket.emit (events.js:223:5)
(node:5516) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:5516) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

代码:

    let channel = message.mentions.channels.first();
    if (!channel) return 
    let announcement = args.slice(1).join(" ");
  
    channel.send(announcement)

1 个答案:

答案 0 :(得分:0)

错误是您传递变量的方式:

在您拥有的其他命令文件之一中:

execute(client, message, args) {
  //this works
}

在公告命令文件中,您拥有:

execute(message, args){
  console.log(message, args);

  /* =>
  Client {
   ...
  }

  Message {
   ...
  }
  */
}

因此,在这种情况下,message是实际的客户端对象,而args是实际的消息对象,因此只需修复可变点,

您在announce.js中的新执行函数

execute(client, message, args) {
  //rest of code here
}