This repository contains a Discord chatbot powered by OpenAI's GPT-4 model. It allows you to interact with the AI model within a Discord server. The chatbot listens to messages sent by users and replies using GPT-4, making it a great addition to any server for fun, engaging, and informative conversations.
- Clone the repository:
git clone https://github.com/aaronmansfield5/discord-gpt-4-chatbot.git
cd discord-gpt-4-chatbot
- Install the required dependencies:
npm install discord.js dotenv openai
- Create a .env file in the project root directory and add your Discord bot token and OpenAI API key:
GPT_API_KEY=your_openai_api_key
DISCORD_BOT_TOKEN=your_discord_bot_token
Replace your_openai_api_key
and your_discord_bot_token
with your actual OpenAI API key and Discord bot token.
- Go to the Discord Developer Portal and log in with your Discord account.
- Click the "New Application" button and give your application a name.
- Go to the "Bot" tab and click the "Add Bot" button. Confirm by clicking "Yes, do it!".
- Under the "Bot" tab, you can see the bot's token. Click the "Copy" button to copy the token.
- Go to the OpenAI Platform and sign up for an account or log in if you already have one.
- Once logged in, go to the API keys section.
- Generate a new API key or use an existing one.
- Start the bot by running:
node main.js
-
Invite the bot to your Discord server using the invite link provided by the Discord Developer Portal.
-
Once the bot is online, it will start listening to messages and respond using GPT-4.
The main components of the code are:
- Importing required libraries and initializing the Discord client and OpenAI API client.
- Loading API keys from the .env file.
- Setting up event listeners for the ready and messageCreate events.
- Implementing the generateResponse function, which sends a message to the OpenAI API and processes the response.
- Sending the AI-generated response to the Discord channel.
- When a message is received, the generateResponse function is called with the message content as the prompt. It sends the prompt to the OpenAI API and receives a response generated by the GPT-4 model. The bot then sends this response back to the Discord channel.
Feel free to submit issues or pull requests if you find any problems or have suggestions for improvements. Contributions are welcome and appreciated!