【问题标题】:Discord.js allow more words behind the commandDiscord.js 允许命令后面的单词更多
【发布时间】:2021-11-16 10:59:56
【问题描述】:

我正在尝试创建命令,但是它们仅在写入特定单词时触发...!price 仅作为示例。我希望它即使在命令!price random text bla bla bla 之后写入任何文本后也能触发我有点想不通,因为我试图为每个命令使用不同的文件,以便在主 .js 文件中没有所有内容。

// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const config = require("./configtest.json");
const ping = require("./commands/ping.js")
const price = require("./commands/price.js")

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

//Commands
client.on("messageCreate", (message) => {
  if (message.author.bot) return;
  var command = (message.content == "!price" || message.content == "!ping")
  if(command){ // Check if content is defined command
    if (message.content.toLowerCase() == "!price" ) command = price
    if (message.content.toLowerCase() == "!ping" ) command = ping
      command(message); // Call .reply() on the channel object the message was sent in
    }
  });

这很好,但是如果我在!price 或!ping 后面放任何东西,它们就不会触发。

我也试过了,但这对我不起作用...

client.on("messageCreate", (message) => {
  if (message.author.bot) return;
  var command = (message.content == "!price" || message.content == "!ping")
  if(command){ // Check if content is defined command
    const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
    const cmd = args.shift().toLowerCase();
    if (cmd === "!price" ) command = price
    if (cmd === "!ping" ) command = ping
      command(message); // Call .send() on the channel object the message was sent in
    }
  });

错误:command(message); // Call .send() on the channel object the message was sent in ^TypeError: command is not a function

我的 ping.js 文件是这样的

module.exports = (message) => { // Function with 'message' parameter
    message.reply("Pong!").catch(e => console.log(e));
}

【问题讨论】:

    标签: javascript node.js discord discord.js command


    【解决方案1】:

    您可以使用String.prototype.startsWith 而不是使用相等运算符,这样您就可以查看它是否以命令开头,而不是完全以命令开头。

    var command = (message.content.startsWith("!price") || message.content.startsWith("!ping"))
    

    这会起作用,因为像!ping abc 这样的字符串确实 以!ping 开头。但是,如果它在中间(通常不是人们想要的机器人),这将不起作用:abc !ping abc

    【讨论】:

    • 这也是一个有趣的解决方案 :-) 测试它并且工作正常。
    • 如果有帮助,您可以通过单击复选标记将其标记为正确!
    • 完成了,呵呵,;-) 我找到了不同的解决方案,但这个也很有帮助!有一个美好的一天/晚上。
    【解决方案2】:

    我能够找到解决方案,首先通过我们的 args 定义命令,然后在 if 语句中比较我们发送的命令是否已知。

    client.on("messageCreate", (message) => {
      if (message.author.bot) return;
      const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
      const cmd = args.shift().toLowerCase();
      var command = (cmd === "price" || cmd === "ping")
      if(command){ // Check if content is defined command
        if (cmd === "price") command = price
        if (cmd === "ping") command = ping
          command(message); // Call .send() on the channel object the message was sent in
        }
      });
    

    如果您不想在config.json 中使用定义的前缀,您也可以删除.slice(config.prefix.length),因此我们必须在此处添加!触发前word。

    client.on("messageCreate", (message) => {
      if (message.author.bot) return;
      const args = message.content.trim().split(/ +/g);
      const cmd = args.shift().toLowerCase();
      var command = (cmd === "!price" || cmd === "!ping")
      if(command){ // Check if content is defined command
        if (cmd === "!price") command = price
        if (cmd === "!ping") command = ping
          command(message); // Call .send() on the channel object the message was sent in
        }
      });
    

    【讨论】:

      猜你喜欢
      • 2018-07-07
      • 2021-04-19
      • 1970-01-01
      • 2018-07-09
      • 2013-08-18
      • 1970-01-01
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多