将消息发送到discord.js v12中的特定Discord频道

时间:2020-07-16 23:42:19

标签: javascript discord discord.js

即使我从官方文档中获得了该代码,我尝试用于将消息发送到特定频道的每一行代码在discord.js v12中似乎也不起作用。这是我的代码:

const Discord = require('discord.js');
const client = new Discord.Client();
const announcementChannel = client.channels.cache.get(process.env.CHANNELANN);
let announcement = "MESSAGE HERE";

client.on("presenceUpdate", (oldPresence, newPresence) => {
  if (!newPresence.activities) return false;
  newPresence.activities.forEach(activity => {
      if (activity.type == "STREAMING") {
        if(newPresence.user.id == "THE ID IS HERE") {
          announcementChannel.send(announcement);
       }
      } else {
        client.user.setActivity("over the server", {
          type: "WATCHING"
        });
      };
  });
});
client.login(process.env.TOKEN);

这是我收到的错误:

          announcementChannel.send(announcement);
                              ^
TypeError: Cannot read property 'send' of undefined

3 个答案:

答案 0 :(得分:0)

我可以通过以下方式解决此问题:

client.channels.cache.get(process.env.CHANNELANN).send(announcement);

答案 1 :(得分:0)

但是,现在它只会继续发送垃圾邮件,并且不仅发送一次然后停止。您知道有什么解决办法吗?

启动后,请等到停止流播放

答案 2 :(得分:0)

我发现的所有答案都不适用于最新版本 (12.5.1),所以这对我有用。

            const channel = client.channels.cache.get(textChannelId) as TextChannel;
            if (channel) {
                const m = new Message(client, {id: clientId}, channel);
                const content: MessageOptions = {content: message};
                //if (image) {
                //    content.embed = {image: {url: image}};
                //}
                await m.channel.send(content);
            }