无法读取属性不足的属性“发送" [英] Cannot read property 'send' of underfined
问题描述
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();
}
},
};
请帮助查找我的代码中的错误!我想制作一个每10分钟在特定频道中发送一条消息的机器人(尽管在这种情况下,我每分钟都放置一条消息,以便可以看到它是否起作用)
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()
实例,该实例不共享相同的频道,成员,角色等作为原始内容.代替创建新的 Discord.Client()
,您应该将原始参数作为参数传递给 execute()
函数.
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:
实例化消息的客户端
The client that instantiated the message
因此,与其写作:
const channel = client.channels.cache.get('id');
您可以这样写:
const channel = message.client.channels.cache.get('id')
它将完美运行!
这篇关于无法读取属性不足的属性“发送"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!