eatingchurros
eatingchurros

Reputation: 13

How to make bot input a word into a pre-set block of text (Discord.js)

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!

Upvotes: 1

Views: 809

Answers (2)

PerplexingParadox
PerplexingParadox

Reputation: 1196

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

Upvotes: 0

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23161

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

Upvotes: 1

Related Questions