Discord.js 从带有前缀的句子中过滤出命令(比如 Hey Bot 给我一个蛋糕)
Discord.js Filter the command out of a sentence with prefix (like Hey Bot give me a cake)
我对 JS 和使用 discordjs 比较陌生。目前,带前缀 (+) 和斜线命令 (/) 的命令对我有用。但是,只有看起来像这样的命令才有效:Hey Bot ping
、Hey Bot help
等
我希望机器人过滤掉命令。例如 Hey Bot show ping
。 (Hey Bot
是前缀,ping
命令).. 我希望机器人像 Siri 一样工作,比如 Hey Bot show me the ping
。但这仅适用于 Hey Bot ping
或 Hey Bot ping word1 word2 etc
。也许你知道我如何实现这一点..非常感谢!!
这是我的命令ping.js:
const { Message, Client } = require("discord.js");
module.exports = {
name: "ping",
aliases: ['p'],
permissions : ["SEND_MESSAGES"],
/**
*
* @param {Client} client
* @param {Message} message
* @param {String[]} args
*/
run: async (client, message, args) =>
{
message.channel.send({ content: `${client.ws.ping} ws ping` });
},
};
我试过了
run: async (client, message, args) => {
if (message.content.toLowerCase().includes("ping")) {
message.channel.send({ content: `${client.ws.ping} ws ping` })
},
而不是 message.channel.send({ content: `${client.ws.ping} ws ping` });
但它对我不起作用..
这是我创建消息的方式:
使用命令和事件处理程序
const client = require("..");
var config = require("../settings/config.json");
var ee = require("../settings/embed.json");
const { MessageEmbed } = require("discord.js");
client.on('messageCreate', async message => {
let prefix = config.prefix
if (!message.guild) return;
if (message.author.bot) return;
if (message.channel.partial) await message.channel.fetch();
if (message.partial) await message.fetch();
const args = message.content.slice(prefix.length).trim().split(/ +/);
const cmd = args.shift().toLowerCase();
if (message.mentions.has(client.user)) {
message.channel.send({
embeds: [new MessageEmbed()
.setColor(ee.embed_color)
.setAuthor(`TEST`)
.setDescription(`Use \`${prefix}help\``)
.setFooter(ee.embed_footertext, ee.embed_footericon)
]
});
}
const command = client.commands.get(cmd.toLowerCase()) || client.commands.find((cmds) => cmds.aliases && cmds.aliases.includes(cmd));
if (command) {
if (!message.member.permissions.has(command.permissions || [])) {
return message.reply({
embeds: [
new MessageEmbed()
.setColor(ee.embed_color)
.setDescription(`** wrong ${command.permissions} **`)
]
})
}
command.run(client, message, args, prefix)
}
})
这并不理想,Siri 非常先进,但要搜索命令,您可以这样做:
const command = client.commands.find(c => message.content.includes(c.name.toLowerCase())
当然你可以包含别名:
String.prototype.includesAny = function (args) {
for (const arg of args) {
if (this.includes(arg)) return true;
}
}
const command = client.commands.find(c => message.content.includes(c.name.toLowerCase()) || message.content.includesAny(c.aliases)
我对 JS 和使用 discordjs 比较陌生。目前,带前缀 (+) 和斜线命令 (/) 的命令对我有用。但是,只有看起来像这样的命令才有效:Hey Bot ping
、Hey Bot help
等
我希望机器人过滤掉命令。例如 Hey Bot show ping
。 (Hey Bot
是前缀,ping
命令).. 我希望机器人像 Siri 一样工作,比如 Hey Bot show me the ping
。但这仅适用于 Hey Bot ping
或 Hey Bot ping word1 word2 etc
。也许你知道我如何实现这一点..非常感谢!!
这是我的命令ping.js:
const { Message, Client } = require("discord.js");
module.exports = {
name: "ping",
aliases: ['p'],
permissions : ["SEND_MESSAGES"],
/**
*
* @param {Client} client
* @param {Message} message
* @param {String[]} args
*/
run: async (client, message, args) =>
{
message.channel.send({ content: `${client.ws.ping} ws ping` });
},
};
我试过了
run: async (client, message, args) => {
if (message.content.toLowerCase().includes("ping")) {
message.channel.send({ content: `${client.ws.ping} ws ping` })
},
而不是 message.channel.send({ content: `${client.ws.ping} ws ping` });
但它对我不起作用..
这是我创建消息的方式:
使用命令和事件处理程序
const client = require("..");
var config = require("../settings/config.json");
var ee = require("../settings/embed.json");
const { MessageEmbed } = require("discord.js");
client.on('messageCreate', async message => {
let prefix = config.prefix
if (!message.guild) return;
if (message.author.bot) return;
if (message.channel.partial) await message.channel.fetch();
if (message.partial) await message.fetch();
const args = message.content.slice(prefix.length).trim().split(/ +/);
const cmd = args.shift().toLowerCase();
if (message.mentions.has(client.user)) {
message.channel.send({
embeds: [new MessageEmbed()
.setColor(ee.embed_color)
.setAuthor(`TEST`)
.setDescription(`Use \`${prefix}help\``)
.setFooter(ee.embed_footertext, ee.embed_footericon)
]
});
}
const command = client.commands.get(cmd.toLowerCase()) || client.commands.find((cmds) => cmds.aliases && cmds.aliases.includes(cmd));
if (command) {
if (!message.member.permissions.has(command.permissions || [])) {
return message.reply({
embeds: [
new MessageEmbed()
.setColor(ee.embed_color)
.setDescription(`** wrong ${command.permissions} **`)
]
})
}
command.run(client, message, args, prefix)
}
})
这并不理想,Siri 非常先进,但要搜索命令,您可以这样做:
const command = client.commands.find(c => message.content.includes(c.name.toLowerCase())
当然你可以包含别名:
String.prototype.includesAny = function (args) {
for (const arg of args) {
if (this.includes(arg)) return true;
}
}
const command = client.commands.find(c => message.content.includes(c.name.toLowerCase()) || message.content.includesAny(c.aliases)