A Discord chat bot that acts as a helpful AI assistant, using ChatGPT
Explore the docs »
View Demo
·
Report Bug
·
Request Feature
Table of Contents
Halimah and Adel's second project for Founders and Coders bootcamp: a Discord AI bot, made using asynchronous functions and Node.js.
If you want to install this project on your local machine and play around with it, follow these steps:
- Get a free API Key at OpenAI
- Create a bot on Discord Developers
- Assign the permissions you want the bot to have (ensure you select
bot
andapplications.commands
) and generate a URL - Invite your bot to a server you manage by pasting the generated link
- Get your bot's token
- Clone the repo
git clone https://github.com/fac30/discord-ai-bot--halimah-adel.git
- Install NPM packages
npm install
- Enter your API and Discord Token in
.env
TOKEN=ENTER-TOKEN-HERE API_KEY=ENTER-API-KEY-HERE
- Launch your bot in the terminal so it goes online
node index.js
(Note: you can optionally install nodemon
as a global package so you don't have to launch your bot in the terminal every time you make a change to the code.)
npm install -g nodemon
If you want to play around with the bot as it's currently configured, ensure you type !
before your message, so the bot knows you're talking to it instead of another user. Then ask it any question you'd like.
To ensure the API Key isn't overloaded, there is a token limit, so you might not get as full an answer as you expect.
You can change this by increasing the max_tokens
value in index.js
, line 83:
max_tokens: 25,
Make sure to save after making this change!
- Set up a new Node.js project with
.env
,openai
anddiscord.js
libraries - Initialise bot and start listening for messages
- Add message handling
- Optimise event handling
- Integrate OpenAI into the bot
- Manage OpenAI response
- Add command prefix
- Implement error handling
If we have time, we aim to attempt some of these stretch goals:
- Enable the bot to message users directly
- Implement dialogue boxes and interactive responses
- Create private channels
- Automated moderation
- Add multimedia responses
See the open issues for a full list of proposed features (and known issues).
"Writing tests is an important part of software development. It allows you to ensure that your code works as intended and catches any potential bugs or errors before they make it into production."
In our Discord Bot project, we use the built in Node.js test library.
It requires the following steps:
-
import the tester libary in each test file
const assert = require('assert'); const test = require('node:test');
-
write a test using assert method, create an expected and received value, which the test will compare with the assert.
Method Description assert.deepEqual Checks if two values are equal assert.strictEqual Checks if two values are equal assert.notDeepEqual Checks if two value are not equal assert.notStrictEqual Checks if two value are not equal assert.fail throw an Assertion Error assert.ifError Throw a specified error if the specified error evaluates to true assert.ok Check if a value is true -
An example of a test
test('description of the test', () => { try { assert.strictEqual(typeof variable, 'object', 'variable should be an object'); assert.ok(variable instanceof Class, true, 'variable should be an instance of discord.js Class'); } catch (error) { assert.fail(`event failed: ${error.message}`); } });
-
Add the testable test file to the package.json
"scripts": { "name-test": "node ./tests/test.nameOfTest.js", },
-
run the test in the terminal
npm run test
If you'd like to contribute to this project, you can fork the project and open a pull request.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature
) - Commit your Changes (
git commit -m 'Add some feature'
) - Push to the Branch (
git push origin feature
) - Open a Pull Request
Project Link: https://github.com/fac30/discord-ai-bot--halimah-adel
We used a number of resources to understand how to use OpenAI's API and Discord.js to create an AI chat bot. This includes official documentation:
- Discord.js official documentation
- OpenAI API documentation
And online tutorials. Thanks to the following creators for their excellent tutorials on creating a Discord AI bot:
- Beau Carnes: creating a Discord bot
- UnderCtrl: creating a Chat GPT Discord bot
- Omnidev: creating a Discord AI chat bot
- EvenMoreCode: creating a Discord File Structure: handling commands and events
And, of course, huge thanks to othneildrew for the comprehensive README template that we built this README with!