【问题标题】:My discord bot won't stop spamming a respond to my command我的不和谐机器人不会停止向我的命令发送垃圾邮件
【发布时间】:2021-09-09 10:31:13
【问题描述】:

我向我的机器人添加了//search 命令。但是当我尝试运行它时,它给了我在哪里搜索的选项,但过了一会儿,它开始发送垃圾邮件,每次我输入频道时,它也会响应。我什至还没有回答这个问题,它立即开始回复并给我硬币。有没有办法解决这个问题?我已经多次扫描代码,没有发现错别字或错误;事实上,登录我的控制台也没有错误。

如果需要代码,这里:

const profileModel = require("../models/profileSchema");

module.exports = {
  name: "search",
  aliases: [],
  permissions: [],
  cooldowns: 30,
  description: "Search for some coin!",
  async execute(message, args, cmd, client, Discord, profileData) {
    const locations = [
      "Dragonspine",
      "Windrise",
      "Qingyun Peak",
      "Mt. Hulao",
      "Mondstadt City",
      "Springvale",
      "Kamisato Estate",
      "Guyun Stone Forest",
      "Fort Mumei",
      "Watatsumi Island",
    ];

    const chosenLocations = locations
      .sort(() => Math.random() - Math.random())
      .slice(0, 3);

    const filter = ({ author, content }) =>
      message.author == author &&
      chosenLocations.some(
        (location) => location.toLowerCase() == content.toLowerCase()
      );

    const collector = message.channel.createMessageCollector(filter, {
      max: 1,
      time: 30000,
    });

    const earnings = Math.floor(Math.random() * (1000 - 100 + 1)) + 100;

    collector.on("collect", async (m) => {
      message.channel.send(`You found ${earnings} primogems !`);

      await profileModel.findOneAndUpdate(
        {
          userID: message.author.id,
        },
        {
          $inc: {
            primogems: earnings,
          },
        }
      );
    });

    collector.on("end", (collected, reason) => {
      if (reason == "time") {
        message.channel.send("You ran out of time!");
      }
    });

    message.channel.send(
      `<@${
        message.author.id
      }> **Which location would you like to search?\n** Type the location in this channel\n \`${chosenLocations.join(
        "` `"
      )}\``
    );
  },
};

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    超级容易修复!问题是您要在 TextChannel 上创建一个消息收集器,然后 然后 发送您的消息,然后用户想要选择哪个位置,您只需将收集器移动到Message 对象而不是 TextChannel 发送消息之后。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-08
      • 2020-07-18
      • 2021-08-11
      • 2021-03-05
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      • 2016-08-07
      相关资源
      最近更新 更多