Skip to content

fac30sb/discord-ai-bot--halimah-adel

Repository files navigation

Contributors Forks Stargazers Issues Halimah's LinkedIn Adel's LinkedIn


Waving robot to imply Artificial Intelligence

Discord AI bot

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
  1. About The Project
  2. Installation
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgements

About The Project

Discord AI Bot in use

Halimah and Adel's second project for Founders and Coders bootcamp: a Discord AI bot, made using asynchronous functions and Node.js.

(back to top)

Built With

  • Node
  • Discord
  • Dotenv
  • OpenAI

(back to top)

Installation

If you want to install this project on your local machine and play around with it, follow these steps:

  1. Get a free API Key at OpenAI
  2. Create a bot on Discord Developers
  3. Assign the permissions you want the bot to have (ensure you select bot and applications.commands) and generate a URL
  4. Invite your bot to a server you manage by pasting the generated link
  5. Get your bot's token
  6. Clone the repo
    git clone https://github.com/fac30/discord-ai-bot--halimah-adel.git
  7. Install NPM packages
    npm install
  8. Enter your API and Discord Token in .env
    TOKEN=ENTER-TOKEN-HERE
    API_KEY=ENTER-API-KEY-HERE
    
  9. 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

(back to top)

Usage

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!

(back to top)

Roadmap

  • Set up a new Node.js project with .env, openai and discord.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

Stretch goals

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).

(back to top)

Testing

"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

Contributing

If you'd like to contribute to this project, you can fork the project and open a pull request.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature)
  3. Commit your Changes (git commit -m 'Add some feature')
  4. Push to the Branch (git push origin feature)
  5. Open a Pull Request

(back to top)

Contact

Project Link: https://github.com/fac30/discord-ai-bot--halimah-adel

(back to top)

Acknowledgements

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:

And online tutorials. Thanks to the following creators for their excellent tutorials on creating a Discord AI bot:

And, of course, huge thanks to othneildrew for the comprehensive README template that we built this README with!

(back to top)

Releases

No releases published

Packages

No packages published