Creating a Discord bot using Javascript

Leon Fan
4 min readMay 31, 2021

--

Two weeks ago I was talking to a friend of mine, a computer science grad, about the software engineering bootcamp that I had recently started. He coded mainly in Javascript, the language taught in the bootcamp, so we had plenty to talk about.

We were going back and forth, discussing our thoughts about the language. We began joking around about outlandish ideas such as making an UwU translator bot to annoy our friends on a messaging platform called Discord. We realized that we could easily write out the code since it is fairly simple in terms of functionality. We decided to set aside the following weekend to work on this bot just so we can see our friends get mildly amused by such a pointless project.

The general idea of this bot is to translate Discord messages in a server, and then return that message but with the “r”s and “l”s replaced with “w”s. For example, the bot would read the word “hello” and then return a message that said “hewwo”.

Luckily for us, creating a bot on Discord is extremely easy, Discord allows users to create bot accounts that are tied to their account. We had our bot created quickly and, with it, its own key which we then had to tie to the Javascript code. Then, we installed Discord.js, a node.js module that lets us easily interact with the Discord api in order to read and write messages.

https://discord.js.org/#/

linking all the necessary files for Discord.js to do its magic

Following the guide provided by the Discord.js documentation, We set up our client, linked our account key, and logged the bot in using that key. Every time the bot is run, the terminal will log “Weady! :3” once everything is finished loading. Finally, we were ready to start writing the functionality for the bot.

Basically the entire code block for our translation functionality. It’s kinda bulky so I’ll split it into 2 parts when explaining it down below

As we started writing out the code for our translator, we quickly ran into our first roadblock: we had to prevent it from reading and translating its own messages or it would cause an infinite loop of the same message being repeated over and over.

the “reading” half of the code

To fix this, we set up a conditional clause where whenever a message is sent, we would check if the author of that message was our bot: “ÒwÓ” and if it was, then the bot would return nothing. We also decided that the bot should not translate links because they’re most likely going to end up broken. To solve that in a crude manner, the bot would ignore any message that contained “http”.

Finally, we thought that the bot would lose its novelty if it tried to translate every message, especially because some messages don’t even have “r”s and “l”s to translate. We wrote a few lines to count out how many “r”s,“l”s, “R”s and “L”s there are in a message. The empty arrays after the || operator so that the code wouldn’t error out if there weren’t any letters we were counting for by counting the length of the empty array, which is 0. Finally, we decided that the bot should only activate if the count was greater than 4 so it would pop in at unexpected times and give everyone a laugh.

the “write” bit

We assigned the content of a message to readMessage and then split the message up by words using the .split() function by passing a space in as the separator. We decided that if the word included “@”, we would leave it alone, so if someone tags another person in their message, the bot wouldn’t UwUify the tag so it would be doubly annoying (not that anyone in our discord server actually tags anyone). Basically the code then iterates through each word and replaces each “r”, “l”, “R”, “L” it can find with “w” or “W” depending on the original letters capitalization. By wrapping the letter we wished to replace in forward slashes, we could replace individual letters. Since .replace() only replaces the first instance of the letter, the g (global modifier) after the slash makes it so it will replace every instance of that letter.

As we modify each word, it gets appended to the end of our newString variable which started out as an empty string. Word by word, our translated message then is written out. Once our message is fully translated, we decided to make it even worse by appending a random emoji from an array of grade-A, choice emojis at the end of the sentence. Finally the bot sends the glorious translated message for all our friends to see.

Also I have to mention that we spent 40 minutes troubleshooting this bot because it suddenly stopped translating. Eventually, we realized that the message we were trying to activate the bot contained exactly 4 “r”s and “l”s which is just under the threshold we set earlier.

Once the bot was finished, we hosted it on Heroku so that it would stay active in our discord server even if our computers were off.

--

--