A Discord bot that uses Google's Gemini AI to respond to messages in your server.
- Responds to messages using Google Gemini 2.5 Flash model
- Automatic message handling with AI-generated responses
- Configurable response length (max 40 lines)
- Bot ignores messages from other bots to prevent loops
- Web interface with bot information and Discord invite button
- Beautiful, responsive UI built with Express, EJS, HTML, and CSS
- Node.js (v16 or higher)
- A Discord account
- A Google AI Studio account
-
Clone or download this repository
-
Install dependencies:
npm installThis will install:
discord.js- Discord API wrapper@google/genai- Google Generative AI SDKdotenv- Environment variable managerexpress- Web server frameworkejs- Embedded JavaScript templating
- Go to Discord Developer Portal
- Click New Application, enter a name, and click Create
- In the left sidebar, click Bot
- Click Reset Token (you may need to enter your password)
- Copy the token immediately - it's only shown once
- Save it securely (you'll add it to
.envfile)
- In Developer Portal, go to OAuth2 → URL Generator
- Under "Scopes", select bot
- Under "Bot Permissions", select:
- Send Messages
- Read Messages/View Channels
- Read Message History
- (For testing, you can use Administrator, but avoid in production)
- Copy the generated URL at the bottom
- Paste it in your browser, select your server, and click Authorize
- In Developer Portal, go to Bot → Privileged Gateway Intents
- Enable these intents:
- ✅ Message Content Intent (required to read message content)
- ✅ Server Members Intent (optional)
- ✅ Presence Intent (optional)
- Click Save Changes
- Go to Google AI Studio
- Sign in with your Google account
- Click Get API Key or Create API Key
- Select an existing project or create a new one
- Copy the generated API key
- Keep it private - treat it like a password
- Create a
.envfile in the root directory (copy from.env.example):
cp .env.example .env- Open
.envand add your keys:
TOKEN=your_discord_bot_token_here
GEMINI_API_KEY=your_gemini_api_key_here
PORT=3000
DISCORD_CHANNEL_LINK=https://discord.gg/your-invite-link.env to version control! It's already in .gitignore.
index.js- Main bot file that handles Discord events + Express web serverGeimiai.js- Google Gemini AI integration moduleviews/index.ejs- EJS template for the web interfacepublic/css/style.css- Stylesheet for the web page.env- Environment variables (tokens and API keys)package.json- Project dependencies and metadata
Discord.js Gateway Intents (index.js):
GatewayIntentBits.Guilds- Access to server informationGatewayIntentBits.GuildMessages- Detects when messages are sentGatewayIntentBits.MessageContent- Reads the actual message content
Message Flow:
- User sends a message in Discord server
- Bot receives message via
messageCreateevent - Checks if message is from another bot (ignores if true)
- Sends message content to Gemini AI via
aireponces - Receives AI-generated response
- Replies to the user with their name + AI response
Web Server (index.js):
- Express server runs on port 3000 (configurable via
.env) - Serves a beautiful web interface with bot information
- Features a button that opens your Discord channel invite link
- Uses EJS templating to dynamically display bot name and channel link
AI Configuration (Geimiai.js):
- Model:
gemini-2.5-flash(fast and efficient) - Max response: 40 lines (configured via systemInstruction)
- Thinking budget: 0 (disabled for faster responses)
Start the bot with:
node index.jsOr update package.json to add a start script:
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
}Then run:
npm startYou should see:
Web server running on http://localhost:3000
Logged in as YourBotName#1234!
Open your browser and go to:
http://localhost:3000
You'll see a beautiful page with:
- Bot status and information
- Feature list
- Technical specifications
- "Open Discord Channel" button - Click this to join your Discord server
- Built with badges showing the tech stack
Once the bot is online in your server:
- Send any message in a channel where the bot has access
- The bot will reply with an AI-generated response
- The reply format:
YourUsername:- [AI Response]
- Visit
http://localhost:3000in your browser - View bot information, features, and statistics
- Click "Open Discord Channel" button to join your server
- Share this URL with others to invite them to your Discord!
- ✅ Make sure Message Content Intent is enabled in Developer Portal
- ✅ Check that bot has permission to read and send messages in the channel
- ✅ Verify your
TOKENin.envis correct
- ✅ Check your
GEMINI_API_KEYin.env - ✅ Make sure you created the key at Google AI Studio
- ✅ Verify your Discord token is valid
- ✅ Check your internet connection
- ✅ Make sure
node index.jsis running without errors
- ✅ Make sure the bot is running (
node index.js) - ✅ Check if port 3000 is available (or change PORT in
.env) - ✅ Try accessing
http://localhost:3000orhttp://127.0.0.1:3000
- ✅ Update
DISCORD_CHANNEL_LINKin your.envfile - ✅ Get your server invite link from Discord: Right-click channel → Invite People → Copy Link
🔒 Never share or commit:
- Discord bot token
- Gemini API key
.envfile
✅ Always use:
.env.examplefor documentation.gitignoreto exclude sensitive files
ISC
Created for learning Discord bot development with AI integration.
For detailed setup steps, see step.md or stepsai.md.