首页 > 解决方案 > 每次我要进行更新时,如何让我的机器人发送一条消息,以便我的客户知道?(discord.js)

问题描述

我需要知道如何做到这一点,所以每当我对我的机器人进行更新时,它会在一个名为公告的频道中向每个服务器发送一条消息,然后它让每个人都知道它正在更新并且会有停机时间?如果有人知道帮助,请告诉我!

标签: discorddiscord.js

解决方案


您可以通过检查您的机器人所在的每个服务器是否有一个名为“公告”的频道,然后向该频道发送消息来实现这一点。

这假设您的机器人已在名为 的变量下初始化client

// Set our message. This can also be an Attachment or RichEmbed
var message = "Going offline due to updates! Be back in 10 minutes.";

// For all the servers this bot is in,
client.guilds.forEach((guild) => {
    // Look for a channel named "announcements".
    var channel = guild.channels.find(channel => channel.name === "announcements");
    // Make sure that "channel" is not null (i.e. it exists)
    if (channel !== null)
        // Send our update message.
        channel.sendMessage(message);
});

请注意,如果您的机器人是公开的并且位于您无法控制的其他服务器上,则不建议这样做,因为它完全是无端的,并且可能会激怒某些服务器所有者。


推荐阅读