Rajaneesh R
Rajaneesh R

Reputation: 26

Specify the last args

I have created a discord.js bot and i was thinking so making a translator and i wanted to get the messages last args.

client.on("message", async (message, args) => {
  if (message.content.startsWith(`${config.bot.prefix}ts `)) {
    const args = message.content
      .slice(config.bot.prefix.length)
      .trim()
      .split(/ +/g);
    let tmsg = args[1];
    let lang = args[2];
    translate(tmsg, { to: lang })
      .then((res) => {
        const tldmsg = res.text;
        message.reply({
          embed: {
            title: "Translated",
            description: `${tldmsg}`,
            fields: [{ name: "Translated to", value: `**Language: **${lang}` }],
            footer: {
              text: `Requested by ${message.author.username}`,
            },
            color: config.bot.color
          },
        });
        //console.log(res.text); //translated language
        //console.log(res.from.language.iso); //language converted
      })
      .catch((err) => {
        console.error(err);
      });
  }
});

this is my code in the args[2] i want that to be the very last args of the message.

Upvotes: 0

Views: 73

Answers (1)

Yonghoon Lee
Yonghoon Lee

Reputation: 2514

You can get last the last one with Array.prototype.pop()

client.on("message", async (message, args) => {
  if (message.content.startsWith(`${config.bot.prefix}ts `)) {
    const args = message.content.trim()
      .slice(`${config.bot.prefix}ts `.length)
      .trim()
      .split(' ');
    let lang = args.pop();
    let tmsg = args.join(' ').trim();
    // translate
  }
});

Upvotes: 1

Related Questions