javascript - Discord.js 从带前缀的句子中过滤出命令(例如 Hey Bot 给我一个蛋糕)

标签 javascript node.js discord.js bots args

我对 JS 和使用discordjs 比较陌生。目前,带有前缀 (+) 和斜杠命令 (/) 的命令对我有用。但是,只有类似这样的命令才有效:Hey Bot pingHey Bot help 等。

我希望机器人过滤掉该命令。例如 Hey Bot show ping。 (Hey Bot 是前缀,ping 命令)..我希望机器人像 Siri 一样工作,就像 Hey Bot 显示我 ping 一样。但这仅适用于 Hey Bot pingHey Bot ping word1 word2 等。也许你知道我如何实现这一点..非常感谢!!

这是我的命令 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)

关于javascript - Discord.js 从带前缀的句子中过滤出命令(例如 Hey Bot 给我一个蛋糕),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69548319/

相关文章:

javascript - 分割字符串并保留分割器

javascript - 使用 lodash .groupBy。如何为分组输出添加自己的键?

javascript - 如何在不使用 cookie 的情况下处理单页应用程序中的 CSRF 和 XSS 攻击

javascript - Vue.js 导入对象

node.js - AWS Cognito 用户池的事件触发器对象

javascript - 未定义的成员.voiceChannel

node.js - heroku 不显示 Procfile 文件 Discord.js

javascript - 异步编程的新手。如何让不同对象按正确顺序发生事情?

node.js - toPkcs12Asn1 所需的参数

python - Tensorflow 服务客户端/Base64 问题/错误 3