【问题标题】:Trying to do scheduled message with cron but getting 'send' of undefined error尝试使用 cron 发送预定消息但收到未定义错误的“发送”
【发布时间】:2021-01-31 16:45:33
【问题描述】:

我正在尝试在 Discord.js v12 上使用 cron 发送预定消息。但我收到此错误:

channel.send("测试"); ^

TypeError: 无法读取未定义的属性“发送”

我的代码是这样的:

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

const channel = client.channels.cache.get('channelidhere');
const job = new cron.CronJob('0 0 12 * * *', () => {
  channel.send("test");
});

job.start();

client.login("token")

【问题讨论】:

  • 将您的代码移动到客户端的就绪事件中。这应该可以解决问题。
  • 您的频道 ID 错误,或者您试图在频道缓存之前获取该频道。正如@Jakye 指出的那样,您应该将它放在客户的ready 事件中,这样它只会在所有内容都被缓存并且......好吧,准备好时触发。
  • 我做到了,错误消失了,但机器人完全静默现在尝试更改 channel.send 到 console.log 仍然静默。
  • 静默是什么意思?
  • 我的意思是ready事件不起作用,当我把ready事件放在它上面时命令不起作用

标签: discord.js


【解决方案1】:

你可以试试:

 const channel = member.guild.channels.cache.find(
    (channel) => channel.id === "ChannelIdHere"
  );

【讨论】:

    猜你喜欢
    • 2021-01-18
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 2014-02-23
    • 1970-01-01
    相关资源
    最近更新 更多