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')
它会完美运行!
这篇关于无法读取未定义的属性“发送"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法读取未定义的属性“发送"


- Flexslider 箭头未正确显示 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Fetch API 如何获取响应体? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01