Add message counting and date-bounded scrapes

This commit is contained in:
Fisto
2026-09-16 12:06:34 +02:00
parent f14f067413
commit fae754fba0
21 changed files with 410 additions and 22 deletions
+27
View File
@@ -54,6 +54,33 @@ export function slopbotStatus(client: Client | null): SlopbotStatus {
};
}
/** The REST surface the search count needs. discord.js `client.rest` satisfies it. */
export interface SlopbotRest {
get(path: string, options?: { query?: URLSearchParams }): Promise<unknown>;
}
/**
* Discord search `total_results` for a channel or range. One request.
* Null when the search fails (rate limit, permissions, or no index).
*/
export async function countMessages(
rest: SlopbotRest,
input: { channelId: string; guildId: string; afterId?: string | null; beforeId?: string | null }
): Promise<number | null> {
const query = new URLSearchParams({ channel_id: input.channelId, limit: "1" });
if (input.afterId) query.set("min_id", input.afterId);
if (input.beforeId) query.set("max_id", input.beforeId);
try {
const data = (await rest.get(`/guilds/${input.guildId}/messages/search`, { query })) as {
total_results?: number;
};
return typeof data?.total_results === "number" ? data.total_results : null;
} catch {
return null;
}
}
/** The ScrapeBackend fetcher over a live client. */
export function slopbotFetcher(client: Client): SlopbotFetcher {
return {