-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatjpt.js
46 lines (39 loc) · 1.19 KB
/
chatjpt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const Discord = require('discord.js');
const client = new Discord.Client();
const fetch = require('node-fetch');
// Authenticate with the OpenAI API
const API_KEY = 'YOUR_OPENAI_API_KEY';
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', async (message) => {
if (message.content.startsWith('!chatgpt')) {
const text = message.content.substring('!chatgpt'.length).trim();
if (!text) {
message.channel.send('Please provide some text for ChatGPT to process.');
return;
}
const response = await fetch('https://api.openai.com/v1/engines/davinci/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY}`,
},
body: JSON.stringify({
prompt: text,
max_tokens: 60,
n: 1,
stop: null,
temperature: 0.5,
}),
})
.then((res) => res.json())
.catch((err) => {
console.error(err);
message.channel.send('An error occurred while processing your request.');
return;
});
message.channel.send(response.choices[0].text);
}
});
client.login('YOUR_DISCORD_BOT_TOKEN');