首页 文章

在每个组/公会的Discord上每隔x小时发送一条消息

提问于
浏览
0

目前我正在研究一个事件机器人,并且该机器人在Discord中的一些组中使用 . 所以现在我有这个代码:

if (command === "init")
  {
    //var d = new Date();
    //var n = d.getHours();
    message.channel.send("BunnBot starting...");
    var interval = setInterval (function () {
  message.channel.send("123")
  //message.channel.send(n);
}, 30 * 1000);
  }

问题是,此命令仅适用于当前组,意味着在任何其他组中我也必须使用init命令 .

我该如何解决?如何让我的Bot向每个群组发送消息?

编辑:好的,我稍微更改了代码,我现在正在使用它:好的,这是我的新代码,现在我遇到了错误信息,发送不是函数

client.on("ready", () => {
console.log('Logged in as BunnyBot');
setInterval (function () {
    client.guilds.forEach(() => { //for each guild the bot is in
        let defaultChannel = "";
        client.guilds.forEach((channel) => {
            if(channel.type == "text" && defaultChannel == "") {
           if(channel.permissionsFor(guild.me).has("SEND_MESSAGES")) {
               defaultChannel = channel;
           }
            }
        })
        message.defaultChannel.send("Message here"); //send it to whatever channel the bot has permissions to send on
        console.log("Sending Messages");
   })
}, 1 * 1000);
})

问题:现在我收到一条错误消息,发送不是函数 .

1 回答

  • 1

    暂时忽略 #general-channel 情况,你可以通过以下方式完成所有公会发布:

    编辑:

    const Discord = require("discord.js");
    const bot = new Discord.Client();
    bot.on("ready", () => {
        bot.guilds.forEach((guild) => { //for each guild the bot is in
             let defaultChannel = "";
             guild.channels.forEach((channel) => {
                   if(channel.type == "text" && defaultChannel == "") {
                   if(channel.permissionsFor(guild.me).has("SEND_MESSAGES")) {
                       defaultChannel = channel;
                   }
                   }
             })
             setInterval (function () {
                  defaultChannel.send("Message here") //send it to whatever channel the bot has permissions to send on
             }, 30 * 1000);
       })
    })
    

    这段代码应该发送到你的机器人所在的所有公会,你希望的消息你想要的间隔,虽然可能不是你想要的 the channel .

相关问题