无法读取未定义的属性“发送"

2022-01-10 00:00:00 cron discord node.js javascript discord.js
const mudaeon = require('./mudaetime.json');
const cron = require('cron');
const Discord = require('discord.js');
const client = new Discord.Client();

module.exports = {
 name: 'mudaetime',
 description: '...',
 execute(message, args) {
  if (mudaeon) {
   const channel = client.channels.cache.get('id');
   let scheduledMessage = new cron.CronJob(
    '* * * * *',
    () => {
     scheduledMessage.start();
    },
    message.react('✅'),
    channel.send('check $tu ! <@&id')
   );
  } else !mudaeon;
  {
   cancel();
  }
 },
};

请帮助找出我的代码中的错误!我想做一个机器人,每十分钟在特定频道发送一条消息(尽管在这种情况下,我每分钟都放一次,这样我就可以看看它是否有效)

Please help find the error in my code! I wanted to make a bot that sends a message in a specific channel every ten minutes (although in this case, I put it for every minute so I can see if it works)

推荐答案

问题是你正在创建一个新的 Discord.Client() 实例,它不共享相同的频道、成员、角色等作为原作.您应该将原始的作为参数传递给您的 execute() 函数,而不是创建新的 Discord.Client().

The problem is that you are creating a new Discord.Client() instance, which does not share the same channels, members, roles, etc. as the original. Instead of creating a new Discord.Client(), you should pass the original one as an argument to your execute() function.

例如,您可以将 async execute(message, args){ 更改为 async execute(message, args, client){.然后,在您的命令处理程序中,将 command.execute(message, args) 更改为 command.execute(message, args, client)

For example, you could change async execute(message, args){ to async execute(message, args, client){. Then, in your command handler, change command.execute(message, args) to command.execute(message, args, client)

然而,还有一个更更简单的方法.client实际上是message对象的一个​​有效属性,指的是:

However, there is an even easier way. client is actually a valid property of the message object, referring to:

实例化消息的客户端

(Message#client 文档)

所以,不要写:

const channel = client.channels.cache.get('id');

你可以写:

const channel = message.client.channels.cache.get('id')

它会完美运行!

相关文章