1

I am trying to make my Discord bot replace various words in a message with other words, but I can't seem to figure out how to do it for more than 1 word.

Let me give an example:

I want to replace the '!say' with nothing

I want to replace 'llama' with 'ogre'

I want to replace 'john' with 'johnny'

So, I want the bot to replace these words with the other words. But I can only figure it out with doing one replacement, otherwise it repeats the message over and each with the different change.

Here's the code I got at the moment for this

client.on("message", message => {
if (message.content.startsWith("!say")) {
message.channel.sendMessage("I say: " + message.content.replace('!say ','' + '\n'))
};
});

Anyone know how? Sorry if this is super confusing :(

1 Answers1

1

I'd make an object whose keys are the words you want to replace, and whose values are their replacements. Then you can construct a regular expression by joining all keys together, and use a replacer function to look up the appropriate replaced value on the object:

const replacements = {
  '!say': '',
  llama: 'ogre',
  john: 'johnny'
};
const pattern = new RegExp(Object.keys(replacements).join('|'), 'g');
client.on("message", message => {
  const replacedText = message.content.replace(pattern, key => replacements[key]);
  // use replacedText
});

const replacements = {
  '!say': '',
  llama: 'ogre',
  john: 'johnny'
};
const pattern = new RegExp(Object.keys(replacements).join('|'), 'g');

const input = '!say foo bar llama baz john buzz';
const replacedText = input.replace(pattern, key => replacements[key]);
console.log(replacedText);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • Can't seem to get this to work. Am I supposed to put something on // use replacedText? – NamesJoeyWheeler Sep 07 '19 at 08:25
  • Works just fine here - press Run code snippet to see a live demonstration, it should work for you as well – CertainPerformance Sep 07 '19 at 08:26
  • Yes, the `replacedText` is the replaced text that you want - once you have it, you can use it however you want – CertainPerformance Sep 07 '19 at 08:26
  • It works on the code snippet, but I can't get it to work with the Discord bot. Here's the full index.js code: https://pastebin.com/P3WujFGL Am I missing something? – NamesJoeyWheeler Sep 07 '19 at 08:32
  • You aren't using the `replacedText` string you constructed anywhere – CertainPerformance Sep 07 '19 at 08:32
  • I'm sorry for being really dumb, but I don't get what you mean by the replacedText string. I never had a string named that in my original code. – NamesJoeyWheeler Sep 07 '19 at 08:39
  • The new variable name that contains the message with replaced text is called `replacedText` - you can now use that however you want. (did you want to call `sendMessage` with it?) – CertainPerformance Sep 07 '19 at 08:40
  • Okay, so I sort of got that to work. But now it is spamming my chat with deformed words. https://imgur.com/a/FH5B820 Here is the code: https://pastebin.com/icJaqTb7 Did I have to change something else on that line? – NamesJoeyWheeler Sep 07 '19 at 08:50
  • Just to be save you should add some [escaping](https://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript) for the keys – Andreas Sep 07 '19 at 08:52
  • @NamesJoeyWheeler _"is spamming my chat"_ - Your script is triggered when a new message gets posted. Your script posts a new (modified) message. – Andreas Sep 07 '19 at 08:54
  • Ok, I managed to fix it by making the bot ignore commands from bots. Thanks! – NamesJoeyWheeler Sep 07 '19 at 09:02