simple node.js script to create a discord bot for openai. Using gpt-3.5-turbo. Will be eventually updating script to use gpt 4 once its available
This script was written entirely using ChatGPT. I understand the basics of how this work, but if you're more advanced in node.js then you will be able to do a lot more than I can. If you have issues, paste the contents of the script into chatgpt, and ask it for help. It will be able to provide you a lot of info.
-
git clone the repository
-
npm install
-
Create an openai account at https://openai.com/api/
-
Open your account from the top right icon, then select "view api keys"
-
Select "Create New Api Key" and copy it
-
Paste the key into the config.json file where it says "openai token"
-
Create a discord api token by going to https://discord.com/developers/applications/
-
Select "New Application" and give it a name and save
-
Select Bot from the left hand panel
-
Press the "Add Bot" button, and press the "Yes, do it" button
-
Copy the token, and paste into the config.json file where it says "discord token" then copy the client id and paste where it says "your app id". Note client ID and app ID are the same thing. Discord just gives it 2 different names for some weird inconsistent reason
-
Select the intents you need, generally it should only be SERVER MEMBERS INTENT, and MESSAGE CONTENT INTENT,
-
Save Changes
-
From the left hand side, under OAuth2, select URL Generator
-
Select the Scopes. Will only need BOT
-
Select the Text Permissions that are required. These should be only
Send Messages
Send Messages in Threads
Embed Links
Attach Files
Read Message History
-
Copy the Generated URL, and paste into the URL bar of the browser and authorize the bot.
-
run the commandreg.js to register the commands to discord
node commandreg.js
- Start the bot. In Linux under Ubuntu 20.04 that this was tested on that should be
node main.js
To communicate with the bot, a user will type
/chat
or
/dalle
Followed by their question or image they want to generate. This is an example of what it will look like.
/chat tell me the weather of the bermuda triangle
/dalle staind glass window. Dog in a fighter jet. Sun in the bottom left corner. Lens flare shining.
If the user wants to clear their chat history to start a new session, then type
/clearchathistory
This will save the chat history in an archive in the DB, and start the chat history like new.
- Create a new user:
sudo useradd -r discordbot
- Set where you want the user's home directory to be, this could be where the script is stored. I suggest /opt/nodejsdiscord as that is what I used. Note, the directory must exist already:
sudo usermod -d /var/empty discordbot
- Set the user's shell to /sbin/nologin:
sudo usermod -s /sbin/nologin discordbot
-
Place the config.json in the same directory as the node.js script
-
Change the readwrite permission to read only from the owner
chmod 600 /path/to/config.json
- Change the owner to whomever will be running the script.
chown discordbot:discordbot /path/to/config.json
If you'd like to run the program in the background I suggest creating a systemd service. Alternatively, the screen command can also be used but will not resume upon reset of the server.
- Create the systemd service file:
sudo nano /etc/systemd/system/jsdiscordbot.service
- Add the following content to the service file:
[Unit]
Description=Discord Bot Service
After=network.target
[Service]
Type=simple
User=discordbot
WorkingDirectory=/opt/nodejsdiscord
ExecStart=/usr/bin/node /opt/nodejsdiscord/main.js
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
Note: Replace /path/to/main.js with the actual path to the main.js script on your system.
- Make sure the main.js script has the correct permissions:
sudo chown discordbot:discordbot /path/to/main.js
sudo chmod 700 /path/to/main.js
- Reload the systemd configuration:
sudo systemctl daemon-reload
- Start the service:
sudo systemctl start jsdiscordbot.service
- Enable the service to start automatically at boot:
sudo systemctl enable jsdiscordbot.service
With these steps, the main.js script should now run as the discordbot user, with the least privilege necessary to execute the script, and will start automatically at boot.