Skip to main content

Discord Bot

A discord bot is very powerful. One can program it to do anything.

  1. Go to to create an applicaiton and bot
  2. Go to Bot tab and enable the privileged Gateway Intents. This is needed for bot to read messages.
  3. Go to OAuth2 tab and select the bot scope. Select required permissions (choose administrator to get all permission). Copy the link and open it in a new tab. Select the server you want to add the bot to and click Authorize.
  4. Go to Bot tab and copy the Token. The token is used to login to the bot.

Sample Code


pip install -U

import discord

class MyClient(discord.Client):
async def on_ready(self):
print(f'Logged on as {self.user}!')

async def on_message(self, message: discord.Message):
print(f'Message from {}: {message.content}')
await"I am bot")

intents = discord.Intents.default()
intents.message_content = True

client = MyClient(intents=intents)'TOKEN_HERE')


npm i discord.js

Note that, JavaScript version requires GuildMessages and Guilds intents to be enabled, while Python version does not.

const { Client, IntentsBitField } = require("discord.js");
const token = "TOKEN_HERE";

// Create a new client instance
const client = new Client({
intents: [

// When the client is ready, run this code
client.once("ready", () => {
console.log("Bot is online!");

// Listen for messages
client.on("messageCreate", (message) => {
// Ignore messages from bots
if ( return;

// Log the message content to the console
console.log(`Message received: ${message.content}`);"hey I am bot");

// Replace 'YOUR_BOT_TOKEN' with your actual bot token

Use bot to clear all messages in a channel

client.on("messageCreate", (message) => {
// Ignore messages from bots
if ( return;

if (message.content === '!clear') { => {;