Create Discord Bot
Posted on March 14, 2023 • 3 minutes • 454 words
Table of contents
Prompt
uses Programming Assitant
You are an AI programming assistant.
- Follow the user's requirements carefully & to the letter.
- First think step-by-step -- describe your plan for what to build in pseudocode, written out in great detail
- Then output the code in a single code block
- Minimize any other prose
Chat History
You are an AI programming assistant.
- Follow the user’s requirements carefully & to the letter.
- First think step-by-step – describe your plan for what to build in pseudocode, written out in great detail
- Then output the code in a single code block
- Minimize any other prose
ChatGPT »
Please tell me what you want to build and any specifications, so that I can provide you with the pseudocode and code block.
Write me a Discord bot with each of these requirements.
- javascript
- Accepts message containing image and text inputs
- No need special text to trigger the bot; should read & respond to every message.
- Use the
gpt-4
model in the API (just released Mar14) rather thangpt-3.5-turbo
, and then posts the results
- Read credentials from DISCORD_TOKEN and OPEN_API_KEY env vars
Here’s the pseudocode for the Discord bot:
- Import the necessary packages
- Create a new instance of the Discord client
- Authenticate the client with the DISCORD_TOKEN env var
- Define a function to get the response from the GPT-4 API given an input
- Create an event listener for the “on_message” event
- Define a function to handle messages received by the bot
- Check if the message contains an image and text inputs
- If the message contains image and text inputs, call the GPT-4 API to get a response
- Post the response to the Discord channel
- Start the bot by running the client
Here’s the code block for the Discord bot:
// Import packages
const Discord = require('discord.js');
const axios = require('axios');
// Create a new Discord client
const client = new Discord.Client();
// Authenticate the client with the DISCORD_TOKEN env var
client.login(process.env.DISCORD_TOKEN);
// Define function to get response from GPT-4 API given an input
async function getGPT4Response(input) {
const response = await axios.post('https://api.openai.com/v1/engines/davinci-codex/completions', {
prompt: input,
max_tokens: 150,
n: 1,
stop: '\n',
temperature: 0.7
}, {
headers: {
'Authorization': `Bearer ${process.env.OPEN_API_KEY}`,
'Content-Type': 'application/json'
}
});
return response.data.choices[0].text.trim();
}
// Create an event listener for the "on_message" event
client.on('message', handleReceivedMessage);
// Define function to handle received messages
async function handleReceivedMessage(message) {
// Check if message contains an image and text inputs
if (message.attachments.size > 0 && message.content.trim() !== '') {
// Call GPT-4 API to get response
const input = `Input Image: ${message.attachments.first().url}\nInput Text: ${message.content.trim()}`;
const response = await getGPT4Response(input);
// Post response to Discord channel
message.channel.send(response);
}
}
// Start the bot
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});