簡體   English   中英

向 discord.js v12 中的特定 discord 通道發送消息

[英]Send message to specific discord channel in discord.js v12

我嘗試用來向特定頻道發送消息的每一行代碼似乎在 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

我可以通過將該行替換為:

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

但是,現在它只是不斷地發送垃圾郵件,並且不僅發送一次然后停止。 你知道有什么解決辦法嗎?

一旦啟動,讓它等到停止流式傳輸

我找到的答案都不適用於最新版本(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);
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM