Əkrəm Cəfər
Əkrəm Cəfər

Reputation: 339

How to replace other thing to custom words

I'm creating a welcome/bye bye message plugin for Discord. I have added custom words, but It's not working on events.

Here is my code:

client.on ('guildMemberAdd', Member => {

  const Database = JSON.parse (fs.readFileSync ('./Bot/Database/Welcome-Bye.json'))
  const customMessage = Database [Member.guild.id].Message
  const Message = new RichEmbed ()
  .setColor (0x00ff00)

  if (customMessage) {
    if (customMessage.includes ('-membertag-')) customMessage.replace ('-membertag-', Member.user.tag)
  }

})

It isn't sending tag of member, sending "-membertag-" again. How can I fix this?

Upvotes: 0

Views: 59

Answers (1)

Chlod Alejandro
Chlod Alejandro

Reputation: 606

String.prototype.replace() returns a string with the contents replaced. It does not directly modify the string.

If you want to preserve the value of that string, you have to assign it again.

if (customMessage.includes('-membertag-'))
     // assign the new value back to customMessage
     customMessage = customMessage.replace('-membertag-', Member.user.tag);

If you want to change all instances of -member-tag-, you can use a regular expression.

if (customMessage.includes('-membertag-'))
     // find all instances of `-member-tag-` (case insensitive) and replace
     customMessage = customMessage.replace(/-membertag-/gi, Member.user.tag);

Upvotes: 1

Related Questions