Discord bots can automate moderation, play music, track server stats, run games, and much more. The best part? Making a Discord bot is easier than most people think — and hosting it 24/7 for free is simple with FreeGameHost.
This guide covers everything from creating your Discord application to writing your first bot commands in both Python and Node.js, all the way to hosting your bot free 24/7 so it stays online even when your computer is off.
Before you write any code, you need to register your bot with Discord.
Visit discord.com/developers/applications and log in with your Discord account.
Click New Application, give it a name (this will be your bot's name), and click Create.
In the left sidebar, click Bot, then click Add Bot. Confirm the action. You can optionally set an avatar for your bot here.
Click Reset Token and copy the token shown. This is your bot's password — keep it secret and never share it publicly.
Scroll down on the Bot page and enable Server Members Intent and Message Content Intent. These are required for most bots to read messages and user data.
Go to OAuth2 → URL Generator. Select bot and applications.commands as scopes. Select the permissions your bot needs (Administrator for full access). Copy the generated URL and open it to add your bot to a server you manage.
Python is the most beginner-friendly option for Discord bots. We'll use discord.py, the most popular Python Discord library.
If you're testing locally first, open a terminal and run:
pip install discord.py
Create a file called bot.py and paste this code:
import discord
from discord.ext import commands
# Replace with your actual bot token
TOKEN = 'YOUR_BOT_TOKEN_HERE'
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_ready():
print(f'Bot is online as {bot.user}')
@bot.command()
async def hello(ctx):
await ctx.send(f'Hello, {ctx.author.mention}! 👋')
@bot.command()
async def ping(ctx):
latency = round(bot.latency * 1000)
await ctx.send(f'Pong! Latency: {latency}ms')
@bot.command()
async def serverinfo(ctx):
guild = ctx.guild
await ctx.send(
f'Server: {guild.name}\n'
f'Members: {guild.member_count}\n'
f'Created: {guild.created_at.strftime("%Y-%m-%d")}'
)
bot.run(TOKEN)
This bot responds to three commands: !hello, !ping, and !serverinfo. To test locally, run python bot.py in your terminal.
Discord now recommends slash commands over prefix commands. Here's how to add them:
import discord
from discord import app_commands
TOKEN = 'YOUR_BOT_TOKEN_HERE'
intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
@tree.command(name='ping', description='Check bot latency')
async def ping(interaction: discord.Interaction):
latency = round(client.latency * 1000)
await interaction.response.send_message(f'Pong! {latency}ms')
@client.event
async def on_ready():
await tree.sync()
print(f'Logged in as {client.user}')
client.run(TOKEN)
If you prefer JavaScript, discord.js is the most popular and well-documented option.
mkdir my-discord-bot cd my-discord-bot npm init -y npm install discord.js
Create a file called index.js:
const { Client, GatewayIntentBits } = require('discord.js');
const TOKEN = 'YOUR_BOT_TOKEN_HERE';
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
]
});
client.once('ready', () => {
console.log(`Bot is online as ${client.user.tag}`);
});
client.on('messageCreate', async (message) => {
if (message.author.bot) return;
if (message.content === '!hello') {
await message.reply(`Hello, ${message.author}!`);
}
if (message.content === '!ping') {
const latency = Math.round(client.ws.ping);
await message.reply(`Pong! Latency: ${latency}ms`);
}
if (message.content === '!serverinfo') {
const guild = message.guild;
await message.reply(
`Server: ${guild.name}\nMembers: ${guild.memberCount}`
);
}
});
client.login(TOKEN);
Run it locally with node index.js to test.
.env file with TOKEN=your_token_here and use the dotenv package to load it. This prevents accidentally committing your token to GitHub.
Once your bot works locally, you need to host it so it stays online 24/7 — even when your computer is off. FreeGameHost offers free Discord bot hosting for both Python and Node.js bots.
Click Create Server and select Discord Bot as the server type. Choose Python or Node.js to match your bot's language.
Use the File Manager in your control panel to upload your bot files (bot.py or index.js, plus any other files your bot needs).
Open the Console tab in your control panel and run pip install discord.py (Python) or npm install (Node.js) to install your bot's dependencies.
Set the startup command to python bot.py or node index.js, then click Start. Your bot will come online and stay online 24/7.
Once your basic bot is running, here are some popular commands to add:
Ready to host your Discord bot free 24/7?
Get Free Discord Bot Hosting →Related: Free Discord Bot Hosting • Free Minecraft Server Hosting