繁体   English   中英

如何在没有消息的情况下向特定频道发送消息 object Discord.js

[英]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);

尝试获取频道并通过访问type属性检查它是否是文本频道:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM