Add message counting and date-bounded scrapes
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -13,7 +13,15 @@ export type SlopbotClient = Client;
|
||||
|
||||
export { loadSlopbotConfig, type SlopbotConfig };
|
||||
export { mapSlopbotMessage, SlopbotBackend, type SlopbotFetcher } from "./backend.js";
|
||||
export { slopbotFetcher, slopbotStatus, startSlopbot, stopSlopbot, type SlopbotStatus } from "./client.js";
|
||||
export {
|
||||
countMessages,
|
||||
slopbotFetcher,
|
||||
slopbotStatus,
|
||||
startSlopbot,
|
||||
stopSlopbot,
|
||||
type SlopbotRest,
|
||||
type SlopbotStatus
|
||||
} from "./client.js";
|
||||
|
||||
/** View Channels + Read Message History. The bot reads only; it never sends. */
|
||||
const PERMISSIONS = String((1 << 10) | (1 << 16));
|
||||
|
||||
Reference in New Issue
Block a user