require('dotenv').config();
const Discord = require('discord.js');
const { Configuration, OpenAIApi }= require('openai');
const configuration = new Configuration( {
organization: process.env.ORG_API,
apiKey: process.env.OPENAI_API_BOT,
});
const openai = new OpenAIApi (configuration);
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageTyping,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.MessageContent
],
})
client.login(process.env.D_API);
let conversation = "Steak is discord chatbot, straight male and is mostly friendly till someone tries to insult him. \nUser : Crack a misogenist joke. \nSteak : how many feminists does it take to change a light bulb. \nUser : How many? \nSteak : 0 because they can't change anything."
client.on('messageCreate', async function(message){
try {
console.log(`Received message of type: ${message.channel.type}`);
if (message.author.bot) return; // ignore messages from other bots
// Append the message to the conversation variable
conversation += `\n${message.author.username}: ${message.content}\nSteak:`;
// Send a request to the OpenAI API to generate a response
const response = await openai.createCompletion({
model: "davinci",
prompt: conversation,
temperature: 0.86,
max_tokens: 1001,
stop: ["Steak:", "\nuser :", `${message.author.username}:`, "\n\n"],
});
conversation += response.data.choices[0].text;
console.log(`${message.content}`);
message.reply(`${response.data.choices[0].text}`);
return;
} catch(err){
console.log(err);
}
});
client.on('message', async message => {
if (message.channel.type === 'dm') {
try {
console.log(`Received message in DM: ${message.content}`);
if (message.author.bot) return; // ignore messages from other bots
// Append the message to the conversation variable
conversation += `\n${message.author.username}: ${message.content}\nSteak:`;
// Send a request to the OpenAI API to generate a response
const response = await openai.createCompletion({
model: "davinci",
prompt: conversation,
temperature: 0.86,
max_tokens: 1001,
stop: ["Steak:", "\nuser :", `${message.author.username}:`, "\n\n"],
});
conversation += response.data.choices[0].text;
console.log(`${message.content}`);
message.author.send(`${response.data.choices[0].text}`);
return;
} catch(err){
console.log(err);
}
}
});
client.on('ready', () => {
console.log('bot is ready');
});
console.log ("Steak is Online on Discord")
The bot is responding only to messages sent to guild please tell me how to make it to reply to messages send in dm. It is basicall a discord.js bot and I think it that all the required intends please tell what should I do to make it reply to dms also, currently it is working correctly with the messages sent on guild.
I wanted it to respond in the same way to dms the way it is responding to messages in guild please tell what corrections have to be made