繁体   English   中英

机器人准备就绪后如何在频道中发送消息?

[英]How to a send message in a channel when the bot is ready?

我试图修改 discord.js 中的“ping pong”示例代码。
我不想登录控制台,而是想在 Discord 的特定频道中发送消息。我尝试使用下面的代码来执行此操作,但它一直显示此错误:

(node:10192) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'channels' of undefined
    at Client.<anonymous> (D:\GAMES\DiscordBot\index.js:7:12)

这是我正在使用的代码:

const Discord = require('discord.js');
const client = new Discord.Client();

console.log(client);

client.on('ready', client => {
  client.channels.get('787667808777998851').send('Hello here!');
});

client.on('message', msg => {
  if (msg.content === 'ping') {
    msg.reply('pong');
  }
});

client.login('auth-token');

简单的错误! 您正在声明一个未定义的新client变量,因为就绪事件不会使用任何值进行回调。 此外, .get不是 function。您要查找的是.fetch ,而此 function 返回解析为频道的 promise。 这是您新的就绪事件。

client.on('ready', () => {
    client.channels.fetch('787667808777998851')
    .then(channel => {
        channel.send("Hello here!");
    })
});

另外,尽量不要在您的问题中发布您的机器人令牌。 这是非常危险的,任何人都可以访问您的机器人。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM