Replace Certain Character in Discord. Js

Now, before you say that this has been posted before, I have a different situation.

With that out of the way, let's get on with the question.

I am making a Discord bot for a friend that does duties for the group and things like that.

Quick note too, I am using the Sitepoint version of Discord.JS because I'm a beginner.

I want the bot to send a message to a certain channel when the show gets canceled for a reason. For example, they would send something like this:

afv!cancel Roblox went down.

or something similar.

But every time it sends a message, every space turns into a comma like this:

:x: The show has been cancelled because: "Roblox,went,down.". Sorry for that!

Here's the index.js code that handles executing commands:

bot.on('message', msg => {
  const args = msg.content.split(/ +/);
  const command = args.shift().toLowerCase();
  const prefix = command.startsWith("afv!");
  
  if (prefix == true) {
    console.info(`Called command: ${command}`);

    if (!bot.commands.has(command)) return;
    
    msg.delete(1);

    try {
      bot.commands.get(command).execute(msg, args, bot);
    } catch (error) {
      console.error(error);
      msg.reply('there was an error trying to execute that command!');
    };

And the cancelled.js file:

module.exports = {
  name: 'afv!cancel',
  description: "in-case the show gets cancelled",
  execute(msg, args, bot) {
    if (msg.member.roles.find(r => r.name === "Bot Perms")) {
      const reason = args.replace(/,/g, " ");
      bot.channels.get('696135370987012240').send(':x: **The show has been cancelled** because: "' + args + '". *Sorry for that!*');
      bot.user.setActivity("AFV! | afv!help", { type: 'PLAYING' });
    } else {
      msg.reply('you are missing the role: Bot Perms!');
    }
  },
};

By the way, upon executing the command, it prints this:

TypeError: args.replace is not a function

Thanks for reading! :)

1

1 Answer

From what I can see, here

const reason = args.replace(/,/g, " ");
bot.channels.get('696135370987012240').send(':x: **The show has been cancelled** because: "' + args + '". *Sorry for that!*');

you are making a const reason, wherein you try to handle that as a string and replace all commas with spaces. Unfortunately, even though it can be displayed in a string and looks to be one, in reality it is an array, so replace() won't work on it. To be able to use replace() you need to first transform your array to an actual string:

const reason = args.join().replace(/,/g, " ");

With that done you won't be seeing this pesky "not a function" error, and you can easily use reason to display your message:

bot.channels.get('696135370987012240').send(':x: **The show has been cancelled** because: "' + reason + '". *Sorry for that!*');
5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest