-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
44 lines (35 loc) · 1.03 KB
/
index.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
require('dotenv').config();
const { Configuration, OpenAIApi } = require('openai');
const { Client, GatewayIntentBits } = require('discord.js');
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY
});
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
async function gptResponse(prompt) {
const openai = new OpenAIApi(configuration);
const response = await openai.createCompletion({
model: 'text-davinci-003',
prompt,
max_tokens: 100,
temperature: 0.9
});
return response.data.choices[0].text;
}
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('messageCreate', async message => {
if (message.content === 'ping') message.reply('Pong!');
if (message.content.startsWith('!gpt')) {
const prompt = message.content;
const response = await gptResponse(prompt);
message.reply(response);
}
});
client.login(process.env.DISCORD_BOT_TOKEN);