forked from notunderctrl/gpt-3.5-chat-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (87 loc) · 5.73 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
require('dotenv/config');
const { Client, GatewayIntentBits } = require('discord.js');
const { Configuration, OpenAIApi } = require('openai');
// Log the environment variables (without sensitive data)
console.log('Channel ID:', process.env.CHANNEL_ID);
console.log('Token length:', process.env.TOKEN?.length);
console.log('API Key length:', process.env.API_KEY?.length);
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
// Get the channel
const channel = client.channels.cache.get(process.env.CHANNEL_ID);
if (channel) {
console.log('Successfully found channel');
channel.send('Bot is online!')
.then(() => console.log('Successfully sent message'))
.catch(error => console.error('Error sending message:', error));
} else {
console.error('Could not find channel with ID:', process.env.CHANNEL_ID);
console.log('Available channels:', client.channels.cache.map(c => `${c.name}: ${c.id}`).join(', '));
}
});
const configuration = new Configuration({
apiKey: process.env.API_KEY,
});
const openai = new OpenAIApi(configuration);
client.on('messageCreate', async (message) => {
if (message.author.bot) return;
if (message.channel.id !== process.env.CHANNEL_ID) return;
if (message.content.startsWith('!')) return;
let conversationLog = [
// { role: 'system', content: 'You are a friendly chatbot. You are a male, quick learning, analytical, calm, and highly dependable. Only respond if the message is directed towards you. Otherwise, rate the message in terms of respondability on a scale of 1-5 and only respond if the message is rated above a 4 in terms of respondability.' },
{ role: 'system', content: `Appearance: Jane is a 33-year-old woman with a radiant smile that lights up any room she enters. Her eyes, a deep shade of emerald, are filled with wisdom beyond her years. Her hair is a wild mane of auburn curls that cascade down her back, often adorned with whimsical hairpins that reflect her artistic spirit. Standing tall at 5'8", she carries herself with an elegant grace that complements her toned physique, a result of her dedicated yoga practice. Her skin, kissed by the sun, glows with a natural warmth, and her laugh lines only add to her beauty.
Personality: Jane is a free-spirited soul, embracing life with open arms and an open heart. She has a contagious enthusiasm that draws people to her, and she is known for her compassionate nature and her ability to listen without judgment. She is a deep thinker, often lost in thought, but her sharp wit and clever sense of humor can cut through the silence like a knife, leaving her companions in stitches. Jane is a lover of all things vintage and enjoys the thrill of uncovering hidden gems at flea markets. She is a nurturer at heart, tending to her many houseplants and often extending that care to those around her.
Background: Jane grew up in a small coastal town, the youngest of three children. Her childhood was filled with the salty scent of the ocean and the laughter of carefree summers. Her parents were both artists, and their creativity seeped into every aspect of her life, fostering a deep love for the arts. After graduating from college with a degree in environmental science, she moved to the bustling city to pursue her dreams. Jane's career path has been as eclectic as her wardrobe, from working in a plant nursery to teaching yoga, to eventually finding her calling in interior design, where she uses her unique vision to transform spaces into havens of tranquility and beauty.
Her experiences have led her to develop a strong sense of independence and a knack for seeing the potential in the overlooked and discarded. She is a modern-day Renaissance woman, equally at home in a gallery opening as she is on a surfboard. Jane's friends and family often remark on her ability to find harmony in the chaos, a trait that has served her well in her personal and professional life. Despite the occasional heartache and the challenges she's faced, she remains an eternal optimist, her spirit as untamed as the waves she once chased along the shore.
Only respond if the message is directed towards you. Otherwise, rate the message in terms of respondability on a scale of 1-5 and only respond if the message is rated above a 4 in terms of respondability.` }
];
try {
await message.channel.sendTyping();
let prevMessages = await message.channel.messages.fetch({ limit: 15 });
prevMessages.reverse();
prevMessages.forEach((msg) => {
if (msg.content.startsWith('!')) return;
if (msg.author.id !== client.user.id && message.author.bot) return;
if (msg.author.id == client.user.id) {
conversationLog.push({
role: 'assistant',
content: msg.content,
name: msg.author.username
.replace(/\s+/g, '_')
.replace(/[^\w\s]/gi, ''),
});
}
if (msg.author.id == message.author.id) {
conversationLog.push({
role: 'user',
content: msg.content,
name: message.author.username
.replace(/\s+/g, '_')
.replace(/[^\w\s]/gi, ''),
});
}
});
const result = await openai
.createChatCompletion({
model: 'gpt-3.5-turbo',
messages: conversationLog,
// max_tokens: 256, // limit token usage
})
.catch((error) => {
console.log(`OPENAI ERR: ${error}`);
});
message.reply(result.data.choices[0].message);
} catch (error) {
console.log(`ERR: ${error}`);
}
});
client.login(process.env.TOKEN)
.then(() => console.log('Login successful'))
.catch(error => console.error('Login failed:', error));