import { fetchPosts } from "https://deno.land/x/redditposts/src/mod.ts";
import * as wmill from "https://deno.land/x/windmill@v1.29.0/mod.ts";
export async function main(
amountOfPosts: string,
subreddit: string,
mentions: string[],
) {
let lastState = await wmill.getInternalState();
const posts = await fetchPosts(`${subreddit}`, {
amount: `${amountOfPosts}`,
category: "top",
});
const items = [];
for (let item in posts) {
if (
mentions.find((mention) => posts[item]["title"]?.includes(mention)) ||
mentions.find((mention) => posts[item]["selftext"]?.includes(mention))
) {
items.push(posts[item]["title"]);
}
}
await wmill.setInternalState(items);
let currentState = await wmill.getInternalState();
let missingVals = currentState.filter(item => lastState.indexOf(item) < 0);
const subset = missingVals.every(val => lastState.includes(val));
if (lastState && !arrayEquals(currentState, lastState) && !subset) {
return { items: items };
}
return "No new mentions on reddit.";
}
function arrayEquals(a, b) {
return Array.isArray(a) &&
Array.isArray(b) &&
a.length === b.length &&
a.every((val, index) => val === b[index]);
}
Submitted by rossmccrann 856 days ago