|
1 | 1 | const { HarmCategory, HarmBlockThreshold } = require("@google/generative-ai");
|
2 | 2 | const { EmbedBuilder, DiscordAPIError } = require("discord.js");
|
| 3 | +const axios = require("axios"); |
3 | 4 |
|
4 | 5 | function botInGuild(interaction) {
|
5 | 6 | const botGuilds = interaction.client.guilds.cache;
|
@@ -241,7 +242,9 @@ async function fetchThreadMessages(Gemini_API_KEY, message) {
|
241 | 242 | : "user";
|
242 | 243 | let content = currentMessage.content;
|
243 | 244 | if (sender === "user") {
|
| 245 | + const attachments = await processAttachments(currentMessage); |
244 | 246 | content = content.replace(/<@\d+>\s*/, "");
|
| 247 | + content += `\n\n${attachments}`; |
245 | 248 | } else if (
|
246 | 249 | sender === "model" &&
|
247 | 250 | currentMessage.embeds.length > 0 &&
|
@@ -275,11 +278,47 @@ async function fetchThreadMessages(Gemini_API_KEY, message) {
|
275 | 278 | return { userQuestion, threadMessages, messageDeleted };
|
276 | 279 | }
|
277 | 280 |
|
| 281 | +async function processAttachments(message) { |
| 282 | + let attachmentsContent = ""; |
| 283 | + |
| 284 | + if (message.attachments.size > 0) { |
| 285 | + let hasTextPlainAttachment = false; |
| 286 | + |
| 287 | + const promises = message.attachments.map(async (attachment) => { |
| 288 | + if (attachment.contentType === "text/plain; charset=utf-8") { |
| 289 | + hasTextPlainAttachment = true; |
| 290 | + const name = attachment.name; |
| 291 | + const url = attachment.url; |
| 292 | + |
| 293 | + try { |
| 294 | + const result = await axios.get(url); |
| 295 | + const attachmentContent = result.data; |
| 296 | + |
| 297 | + attachmentsContent += `Name: ${name}\nContent: ${attachmentContent}\n\n`; |
| 298 | + } catch (error) { |
| 299 | + console.error(`Failed to retrieve content from ${url}: ${error}`); |
| 300 | + } |
| 301 | + } |
| 302 | + }); |
| 303 | + |
| 304 | + await Promise.all(promises); |
| 305 | + |
| 306 | + if (hasTextPlainAttachment) { |
| 307 | + attachmentsContent = |
| 308 | + "Below are the following files attached to my message:\n\n" + |
| 309 | + attachmentsContent; |
| 310 | + } |
| 311 | + } |
| 312 | + |
| 313 | + return attachmentsContent; |
| 314 | +} |
| 315 | + |
278 | 316 | module.exports = {
|
279 | 317 | botInGuild,
|
280 | 318 | safetySettings,
|
281 | 319 | handleGeminiError,
|
282 | 320 | handleResponse,
|
283 | 321 | checkGeminiApiKey,
|
284 | 322 | fetchThreadMessages,
|
| 323 | + processAttachments, |
285 | 324 | };
|
0 commit comments