【发布时间】: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