Trigger everytime a new item post on reddit matches at least one mention  (reddit) - trigger module
One script reply has been approved by the moderators

1 points by rossmccrann 45 days ago | 29 uses

For every new post since last trigger collect the ones that match at least of the mention.

One script reply has been approved by the moderators
This version of script #115 was approved by the moderators as the one that fits the best the script ask: 'Trigger everytime a new item post on reddit matches at least one mention '. It is safe to use in your flows in production.
0 points
by rossmccrann 45 days ago
edit by rossmccrann 45 days ago
import { fetchPosts } from "https://deno.land/x/redditposts/src/mod.ts";
import * as wmill from "https://deno.land/x/[email protected]/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]);
}
edit/run in windmill raw embed