1

I'm a beginner at code and I would like to make my bot input a word said and then, with a block of pre-written text, put that word in the text.

Command: [prefix] [command] [word]

So, an example to complain about churros: ch complain churros

If the preset text is: everyday, I wake up to the smell of [word]. I'm sick of [word].

I would like the output of the command, then, to be: everyday, I wake up to the smell of churros. I'm sick of churros.

How can I do this? An example of how one would code this would be greatly appreciated. :) Thank you!

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
  • Does this help? https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript – Zsolt Meszaros Feb 14 '21 at 20:19
  • I'm not sure if I understand, I want my bot to recognize the command and then the word that a user puts, and then input that word into the original present text. Similar to how ${message.author.tag} pings the person who uses the command. If that makes sense! :) – eatingchurros Feb 14 '21 at 20:26

2 Answers2

1

You can use .replaceAll() to replace every occurrence of a string in a string. If you want to replace [word] in "everyday, I wake up to the smell of [word]. I'm sick of [word]." you can do the following:

const text = "everyday, I wake up to the smell of [word]. I'm sick of [word]."

console.log(text.replaceAll('[word]', 'churros'))

In Discord.js you can grab the incoming message and replace the string like this:

const { Client } = require('discord.js');
const client = new Client();
const prefix = '!';

client.on('message', (message) => {
  if (message.author.bot) return;

  const args = message.content.slice(prefix.length).split(/ +/);
  const command = args.shift().toLowerCase();

  if (command === 'complain') {
    // grab the first word after the command
    const [word] = args;
    const text = "Everyday, I wake up to the smell of [word]. I'm sick of [word].";

    if (!word) {
      return message.reply('You need to send a word to complain about!');
    }

    message.channel.send(text.replaceAll('[word]', word));
  }
});

And it works like this:

enter image description here

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
0

In addition, another answer that gives you a variety of options includes using template literals. Using Zsolt's general structure, but modifying it a little bit, you can use ` (backticks) in your code to insert values straight into your string. Template literals have a variety of different uses, so it's not only limited to here, but are a suitable application.

Code:

const { Client } = require('discord.js');
const client = new Client();
const prefix = '!';

client.on('message', (message) => {
  if (message.author.bot) return;

  const args = message.content.slice(prefix.length).split(/ +/);
  const command = args.shift().toLowerCase();

  if (command === 'complain') {
    // grab the first word after the command
    const [word] = args;
    //Only change is made here, when using template literals make sure to always include backticks or else the code won't recognize it
    const text = `Everyday, I wake up to the smell of ${word}. I'm sick of ${word}.`;

    if (!word) {
      return message.reply('You need to send a word to complain about!');
    }

    message.channel.send(text);
  }
});
PerplexingParadox
  • 1,196
  • 1
  • 7
  • 26
  • Thank you for your input, I'll also look into templates. The ${word} is what I was originally thinking of since it reminded me of ${message.author.tag}! Full circle! – eatingchurros Feb 14 '21 at 22:37