Skip to content

fac30sb/discord-chatbot--Oleg

Repository files navigation

Discord AI Chat-Bot

link at the discord docs: https://discordjs.guide/#before-you-begin

1. Create a new server on discord

  1. Create a new server:

    Screenshot 2024-02-13 at 13 49 23

2. Set up the bot on your server

  1. Go to https://discord.com/developers/applications/

  2. Create the new app

  3. Use OAuth2 --> URL generator and generate a url to invite your app (bot) to your server as a channel member

  4. Issue(reset) the token to use it later in your code and enable the bot

    Screenshot 2024-02-06 at 13 57 40

    Screenshot 2024-02-06 at 13 05 40

    Screenshot 2024-02-06 at 14 11 42


2. Set up the project

  1. Create the project folder and navigate in there

    mkdir discord_bot && cd discord_bot
    
  2. Set up the project:

    npm init -y
    
    npm install discord.js
    
    npm install -g nodemon
    
    npm install --save-dev eslint
    

Important

3. Create the config.json, place it in your root directory and use config-example.json as a template

{
   "token": "<your token>", // you can issue the token for your bot using https://discord.com/developers/ portal
   "apiKey": "<your APIKey>", // you can issue the token for your bot using https://platform.openai.com portal
   "guildId": "<your GuildID>", // Discord calls servers as "guilds", so copy-paste your server ID
   "clientId": "<your Client ID>"  // Client means bot here, so copy-paste your bot ID here
}

  1. Launch the app in the monitoring mode:

    nodemon
  2. Now your bot is "online" but can't talk yet

    Screenshot 2024-02-06 at 14 30 23

    Screenshot 2024-02-06 at 14 22 32

3. Returning the test messages

  1. Modify the script

    index.js

    const { token } = require('../config.json');
    const { Client, IntentsBitField, Partials } = require('discord.js');
    
    
    const client = new Client
    ({
      intents: 
      [
        IntentsBitField.Flags.Guilds,
        IntentsBitField.Flags.GuildMembers,
        IntentsBitField.Flags.GuildMessages,
        IntentsBitField.Flags.MessageContent,
        IntentsBitField.Flags.GuildMessageTyping,
        IntentsBitField.Flags.DirectMessages,
        IntentsBitField.Flags.DirectMessageReactions,
        IntentsBitField.Flags.DirectMessageTyping,
      ],
      partials: 
      [
        Partials.Message, 
        Partials.Channel, 
        Partials.Reaction
      ]
    });
    
    
    client.on('ready', (c) => {
      console.log(`βœ… ${c.user.tag} is online.`);
    });
    
    
    client.on('messageCreate', message => {
      console.log(`Received message from ${message.author.tag} in ${message.guild ? `guild ${message.guild.name}` : 'DM'}. Content: ${message.content}`);
      
      if (message.author.bot) return;
      
      if (message.content === 'ping') {
        message.channel.send('Pong!');
      }
      
      if (message.content === 'hello') {
        message.channel.send('Hello!');
      }
    });
    
    
    client.login(token);
  2. Result

    Screenshot 2024-02-06 at 16 27 45

4. Set up slash commands

Note

Slash commands in Discord are special commands that start with a forward slash (/) and allow users to perform various pre-defined actions or invoke specific features

  1. Create a folder and modules with slash commands and set up the export

    https://github.com/fac30/discord-chatbot--Oleg-Loza/tree/main/commands/utility

  2. Create registercommands.js to register the the slash commands and set up the export

    https://github.com/fac30/discord-chatbot--Oleg-Loza/blob/main/registercommands.js

  3. You will need to copy the server ID and the bot ID. Add them to your config as guildID and clientID

    Screenshot 2024-02-16 at 22 23 41 Screenshot 2024-02-16 at 22 17 30

    {
       "token": "<your token>", // you can issue the token for your bot using https://discord.com/developers/ portal
       "apiKey": "<your APIKey>", // you can issue the token for your bot using https://platform.openai.com portal
       "guildId": "<your GuildID>", // Discord calls servers as "guilds", so copy-paste your server ID
       "clientId": "<your Client ID>"  // Client means bot here, so copy-paste your bot ID here
    }
  4. Modify the index.js to import and register slash commands each time the server launches:

    const { commands } = require('../registercommands'); 		// Import the commands collection from the registercommands module
    
    const commands = new Map(); // Create a Map to store commands
  5. Test the commands:

    Screenshot 2024-02-13 at 14 04 46 Screenshot 2024-02-13 at 14 11 01

4. Set up the conversation and requests to OPEN AI API

  1. Modify the script

    the link at the final script: index.js

  2. Result:

    Screenshot 2024-02-16 at 17 07 24 Screenshot 2024-02-16 at 17 02 31

5. Deploying to a cloud server and run to be available 24 hours a day

Note

PM2 is a daemon process manager that will help you manage and keep your application online 24/7. So it will help keep our app live and running.

  1. Launch the server

    Screenshot 2024-02-14 at 16 45 41
  2. Clone the repository

    git clone
    
  3. Install nodejs and npm

    apt-get installl node 
    
  4. Install dependencies

    npm install discord.js
    npm install axios
    
  5. Instal PM2:

    sudo npm install -g pm2
  6. Start PM2

    pm2 start index.js
  7. Confirm

    pm2 list index.js

    Screenshot 2024-02-12 at 18 01 27



6. Using Your Bot πŸ€– πŸ’¬

If all went well and you've added your bot successfully to a guild, you should be able to interact with it by mentioning, followed by a prompt, like so:

bro Hey, can you tell me a fun fact

or:

bro are you a bot?

Releases

No releases published

Packages

No packages published