Discord.js 允许在命令后面添加更多单词

拉斯蒙

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

这工作正常,但是如果我在!priceor后面放任何东西!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));
}
神话先生

除了使用相等运算符,您还可以使用String.prototype.startsWith它来查看它是否命令开头,而不是完全以命令开头

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章