How to Add a Bot to Discord¶
1. Creating a Discord Bot¶
To add a bot to Discord, you first need to create one through the Discord Developer Portal.
Step 1: Create a New Application¶
- Visit the Discord Developer Portal.
- Click the "New Application" button.
- Give your application a name and click "Create".
Step 2: Create a Bot¶
- In your application dashboard, navigate to the "Bot" tab on the left sidebar.
- Click "Add Bot", then confirm by clicking "Yes, do it!".
- Your bot will now appear under the "Bot" section.
Step 3: Get the Bot Token¶
- Under the "Bot" tab, locate the "Token" section.
- Click "Reset Token", then "Copy" the generated token.
- Store this token securely! It is the authentication key for your bot to connect to Discord. Do not share it publicly.
2. Adding the Bot to a Discord Server¶
Once the bot is created, you need to invite it to your server.
Step 1: Generate the OAuth2 URL¶
Use the following link to authorize your bot to a server, replacing YOUR_CLIENT_ID
with your bot's Client ID:
https://discord.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&permissions=8&scope=bot
YOUR_CLIENT_ID
→ You can find this in the General Information section of your application.permissions=8
→ Grants administrator permissions. You can modify this as needed.
Step 2: Invite the Bot¶
- Open the authorization link in your browser.
- Select the server you want to add the bot to.
- Click "Continue", then "Authorize".
- Complete the CAPTCHA to verify the request.
Your bot is now added to the selected Discord server.
3. Running the Bot¶
Now that the bot is added, you need to start it using the token.
Example: Python Bot using Discord.py¶
To run your bot, install discord.py
(if not already installed):
pip install discord.py
Then create a Python script (bot.py
) and use the following code:
import discord
from discord.ext import commands
TOKEN = "YOUR_BOT_TOKEN"
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="!", intents=intents)
@bot.event
async def on_ready():
print(f"Logged in as {bot.user}")
@bot.command()
async def hello(ctx):
await ctx.send("Hello, I am your bot!")
bot.run(TOKEN)
Replace "YOUR_BOT_TOKEN"
with your actual bot token.
Running the Bot¶
Run the script:
python bot.py
Your bot should now be online and responding to commands.
4. Troubleshooting¶
- Bot is offline? Ensure that:
- The bot script is running.
- The correct token is used.
- The bot has the required permissions in the server.
- Permission issues? Modify the permissions parameter in the invite URL.
Conclusion¶
You have successfully created, added, and run a bot on Discord! You can now expand its functionality by adding commands and integrating it with APIs.