How to Create a Discord Bot Account
In order to work with the Node.js library and the Discord API, we must first create a Discord Bot account.
Here are the step to creating a Discord Bot account.
- Make sure you’re logged on to the Discord website.
- Navigate to the application page.
- Click on the “New Application” button.
- Give the application a name and click “Create”.
- Go to the “Bot” tab and then click “Add Bot”. You will have to confirm by clicking “Yes, do it!”
Keep the default settings for Public Bot (checked) and Require OAuth2 Code Grant (unchecked).
Your bot has been created. The next step is to copy the token.
How to Invite Your Bot to Join a Server
Now you have to get your Bot User into a server. To do this, you should create an invite URL for it.
Go to the “OAuth2” tab. Then select “bot” under the “scopes” section. Now choose the permissions you want for the bot. Our bot is going to mainly use text messages so you don’t need a lot of the permissions. After selecting the appropriate permissions, click the ‘copy’ button above the permissions. That will copy a URL which can be used to add the bot to a server. Paste the URL into your browser, choose a server to invite the bot to, and click “Authorize”.
To add the bot, your account needs “Manage Server” permissions. Now that you’ve created the bot user, start writing the Python code for the bot.
How to Create a Repl and Install discord.js
To use the discord.js library, just add const Discord = require(“discord.js”); at the top of main.js. Repl.it will automatically install this dependency when you press the “run” button.
How to Run the Bot
Now click run button on the top to run your bot in repl.it and go to your Discord room and type “ping”. Your bot should return “pong“.
How to Enable User-submitted Messages
The bot is completely functional, but now let’s make it possible to update the bot right from Discord.
Use Repl.it’s built-in database to store user-submitted messages. This database is a key-value store that’s built into every repl.
How to Set Up the Bot to Run Continuously
If you run your bot in repl.it and then close the tab it is running in, your bot will stop running. But there is a way you can keep your bot running continuously, even after you close your web bowser.
The simplest way is to sign up for paid plan in Repl.it. Their cheapest paid plan is called the Hacker Plan and it includes five always-on Repls.
Once you have signed up for that plan, open your Repl and click the name at the top. Then select the “Always On” option.
How to Create a Web Server in repl.it
Creating a web server is simpler than you may think. To do it, create a new file in your project called server.js. The server will run on a separate thread from the bot.