Skip to content

Commit

Permalink
Quotes API
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleylamont committed Oct 16, 2024
1 parent 2f7ee0c commit d99deee
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { attachNocoDBWebhookListener } from "./nocodb-integration";
import { startServerIcon } from "./server-icon";
import registerCommands from "./command-registry";
import addStageOne from "./secret";
import { attachQuotesServer } from "./quotes-server";

const PORT = 8080;
globalThis.appMaintainers = [];
Expand All @@ -32,6 +33,7 @@ async function main(): Promise<void> {
// Initialise the express app and attach the door server
const expressApp = express();
await attachDoorServer(expressApp);
await attachQuotesServer(expressApp);
await attachNocoDBWebhookListener(expressApp);

// Start the server icon and add reaction events
Expand Down
105 changes: 105 additions & 0 deletions src/quotes-server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import type { Express } from "express";
import { Collection, Message } from "discord.js";
import { Quote } from "./commands/quote";

const quotes: Collection<string, Quote> = new Collection();

export function convertMessageToQuote(
message: Message<true>,
): Quote | undefined {
// Check if the message is an embed
if (message.embeds.length === 1) {
const embed = message.embeds[0];
let author: Quote["quotee"] = embed.title ?? undefined;
if (author == "Anonymous Quote") {
author = undefined;
}
author = author?.replace("Quote of ", "");
return {
message: embed.description ?? "",
quotee: author,
timestamp: new Date(embed.timestamp || message.createdTimestamp),
};
} else {
const messageContent = message.content;
if (!messageContent.startsWith("> ")) {
return undefined;
}
const quoteBody = messageContent;
if (message.mentions.members.size > 0) {
return {
message: quoteBody,
quotee: message.mentions.members
.map((member) => member.displayName)
.join(", "),
timestamp: message.createdAt,
};
} else {
// Guess the quotee
const quotee = messageContent
.split("\n")
.find((line) => !line.startsWith("> "));
if (quotee === undefined) {
return undefined;
}
return {
message: quoteBody,
quotee,
timestamp: message.createdAt,
};
}
}
}

export async function refreshQuotes() {
const quotesChannel = await globalThis.cssaGuild.channels.fetch(
"1169166790094491678",
);
if (quotesChannel === null) {
throw new Error("Quotes channel is null");
} else if (!quotesChannel.isTextBased()) {
throw new Error("Quotes channel is not text based");
}
// Start fetching recent top-level messages
let hitCachedMessage = false;
let messages = await quotesChannel.messages.fetch({ limit: 100 });
while (!hitCachedMessage && messages.size > 0) {
for (const message of messages.values()) {
const quote = convertMessageToQuote(message);
if (quote !== undefined) {
if (quotes.has(message.id)) {
hitCachedMessage = true;
}
quotes.set(message.id, quote);
}
}
if (!hitCachedMessage) {
const lastMessage = messages.last();
if (lastMessage === undefined) {
break;
}
// eslint-disable-next-line no-await-in-loop
messages = await quotesChannel.messages.fetch({
limit: 100,
before: lastMessage.id,
});
}
}
return;
}

export async function attachQuotesServer(app: Express) {
await refreshQuotes();
app.get("/quotes", (_, response) => {
refreshQuotes().then(() => {
response.set("Content-Type", "application/json");
response.send(
JSON.stringify({
quotes: [...quotes.values()].sort(
(a, b) => b.timestamp.getTime() - a.timestamp.getTime(),
),
}),
);
});
});
}

0 comments on commit d99deee

Please sign in to comment.