[英]How do I send a message to a specific channel without a message object Discord.js
我正在尝试让我的机器人每两小时从.json
文件向特定频道发送一个随机问题。 它不在任何事件侦听器中,因此我没有用于发送消息的消息 object。
我试过用client.channels.cache.get('id')
定义频道,但这只是说.send
没有定义。 这是我当前的代码:
setTimeout(() => {
const quiz = require('./quiz.json');
const item = quiz[Math.floor(Math.random() * quiz.length)];
let channel = client.channels.cache.get('812178275463856128')
channel.send(item.question)
}, 7200000);
client.once('ready', async () => {
console.log('Bot is connected...');
const quiz = require('./quiz.json');
const channelID = '812178275463856128';
try {
const channel = await client.channels.fetch(channelID);
if (!channel || channel.type !== 'text')
return console.log(`Can't send message to this channel`);
setTimeout(async () => {
const item = quiz[Math.floor(Math.random() * quiz.length)];
channel.send(item.question);
}, 7200000);
} catch (error) {
console.log(error);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.