【问题标题】:Send message every x hrs on Discord in every group/guild在每个组/公会的 Discord 上每隔 x 小时发送一次消息
【发布时间】:2018-06-27 00:13:33
【问题描述】:

目前我正在开发一个事件机器人,该机器人用于 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);
})

问题:现在我收到一条错误消息,即 send 不是函数。

【问题讨论】:

  • 按组是指guild 还是server
  • 有区别吗?有人告诉我,每个 Discord-Group 都称为公会。有人称它为服务器,对吧?
  • @Xanar 你把它们命名为公会和服务器是正确的(至少不和谐)。你的问题中没有澄清的一点是你的消息应该发送到公会的哪个频道?否则,它非常简单。
  • 通常我希望消息在#general-channel 中。问题是,Discord 不再有默认房间,所以#general-channel 可能被称为 #the-lounge 左右。

标签: javascript discord discord.js


【解决方案1】:

只是一个简单的解决方案,可能不是最好的,但它是一个解决方案。

while(0 == 0) {
    channel = client.channels.cache.get('channel-id');
    channel.send("message you want to sent")
    sleep.sleep(seconds);
}

您将需要名为 sleep 的 npm 包

npm 安装睡眠

【讨论】:

    【解决方案2】:

    node.js 版本 12.x 更改了一些内容,要修复您的代码,您需要编辑一些内容:

      bot.guilds.cache.forEach((guild) => { //for each guild the bot is in
        let defaultChannel = "";
        guild.channels.cache.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
        }, 5000);})
    

    【讨论】:

      【解决方案3】:

      暂时忽略#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);
         })
      })
      

      这个代码应该发送给你的机器人所在的所有公会,你想要的消息和你想要的时间间隔,虽然可能不是你想要的频道

      【讨论】:

      • 太棒了!好吧,我可以通过添加类似 b!default 之类的命令来强制使用默认通道,然后强制机器人在该特定通道中写入。
      • 感谢您的建议,但现在我收到以下错误:ReferenceError: guilds is not defined
      • 完美运行!非常感谢!
      • 没问题!祝你机器人的其余部分好运!
      • 嗨,我正在尝试使用该代码,但我的版本是 12.x。有什么我想念的吗?
      猜你喜欢
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 2020-12-15
      • 2020-05-24
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      相关资源
      最近更新 更多