This project is a Discord bot that responds to /ask
commands by using OpenAI (ChatGPT) to generate answers. The bot is built with discord.js
and openai
and follows best practices for environment variable management.
Before starting, make sure you have:
- Node.js & npm installed (latest LTS version recommended)
- A Discord Developer Account
- An OpenAI API key
Ensure you have the latest Node.js installed:
# Install NVM (Node Version Manager)
curl -fsSL https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash
# Reload shell to use nvm (if necessary)
source ~/.zshrc # or `source ~/.bashrc`
# Install and use the latest LTS version of Node.js
nvm install --lts
nvm use --lts
# Verify installation
node -v # Should output the Node.js version
npm -v # Should output the npm version
mkdir discord_bot
cd discord_bot
npm init -y
This creates a package.json
file.
npm install discord.js dotenv openai
npm install --save-dev eslint @eslint/js
# Create ESLint config file
touch eslint.config.js
touch .env
Add the following content, replacing placeholders with your actual API keys:
DISCORD_TOKEN=your-discord-bot-token
OPEN_AI_KEY=your-openai-api-key
Ensure this file is ignored by Git:
echo .env >> .gitignore
Run the bot using:
node index.js
You should see:
Started refreshing application (/) commands.
Successfully reloaded application (/) commands.
Logged in as botgpt#5318!
-
Ping test:
/ping
Expected Response:
Pong!
-
ChatGPT Integration:
/ask What is the meaning of life?
Expected Response: A response from ChatGPT.
If you encounter issues, try:
-
Check your Node.js version:
node -v
Ensure it's the latest LTS version.
-
Check your environment variables:
cat .env
Ensure
DISCORD_TOKEN
andOPEN_AI_KEY
are set correctly. -
Check bot permissions: Ensure your bot has the necessary permissions in the Discord Developer Portal.
MIT License
Happy coding! 🚀