-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
109 lines (89 loc) · 3.32 KB
/
bot.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
105
106
107
108
109
import dotenv from 'dotenv';
import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs';
import { Client, Events, GatewayIntentBits } from "discord.js";
import { OpenAI } from "openai";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
dotenv.config({ path: path.resolve(__dirname, '.env'), override: true });
let seanChannel;
let data = JSON.parse(fs.readFileSync("data.json", "utf8"));
let heartCount = data.heartCount;
// Create a new client instance
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessageReactions,
]
});
// Initialize OpenAI client
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY, // Use the environment variable
});
console.log("OpenAI API Key:", process.env.OPENAI_API_KEY.substring(0, 20) + "...");
// When the client is ready, run this code (only once).
client.once(Events.ClientReady, async (readyClient) => {
console.log(`Ready!! Logged in as ${readyClient.user.tag}`);
seanChannel = await client.channels.fetch("1293623915138256998");
//startHeartBeat();
});
// Log in to Discord with your client's token
client.login(process.env.DISCORD_BOT_TOKEN);
client.on(Events.MessageCreate, async (message) => {
if (message.channelId !== "1293623915138256998") return;
if (message.author.bot) return;
console.log(message);
try {
let messages = [
{ role: "system", content: "You are a helpful assistant in a Discord chat. You can analyze both text and images." },
{ role: "user", content: message.content }
];
// Check if the message contains an image
if (message.attachments.size > 0) {
const attachment = message.attachments.first();
if (attachment.contentType.startsWith('image/')) {
messages.push({
role: "user",
content: [
{ type: "text", text: "You are my museum tour guide." },
{
type: "image_url",
image_url: {
url: attachment.url
}
}
]
});
}
}
const response = await openai.chat.completions.create({
model: "gpt-4o", // Use the GPT-4 Vision model
messages: messages,
max_tokens: 300
});
const reply = response.choices[0].message.content;
message.reply(reply);
if (message.content.includes("❤️")) {
message.react("❤️");
}
} catch (error) {
console.error("Error calling OpenAI API:", error);
message.reply("Sorry, I encountered an error while processing your message.");
}
});
client.on(Events.MessageReactionAdd, (reaction, user) => {
console.log(reaction._emoji.name, user);
if (reaction._emoji.name == "❤️") {
heartCount++;
fs.writeFileSync("data.json", JSON.stringify({ heartCount: heartCount }));
console.log(heartCount);
}
});
function startHeartBeat() {
setInterval(() => {
seanChannel.send(`${heartCount} ❤️`);
}, 5000);
}