【问题标题】:How to a send message in a channel when the bot is ready?机器人准备好后如何在频道中发送消息?
【发布时间】:2020-09-02 23:29:11
【问题描述】:

我试图修改 discord.js 中的“乒乓”示例代码。
我不想登录控制台,而是想在 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');

【问题讨论】:

  • 请编辑!您的令牌在您的问题中。
  • 我在发布之前更改了令牌和频道 ID,所以这些都是但仍然感谢

标签: node.js bots discord discord.js


【解决方案1】:

简单的错误!您正在声明一个未定义的新 client 变量,因为 ready 事件不会使用任何值进行回调。 .get 也不是函数。你要找的是.fetch,这个函数会返回一个可以解析为通道的promise。这是您的新准备活动。

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

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

【讨论】:

    猜你喜欢
    • 2018-09-25
    • 2020-10-25
    • 2020-02-26
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 2019-03-27
    • 2020-09-25
    • 2016-05-03
    相关资源
    最近更新 更多