Edits history of script submission #115 for ' Trigger everytime a new item post on reddit matches at least one mention (reddit)'

  • deno
    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]);
    }

    Submitted by rossmccrann 1388 days ago