How to make a Discord bot:
create it, get a token and invite it
Create the bot in the Discord Developer Portal, get its token and invite it to your server. Then write a program that logs in with that token to bring the bot online.
Create the application and bot
Every Discord bot belongs to an application, so you create the application first. A Discord account is all you need for that part. Writing the code later takes a computer with Node.js or Python installed. The Developer Portal is in English only.
- Create an application
Open the Discord Developer Portal in your browser and log in. Click New Application, give it a name, accept the terms and create it.
- Set the bot's name and avatar
Choose Bot in the left sidebar. The name and avatar here are what members see in your server.
- Generate a token
On the same page, click Reset Token and copy the string that appears. You’ll need it when you write the code.
- Turn on intents if you need them
The switches are under Privileged Gateway Intents, further down the Bot page. A bot that only uses slash commands leaves them all off. Message Content Intent lets the bot read the text of regular messages, and Server Members Intent covers members joining and leaving. Turn one on only when the bot uses it.
Where to find and reset the token
The token is generated on the Bot page of the Developer Portal and shown only once, right after you click Reset Token. It disappears when you leave the page. If you lose the token or think it has leaked, go back to the Bot page and click Reset Token again. The old token stops working immediately, so replace it in your program and on your server, then restart the bot to bring it back online.
The token is the bot's username and password in one. Don't paste it into a Discord channel, show it in a screenshot or push it to a public GitHub repository.
Generate an invite link
To add the bot to a server, you need the Manage Server permission on that server.
- Open the URL Generator
Choose OAuth2 in the left sidebar and scroll down to OAuth2 URL Generator.
- Select the scopes
Check
botandapplications.commands. Without the second one, slash commands won't appear. - Select the permissions
Under Bot Permissions, check only what the bot really uses, such as Send Messages and Read Message History. Don't tick Administrator as a shortcut.
- Open the link and pick a server
Copy the URL generated at the bottom, open it in your browser, choose the server and click Authorize. The bot shows up in the member list but stays offline until your program runs.
JavaScript example (discord.js)
Install Node.js first, using the version the discord.js guide asks for. Create a new folder and run these two commands inside it.
npm init -y
npm install discord.js dotenvIn the same folder, create a file named .env and paste your token after the equals sign. Leave out spaces and quotes.
DISCORD_TOKEN=paste-your-token-hereNext, create index.js. When the bot comes online, it registers a /ping command and answers pong whenever someone uses it.
require('dotenv').config();
const { Client, Events, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once(Events.ClientReady, async (c) => {
await c.application.commands.set([{ name: 'ping', description: 'Replies with pong' }]);
console.log(`Logged in as ${c.user.tag}`);
});
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === 'ping') await interaction.reply('pong');
});
client.login(process.env.DISCORD_TOKEN);Run node index.js. Once “Logged in as” appears, the bot shows as online in Discord. Try /ping.
Python example (discord.py)
The discord.py documentation lists which Python version to install. With Python in place, install these two packages. The bot reads the same .env file as the JavaScript example.
pip install -U discord.py python-dotenvCreate app.py. It does the same thing as the JavaScript example.
import os
import discord
from discord import app_commands
from dotenv import load_dotenv
load_dotenv()
client = discord.Client(intents=discord.Intents.default())
tree = app_commands.CommandTree(client)
@tree.command(name="ping", description="Replies with pong")
async def ping(interaction: discord.Interaction):
await interaction.response.send_message("pong")
@client.event
async def on_ready():
await tree.sync()
print(f"Logged in as {client.user}")
client.run(os.environ["DISCORD_TOKEN"])Run python app.py. The example syncs commands every time the bot starts. Once you have more commands, sync only when they change.
Run it 24/7 on a server
To keep the bot online, move the whole folder to a machine that stays on and run it there. Leave node_modules behind, because the server installs the packages again. If you deploy from GitHub, keep .env out of the repository and put it on the server separately.
Once the files are uploaded, the Setup wizard in the new vPanel lets you pick the main file and version, and pushes to GitHub can deploy automatically. The .env template the Setup wizard creates uses the same variable name as the examples, DISCORD_TOKEN. vPanel can be switched to English from the menu in the top-right corner.
After pasting the token, press Start. When the line printed by the sample code appears in the console, the bot has logged in.
To link a Minecraft server's chat with a Discord channel, give the DiscordSRV plugin the token of the bot you just made.
References
- Building your first Discord Bot Discord Developer Docs
- Gateway Intents Discord Developer Docs
- discord.js discord.js
- Welcome to discord.py discord.py