Cannot read property #39;send#39; 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();
}
},
};
请帮助找出我的代码中的错误!我想做一个机器人,每十分钟在特定频道发送一条消息(尽管在这种情况下,我每分钟都放一次,这样我就可以看看它是否有效)
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')
它会完美运行!
这篇关于无法读取未定义的属性“发送"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法读取未定义的属性“发送"
基础教程推荐
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
