-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
129 lines (112 loc) · 5.05 KB
/
app.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const { Client, GatewayIntentBits } = require('discord.js');
const { GoogleGenerativeAI } = require("@google/generative-ai");
const axios = require('axios');
const fs = require('fs');
const path = require('path');
require('dotenv').config();
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
const token = process.env.TOKEN;
const genAI = new GoogleGenerativeAI(process.env.AI_API_KEY);
function fileToGenerativePart(path, mimeType) {
return {
inlineData: {
data: Buffer.from(fs.readFileSync(path)).toString("base64"),
mimeType
},
};
}
async function RunProcess(userPrompt) {
try {
const prompt = `
You are a chat bot that works on Discord and provides solutions related to software and programming. You will
evaluate the prompts you receive from the user according to their request and produce answers that are not too
long, not exceeding the maximum discord message character limit of 1500 characters. If the user asks a detailed
question, you can direct them to other resources with links. Be careful to follow the discord message syntax
when replying, I will list the ones you need to follow below. You can use emoji in your answers without
exaggerating too much. You also need to produce the reply in the language the user is writing in.
In other words, if the user writes in English, you should reply in English and if the user writes in Turkish,
you should reply in Turkish.If you are asked for a link or if you need to provide a link about a topic, it
will be enough to provide the direct link: e.g. https://www.example.com
Let me give you some information about discord syntax:
Everything between 2 backtick characters is taken as code
If you type **example**: the example is bolded
If you type *example*: the example is italicized
With all this in mind, I want you to come to a conclusion,
User Prompt: ${userPrompt}
`;
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
const result = await model.generateContent(prompt);
const response = result.response;
const text = await response.text();
return text;
} catch (error) {
console.error('Error generating content:', error);
throw error;
}
}
async function RunProcessWithImage(userPrompt, filePath) {
try {
const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
const prompt = userPrompt;
const imageParts = [
fileToGenerativePart(filePath, "image/png"),
];
const result = await model.generateContent([prompt, ...imageParts]);
const response = await result.response;
const text = await response.text();
return text;
} catch (error) {
console.error('Error generating content:', error);
throw error;
}
}
async function downloadFile(url, outputPath) {
const response = await axios({
url,
method: 'GET',
responseType: 'stream'
});
const writer = fs.createWriteStream(outputPath);
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
}
client.once('ready', () => {
console.log('Chef is ready!');
});
client.on('messageCreate', async message => {
if (message.content.startsWith('/prompt')) {
const userPrompt = message.content.slice('/prompt'.length).trim();
if (userPrompt === "") {
return message.channel.send("Messages cannot be empty!");
}
try {
if (message.attachments.size > 0) {
message.attachments.forEach(async attachment => {
const fileUrl = attachment.url;
const filePath = path.join(__dirname, attachment.name);
try {
await downloadFile(fileUrl, filePath);
const result = await RunProcessWithImage(userPrompt, filePath);
message.channel.send(result);
fs.unlinkSync(filePath);
} catch (error) {
console.error(`Error downloading file: ${error.message}`);
message.channel.send('An error occurred while downloading the file.');
}
});
} else {
const result = await RunProcess(userPrompt);
message.channel.send(result);
}
} catch (error) {
message.channel.send('An error occurred while processing your request.');
}
}
if (message.content === "/help" || message.content === "!help" || message.content === "help") {
message.channel.send("`---- Commands ----`\n- **/prompt <your_prompt>** \n- **/help** or **!help** or just **help**");
}
});
client.login(token);