Skip to content

Commit

Permalink
Where did he go?!
Browse files Browse the repository at this point in the history
  • Loading branch information
Erisfiregamer1 committed Feb 4, 2024
1 parent 0cdf031 commit 2fb1203
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 43 deletions.
2 changes: 2 additions & 0 deletions bots/gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ export async function send(

let useImageModel = false;

console.log(useImageModel)

// Check if any object has the specified property set to the target value
for (let i = 0; i < messages.length; i++) {
if (messages[i].role === "image") {
Expand Down
87 changes: 44 additions & 43 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import "./slashcode.ts";

import client from "./client.ts";

import { ChannelType } from "npm:discord.js";
import { ChannelType, Message } from "npm:discord.js";

const db = await Deno.openKv("./db.sqlite");

Expand Down Expand Up @@ -84,6 +84,47 @@ New database example:
}
*/

const getImagesFromMessage = async (message: Message<boolean>) => {
const images: string[] = [];

// Process attachments
message.attachments.forEach((image) => {
images.push(image.url);
});

// Process URLs in message content
const regx = message.content.match(/\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»]))/ig);

if (regx) {
// Use Promise.all to wait for all asynchronous operations to complete
const resultArray = await Promise.all(regx.map(async (link) => {
const aeiou = await fetch(link);
const isImage = aeiou.headers.get('Content-Type')?.startsWith("image/");
if (isImage) {
console.log(link);
message.content.replace(link, "");
return link;
}
return null;
}));

const filteredImages: string[] = []

resultArray.forEach((link) => {
if (link !== null) filteredImages.push(link)
});

images.push(...filteredImages);
}

// Process stickers
message.stickers.forEach((sticker) => {
images.push(sticker.url);
});

return images;
};

client.on("messageCreate", async (message) => {
if (message.author.bot || JSON.stringify(message.flags) === "4096") return; // The "4096" flag is the @silent flag on discord.
if (
Expand Down Expand Up @@ -374,27 +415,7 @@ client.on("messageCreate", async (message) => {
return;
}
} else if (llm === "gpt4_v") {
const images: string[] = [];

message.attachments.forEach((image) => {
images.push(image.url);
});

const regx = message.content.match(/(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])/)

regx?.forEach(async (link) => {
const aeiou = await fetch(link)
const isImage = aeiou.headers.get('Content-Type')?.startsWith("image/")
console.log(isImage)
if (isImage) {
message.content.replace(link, "")
images.push(link)
}
})

message.stickers.forEach((image) => {
images.push(image.url);
});
const images: string[] = await getImagesFromMessage(message)

if (!gpt4.isEnabled) {
msg.edit(
Expand Down Expand Up @@ -456,27 +477,7 @@ client.on("messageCreate", async (message) => {
return;
}
} else if (llm === "gemini") {
const images: string[] = [];

message.attachments.forEach((image) => {
images.push(image.url);
});

const regx = message.content.match(/(http|ftp|https):\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])/)

regx?.forEach(async (link) => {
const aeiou = await fetch(link)
const isImage = aeiou.headers.get('Content-Type')?.startsWith("image/")
console.log(isImage)
if (isImage) {
message.content.replace(link, "")
images.push(link)
}
})

message.stickers.forEach((image) => {
images.push(image.url);
});
const images: string[] = await getImagesFromMessage(message)

if (!gemini.isEnabled) {
msg.edit(
Expand Down

0 comments on commit 2fb1203

Please sign in to comment.