Skip to content

Commit

Permalink
Merge branch 'scrub-4-(not-a-joke-what-am-i-doing-help-me)' of https:…
Browse files Browse the repository at this point in the history
…//github.com/theyande/scradd into scrub-4-(not-a-joke-what-am-i-doing-help-me)
  • Loading branch information
TheYande committed Aug 24, 2024
2 parents 2d04814 + c970812 commit 1a3a573
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
18 changes: 15 additions & 3 deletions modules/ai/ai-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export class AIChat {
this.maxMessages = maxMessages;
}

async send(message: string | any[], role: string = "user", type: "text" | "image" | "complex" = "text"): Promise<string> {
async send(
message: string | any[],
role: string = "user",
type: "text" | "image" | "complex" = "text",
): Promise<string> {
this.inform(message, role, type);

const response = await fetch(this.apiUrl, {
Expand All @@ -38,14 +42,22 @@ export class AIChat {
return reply;
}

inform(content: string | any[], role: string = "system", type: "text" | "image" | "complex" = "text"): void {
inform(
content: string | any[],
role: string = "system",
type: "text" | "image" | "complex" = "text",
): void {
if (this.history.length >= this.maxMessages) {
this.history.shift(); // Remove the oldest message
}
this.history.push({ role, content, type });
}

sticky(content: string | any[], role: string = "system", type: "text" | "image" | "complex" = "text"): void {
sticky(
content: string | any[],
role: string = "system",
type: "text" | "image" | "complex" = "text",
): void {
this.stickyMessages.push({ role, content, type });
}

Expand Down
44 changes: 32 additions & 12 deletions modules/ai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ only alert when its it obvious when a rule is broken. if not do not alert.

const memory = new Database<{ content: string }>("aimem");
await memory.init();
defineEvent("messageCreate", async (m) => {
defineEvent("messageCreate", async (m) => {
if (m.author.bot) return;
if (
!(
Expand All @@ -213,16 +213,36 @@ defineEvent("messageCreate", async (m) => {
}, 4000);
const reference = m.reference ? await m.fetchReference() : null;
try {
let response = m.attachments.filter((attachment) => attachment.contentType?.match(/^image\/(bmp|jpeg|png|bpm|webp)$/i)).map(() => "").length ?
await ai.send(
[
{ type: 'text', text: `${m.reference ? `\n(replying to ${reference?.author.displayName} : ${reference?.author.id}\n${reference?.content})\n` : ""}${m.author.displayName} : ${m.author.id} : ${m.channel.isDMBased() ? `${m.author.displayName}'s DMs` : m.channel.name}\n${m.content}`},
...([...m.attachments.filter((attachment) => attachment.contentType?.match(/^image\/(bmp|jpeg|png|bpm|webp)$/i)).map(v => v.url)].map((i) => ({type: "image_url", image_url: { url: i }})))
], "user", "complex"
) :
await ai.send(
`${m.reference ? `\n(replying to ${reference?.author.displayName} : ${reference?.author.id}\n${reference?.content})\n` : ""}${m.author.displayName} : ${m.author.id} : ${m.channel.isDMBased() ? `${m.author.displayName}'s DMs` : m.channel.name}\n${m.content}`,
)
let response =
(
m.attachments
.filter((attachment) =>
attachment.contentType?.match(/^image\/(bmp|jpeg|png|bpm|webp)$/i),
)
.map(() => "").length
) ?
await ai.send(
[
{
type: "text",
text: `${m.reference ? `\n(replying to ${reference?.author.displayName} : ${reference?.author.id}\n${reference?.content})\n` : ""}${m.author.displayName} : ${m.author.id} : ${m.channel.isDMBased() ? `${m.author.displayName}'s DMs` : m.channel.name}\n${m.content}`,
},
...[
...m.attachments
.filter((attachment) =>
attachment.contentType?.match(
/^image\/(bmp|jpeg|png|bpm|webp)$/i,
),
)
.map((v) => v.url),
].map((i) => ({ type: "image_url", image_url: { url: i } })),
],
"user",
"complex",
)
: await ai.send(
`${m.reference ? `\n(replying to ${reference?.author.displayName} : ${reference?.author.id}\n${reference?.content})\n` : ""}${m.author.displayName} : ${m.author.id} : ${m.channel.isDMBased() ? `${m.author.displayName}'s DMs` : m.channel.name}\n${m.content}`,
);
//[...m.attachments.filter((attachment) => attachment.contentType?.match(/^image\/(bmp|jpeg|png|bpm|webp)$/i)).map(v => v.url)]

do {
Expand Down Expand Up @@ -324,7 +344,7 @@ async function executeCommands(
(await gracefulFetch(
`https://discord.com/api/v9/gifs/search?q=${encodeURIComponent(command.option)}&media_format=gif&provider=tenor&locale=en-US`,
)) ?? [];
await m.reply(gifs.slice(0, 10).at(Math.round(Math.random() * 10))?.src ?? "")
await m.reply(gifs.slice(0, 10).at(Math.round(Math.random() * 10))?.src ?? "");
}
break;
default:
Expand Down

0 comments on commit 1a3a573

Please sign in to comment.