简体   繁体   English

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

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

Every line of code i try to use to send a message to a specific channel doesn't seem to work in discord.js v12 even when i got that code from the official documentation.我尝试用来向特定频道发送消息的每一行代码似乎在 discord.js v12 中不起作用,即使我从官方文档中获得了该代码。 Here is my code:这是我的代码:

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);

Here is the error i receive:这是我收到的错误:

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

I was able to fix this by replacing that line with:我可以通过将该行替换为:

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

However, now it just keeps spamming the message and doesn't only send it once then stops.但是,现在它只是不断地发送垃圾邮件,并且不仅发送一次然后停止。 You know any fix for that?你知道有什么解决办法吗?

Once initiated, make it wait until the stop streaming一旦启动,让它等到停止流式传输

None of the answers I found worked with the latest version (12.5.1) so here's what worked for me.我找到的答案都不适用于最新版本(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