問題描述
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();
}
},
};
請幫助找出我的代碼中的錯誤!我想做一個機器人,每十分鐘在特定頻道發(fā)送一條消息(盡管在這種情況下,我每分鐘都放一次,這樣我就可以看看它是否有效)
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)
推薦答案
問題是你正在創(chuàng)建一個新的 Discord.Client()
實例,它不共享相同的頻道、成員、角色等作為原作.您應(yīng)該將原始的作為參數(shù)傳遞給您的 execute()
函數(shù),而不是創(chuàng)建新的 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')
它會完美運行!
這篇關(guān)于無法讀取未定義的屬性“發(fā)送"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!