Search code examples
javascriptnode.jsdiscord.jsbotsargs

Discord.js Filter the command out of a sentence with prefix (like Hey Bot give me a cake)


I am relatively new to JS and using discordjs. Currently, commands with prefix (+) and slash commands (/) work for me. However, only commands that look like this work: Hey Bot ping, Hey Bot help etc.

I would like the bot to filter out the command. So for example Hey Bot show ping. (Hey Bot is prefix, ping the command) .. I want the bot to work like Siri, like Hey Bot show me the ping. But this only works with Hey Bot ping or Hey Bot ping word1 word2 etc. Maybe you have an idea how I can realize this .. Many thanks!!

This is my command 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` });
    },
};

I tried

run: async (client, message, args) =>    {
      if (message.content.toLowerCase().includes("ping")) {
      message.channel.send({ content: `${client.ws.ping} ws ping` })
    },

instead of message.channel.send({ content: `${client.ws.ping} ws ping` }); but it don't works for me ..

This is my how the message is create:

works with commands & event handler

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)
    }
})

Solution

  • This is not ideal, Siri is very advanced, but to search for the command you can do this:

    const command = client.commands.find(c => message.content.includes(c.name.toLowerCase())
    

    And of course you can include aliases:

    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)