A template for Discord bot code using HalfBot framework
Go to the Discord Developers Portal, Press on New Application
and fill in the required information
Customise your bot however you want, we recommend adding tags since it's used by the /about
command, we also recommend adding the most important commands in your application description using the context </COMMAND_NAME:COMMAND_ID>
, you can get the command id by right-clicking the command description area when calling it
You can also get the command id from Server Settings => Integrations => (Your bot)
by right-clicking the command area
Go to the Bot
section left on the navigation bar then copy the token if it's shown, if not then just click Reset Token
and copy it and save it in a safe place because we'll need it later
Go to the URL Generator
section under OAuth2
section left on the navigation bar, here we can generate an invite link for the bot so you can invite it to your server
Choose the following for starting:
- Scopes: bot, applications.commands
- Bot Permissions: Administrator
You can generate a different one later
You'll end up with a link similar to the one shown below, if you face any problems in the last steps copy this link and change the CLIENT_ID
value, which you can copy from the General Information
section left on the navigation bar under the name Application ID
Take the final link you have and paste it in any browser or in Discord (then click it) and the bot invite form will show up
There's a much easier way to let people invite you're bot to their servers other than giving them the link you created earlier or using a command to generate the invite link (unless you want only selected people to be able to invite your bot to their servers)
Go to the General
section under OAuth2
left in the navigation bar, press on the dropdown below and choose In-app Authorization
A scopes-and-permissions box checks will show up, choose the scopes and permissions you want then click save, now your bot has a beautiful invite button on it's profile
Never choose the
Administrator
permissions since it gives full permissions and usually people won't trust bots asking for this permission turned on, so just choose the minimal required permissions for your bot to run
Create a file called .env
in the root path of your project, never move or rename this file, this file will store sensitive data so keep it safe and never share it with anyone and most importantly don't push it to GitHub, you can do so by making sure that there's a '.env' line in your .gitignore
file
Copy the following into your .env
file then Replace PUT_YOUR_TOKEN_HERE
with your own bot token that you should've saved already, if not then read the Bot Token section
TOKEN=PUT_YOUR_TOKEN_HERE
To download all the required packages, run the following command
npm i
Now all that remains is to run the bot and try it out by running the following command
npm run start
Now that we have the bot setup and ready, it's time to modify it by putting your brand as well as updating the existing bot functionalities and adding new ones
The folder data/
contains various data stored in JSON files, upon initialisation, the bot will read them and store them inside it as bot.data.FILE_NAME
Here you should store non-sensitive informations that are used in the bot like guild/channel/role/user IDs or brand social media links or anything else, it's better to separate the data from the logic
The files inside the
data/
folder must be JSON files only and mustn't contain and comments or anything that's not acceptable by theJSON.parse()
function
If you want to rename the
data/
folder or change it's path then you must change the value of thedirectories.data
in thebot/launch.js
file
The bot modules should go under the bot/modules/
folder, there are two types of modules that are commands and events, each having their own folder
There are couple of code snippets to help you create bot module files much faster pre-configured inside .vscode/
file, these snippets support JavaScript CommonJS, JavaScript ES Module and TypeScript
ES Module (import/export) are not yet supported in HalfBot, use CommonJS (require()) only, for now
Available code snippets are as follow
- Command: command
- Event
- Client Event: event-client
- Repeating Event: event-repeat
There are some settings already configured in .vscode/
, .prettierrc.json
and .eslintrc.json
, it's better not to change anything in them if it's your first time using Node.js
You can run the following npm scripts for formatting the code
npm run format