Discord.js 允许命令后面有更多单词

Discord.js allow more words behind the command

我正在尝试创建命令,但它们仅在写入特定单词时触发... !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));
}

我能够找到解决方案,首先通过我们的参数定义命令,然后在 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
    }
  });

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

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

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