Categories
Coding

Coding About #1 – Looking into Discord Bot Coding   

Featured image: Image by OpenClipart-Vectors from Pixabay

Discord is a social application I use a lot. Largely for my own usage it is for entertainment purposes such as joining communities that revolve around my interests. And it is a place where I can be social with less anxiety. A place to chill out.  

As of late and where I have mentioned before in some previous blogposts I am interested in learning to code. I want to learn JavaScript and Python. Tutorials are one thing to use to pick up some of the basics but as I soon learned it cannot be the only thing. You need to choose a specific area to focus on rather than broad-based approaches. Things like choosing what framework most interests you and what specifically you want to use it for.  

I am still figuring that out myself by researching different frameworks and what I could use them for. One is React for front-end UI development. I learned the best way to learn to code is to create a project and jump right in – and using coding communities and websites, and most importantly documentation to learn how to do the things you want to do – not the things you think you need to do.  

So, I thought I’d start by giving myself a project to create a bot for Discord. Bots on discord are applications that can join servers and perform various functions and aid activities/actions such as user interaction, moderation, entertainment, using it pull up resources, logging things and much more. Pretty much you can get a bot to suit pretty much any need you want and make things easier and more interesting on your Discord server – just as long as you keep within the Terms of Service and Community Guidelines. So, for example creating bots to do malicious things on Discord is a no-no obviously.  

Many servers will use commonly used bots that have been verified by Discord. This makes sense as such bots are verified to be safe and secure to use without malicious intent. But some servers also want to look cool by creating their own custom bot that you won’t find anywhere else.  

I have wanted to create my own Discord bot for some time and is also one of the reasons I wanted to learn Python because that programming language is commonly used in the creation of Discord bots. Until now I’ve just never got around to it. For my project I’ve decided to use JavaScript to create the bot as that is the language I am currently focusing on learning.  

Starting Out

My first steps were researching where the heck you even begin because there are many ways you can go about it. For one you can just do it on an IDE you’ve installed such as Visual Studio Code. Perhaps one day I will do that as I do have it installed but I more want to use that when I become more serious with projects. This project for now will just be simple.  

I quickly found a tutorial on FreeCodeCamp that builds a basic Discord bot using JavaScript. The framework used is node.js which is for backend JS development. Obviously, all the code we’re going to be writing for the bot will be on the backend. And since it is a Discord bot we will also be using the discord.js library which is Discord’s own JavaScript framework for Discord application development and it is an API client. It’s a bit of a no-brainer really.  

Finally, the IDE I am using is repl.it. I am using this as it provides free hosting for the Discord bot and also means I do not need to store any of the files on my own computer (which again I’m choosing to reserve for when I get more serious). It’s free to use, requires no download, has a community around it, and pretty much all the support and tools needed for most personal projects. The only caveat is anyone can view your code, though obviously there are still ways to hide sensitive code/strings (like the Discord bot token). Plus, the initial guidance I am using uses it.  

Although I am initially following a guide on FreeCodeCamp (to get the bare basics) I will certainly be planning things outside of that I want my bot to be able to do and learning about it and how to do it through my own research. 

Creating the Bot

To get started I first needed to go to Discord’s developer portal to create a new application. Creating the bot itself is very easy in that regard because all you do is click a button and input a few details and then add it as a bot user and then just like that you have a bot. Oh okay… so we’re done then? No. The bot does absolutely nothing because we have not coded it to do anything so as of now it’s just a bot that does nothing.  

Initially I wanted to call my bot George, but that name had already been taken so I called it GeorgeTheThird instead. Next up was actually getting the bot on to a Discord server of mine so I could actually interact with it and test the things I code it to do. This is pretty easily done from the developer portal itself by giving the bot the needed server permissions and generating a URL and from there using that URL to get the bot to join my Discord server with the permissions I had set.  

The server permissions involve things like whether it can view logs, messages, channels, ban or kick members, create server invites, and much, much more. The permissions you give will be based on what you plan to code your bot to do. So, for example, if your bot will interact based on user messages then it will need the permission to view/read messages on the server. You can always change server permissions for your bot later on – they’re not set in stone.

The nuclear option is to give the bot Administrator so it has access to pretty much everything but if you’re going to do this you should be careful who has access to the bot. Generally, the best practice is to specifically give permissions to the bot on what you plan for it to do initially and then just edit permissions as your bot evolves and you get new ideas. Don’t give your bot permissions that you do not plan to use right away.  

Another important thing after bot creation on the developer portal is to grab the bot’s generated token. This is needed to code the bot in the IDE. The token should never be shown to anyone else otherwise it will allow them to do things to your bot you do not want. If your token does get compromised or you forget it the developer portal allows you to generate a new one.  

Creating a repl and typing the code

After all that initial setup it was time to get to coding. As previously said, I am using the repl.it IDE so I can host the bot’s code for free and more easily follow-along in the initial guide. So first I needed to create an account on that website and then create my first Repls. The amazing thing about this website is you can code using dozens of different languages and frameworks without needing to actually download anything on to your computer and I’d say it is much easier for beginners (like me) to use as well. So, I chose node.js as the framework/template and created my first repls.  

Now the coding could actually begin. The first thing I needed to do was add the discord.js package/dependency to the repl so I could actually use its features in the code. This was very simply done by putting a const variable and saving require(‘discord.js’); to it, this was on line 1. Upon running that line of code the repl installs the package and you’re good to go. I can also use the discord.js documentation helpfully published by Discord to learn my way and it would come in handy later on. But for now, I was still following the FreeCodeCamp guide.  

By following this guide, I added two functions to the code used to detect events – one that logs the bot on (so you can actually interact with it) and another simple function that uses an If statement to reply to the message ‘ping’ with ‘pong’. I’ve seen this on other Discord bots actually so it’s pretty common and likely something many newbies like me start with.  

I also made sure to comment my code along the way as well so I could remember what they do based on the explanations given by the FreeCodeCamp guide. I know that it is best practice to do this with code as many previous things I have read have told me to do it and that other developers would love me for it. It is especially important when your code becomes monstrously large. Even the best developers will struggle to deal with large amounts of code that has either not been commented or has been commented poorly/inconsistently.  

Now to actually log the bot in I also needed to add my bot’s token to the code but obviously I can’t just put it right in the code as a plain-text string otherwise anyone can just easily look at my repl and see it. Now this is where I began realising that the FreeCodeCamp guide was perhaps a bit outdated. On the guide it tells me to make a new file on the repl called a .env file which are (or in this case were) used to hide sensitive strings from your code. It did this by using a keyword associated in the .env file that you could reference to your code so that it could use the token without revealing it to any snoopers.  

The First Error

So, I created the file and associated it to the code… and error. When I ran the code I got an invalid token error. This meant that either the token has stopped working for whatever reason or the code simply was not able to read the token for whatever reason. So, since the guide didn’t have guidance on what to do if you came across this error it was time for me to do my own investigation (and this is actually a good thing because you learn more when you do things yourself).  

The first thing I naturally tried to do was generate a new token and then use that one instead just in case for whatever reason the old token had just had some issue with it. This simple solution unfortunately did not work so it was time for some deeper research. I noticed on my Google escapades that other people (some of whom clearly following the same FreeCodeCamp guide I was) was also getting the same error.  

Solutions involved generating a new token (which I already tried and also did not work for these people) or changing the syntax in certain ways which also failed to work. One suggestion advised installing a certain dependency that they said allowed discord.js to read the bot token, which actually worked for some people, so I tried it myself – still no dice. I also messed up in the console on repl.it when attempting this and inputted something that led the console to continuously try and install a package that I did not want, and I could not find a way to get it to stop running.  

In panic I copied all my code away and then deleted the repl and created a new one and copied all my code back in. Probably an unnecessary way of doing it, but luckily, I only had a few lines of code, so it wasn’t a nightmare. I made sure to be more careful what I put into the console from then on. Also, for those who don’t know the console is a tool that shows you the output of your run code and you can use it to install packages/dependencies, and it’s important/useful for troubleshooting bugs/errors in the code.  

I then got back to research on the token issue. In the end it turns out that repl does not use .env files to hide sensitive strings anymore, instead there is a Secrets tool that you add the sensitive string to, input the keyword and then it generates a variable that is associated to it and that I put into my code. This worked! My bot’s token was now accepted.  

More Debugging and Fixing + Discord Bot Intents

Now that my bot was online it was time to test the ping, pong function. It did not work. Now, of course I was confused as it why it was not working since I had copied the FreeCodeCamp code as required and had made no mistakes in the syntax. So, it was once again time for another round of solo research. My discovery led me to once again realise that the FreeCodeCamp guide was outdated. Discord.js has since been updated to where you needed to declare intents, this is similar to giving a bot server permissions but instead you’re giving these permissions to the discord.js client.  

I used the discord.js documentation to figure out how to do this and added the intents I needed (ones to do with member/message functions). After that stint of self-learning and discovery I ran the code and obtained a new error about ‘disallowed intents’. At first, I wondered if maybe I had added the wrong intents but upon further research in the discord.js documentation I discovered that certain intents were known as ‘privileged intents’ because they handled information or performed actions of a sensitive nature.  

Two of my intents were considered privileged and so would not work. Initially, I was afraid that I’d need Discord to verify my bot in order to be allowed to use them which then had me thinking how on Earth I was supposed to do anything with my bot at all. But then I saw that you only needed bot verification for bots that were in over 100 servers and those bots are required to request permission to use privileged intents. For bots that don’t require verification they can just toggle in the bot settings on the Discord developer portal to allow them to use privileged intents. It’s a reasonable security measure to help combat malicious behaviour.  

Okay. So, we got the token working, we got the right intents, we toggled on the ability to use privileged intents, so now the ping, pong worked right? Right? No. It still doesn’t work but this time there is no error message to go off of… absolutely nothing. It simply just does not work even though as far as I can tell all the code is correct. So now, my next phase will be to do research on more recent bot creations so I can compare my code to there’s and see what they have done differently.  

We’ve dug so far yet paved so little. But I am still having fun and I have learned some important things. Hopefully next time we can actually get my bot to do something.  

Other Coding Stuff

And actually… I did manage to get it to do something things in the end but I’ll talk about that in my next blogpost. I am looking forward to making more of this kind of content as I discover more in the world of coding. I recently also discovered HTML tables – I say ‘discovered’ but I had known they existed – but only now have I actually been getting into making them on vscode.  

The reason for this is I have started using something called the Odin Project, a free online learning website for coding, that gives you numerous lessons on different things to do with HTML, CSS, JavaScript. It is not simply just a guide you read but it also prompts you to do your own research and gives you practicals to do – such as practicals on the MDN web docs website (a place that serves documentation and guides for HTML, CSS, JavaScript and a number of other cool things. This is what has really got me into HTML tables and I have plans to make HTML tables on various interests of mine.  

I also learned about svg (vector graphics) which are images made up of XML markup. It’s great for making your own icons and logos, and even basic animations as well, plus much more. I am looking forward to digging into it. I might make some SVG’s in the future for some of my blogposts.  

A good balance of theory, practical content, and self-created projects is always the best way to go in order to effectively learn and retain information and its related concepts.  

I’ll also make sure to actually document some pictures in my next blogpost as well of the progress.


Thank you for reading. If you have any queries please Email me, you can find my Email in the Contacts & Community section. Please also follow The Weekly Rambler on Twitter, Reddit, Pinterest and Facebook which you can access through the buttons at the bottom of this website. You can also use the social media buttons under each blogpost to share with your family, friends and associates.