【问题标题】:Send message to specific discord channel in discord.js v12在 discord.js v12 中向特定的不和谐频道发送消息
【发布时间】:2020-11-06 17:37:35
【问题描述】:

我尝试用于向特定频道发送消息的每一行代码在 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

【问题讨论】:

  • process.env.CHANNELANN 是 ID、名称还是其他?
  • 没关系,我现在可以使用client.channels.cache.get(process.env.CHANNELANN).send(announcement); 修复它但是,现在它只会不断发送垃圾邮件,并且不会只发送一次然后停止。你知道有什么解决办法吗?

标签: javascript discord discord.js


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    但是,现在它只是不断地发送垃圾邮件,而不是只发送一次然后停止。你知道有什么解决办法吗?

    一旦启动,让它等到停止流式传输

    【讨论】:

    • 我该怎么做?
    【解决方案3】:

    我可以通过将该行替换为:

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

    【讨论】:

      猜你喜欢
      • 2021-08-21
      • 2020-04-04
      • 2020-05-22
      • 2021-08-09
      • 2020-07-14
      • 2020-06-25
      • 2021-10-19
      • 2022-01-25
      相关资源
      最近更新 更多