Last active
July 27, 2020 15:53
-
-
Save Birdie0/5830535877a94ab772efeb897e58e0e8 to your computer and use it in GitHub Desktop.
Reddit.rss to Discord filter code + parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ========== | |
// =SETTINGS= | |
// ========== | |
// set to false if you want to filter text, link or posts without preview | |
const textPosts: boolean = true | |
const linkPosts: boolean = true | |
const noPicPosts: boolean = true | |
// DO NOT EDIT CODE BELOW UNLESS SAID OTHERWISE!!! | |
// Trigger Data | |
// !!! Feed.newFeedItem === Trigger | |
const entryTitle: string = Trigger.EntryTitle | |
const entryUrl: string = Trigger.EntryUrl | |
const entryAuthor: string = Trigger.EntryAuthor | |
const entryContent: string = Trigger.EntryContent | |
const entryImageUrl: string = Trigger.EntryImageUrl | |
const entryPublished: string = Trigger.EntryPublished | |
const feedTitle: string = Trigger.FeedTitle | |
const feedUrl: string = Trigger.FeedUrl | |
const currentUserTime: moment.Moment = Meta.currentUserTime | |
const triggerTime: moment.Moment = Meta.triggerTime | |
let body: { | |
username?: string, | |
avatar_url?: string, | |
content?: string, | |
embeds?: { | |
color?: string | number, | |
author?: { name: string, url?: string, icon_url?: string }, | |
title?: string, | |
url?: string, | |
description?: string, | |
fields?: { name: string, value: string, inline?: boolean }[], | |
image?: { url: string }, | |
thumbnail?: { url: string }, | |
footer?: { text: string, icon_url?: string }, | |
timestamp?: string | moment.Moment | |
}[], | |
tts?: boolean, | |
allowed_mentions?: { | |
parse?: ("everyone" | "roles" | "users")[], | |
roles?: string[], | |
users?: string[] | |
} | |
} = {} | |
if (entryContent.indexOf("<table>") === 0) { // link post | |
if (!linkPosts) { MakerWebhooks.makeWebRequest.skip("skipped link post!") } | |
const data: RegExpMatchArray = entryContent.match(/^<table> <tr><td> <a href="(\S+)"> <img src="(\S+)" alt="(.+)" title="(.+)" \/> <\/a> <\/td><td>   submitted by   <a href="(\S+)"> \/u\/(\S+) <\/a>(?:   to   <a href="(\S+)"> r\/(\S+) <\/a>)? <br\/> <span><a href="(\S+)">\[link\]<\/a><\/span>   <span><a href="(\S+)">\[comments\]<\/a><\/span> <\/td><\/tr><\/table>$/) | |
if (!data) { MakerWebhooks.makeWebRequest.skip("error") } | |
const feedUrl: string = data[1] | |
const thumbnailUrl: string = data[2] | |
const thumbnailAlt: string = data[3] | |
const thumbnailTitle: string = data[4] // same as data[3] | |
const authorUrl: string = data[5] | |
const authorName: string = data[6] | |
const subredditUrl: string = data[7] || `https://www.reddit.com/r/${data[10].match(/reddit\.com\/r\/(.+?)\//i)[1]}` | |
const subredditName: string = data[8] || data[10].match(/reddit\.com\/r\/(.+?)\//i)[1] | |
const postUrl: string = data[9] | |
const commentsUrl: string = data[10] // same as data[1] | |
body = { // EDITABLE-BEGIN!!! | |
embeds: [{ | |
author: { | |
name: authorName.slice(0, 256), | |
url: authorUrl | |
}, | |
title: entryTitle.slice(0, 256), | |
url: entryUrl, | |
image: { | |
url: postUrl | |
}, | |
footer: { | |
text: `r/${subredditName}`, | |
icon_url: "https://www.redditstatic.com/desktop2x/img/favicon/favicon-96x96.png" | |
}, | |
color: 0xFF4500 | |
}] | |
} // EDITABLE-END!!! | |
} else if (entryContent.indexOf("<!-- SC_OFF -->") === 0) { // text post | |
if (!textPosts) { MakerWebhooks.makeWebRequest.skip("skipped text post!") } | |
const data: RegExpMatchArray = entryContent.match(/^<!-- SC_OFF --><div class="md">(.+) <\/div><!-- SC_ON -->   submitted by   <a href="(\S+)"> \/u\/(\S+) <\/a>(?:   to   <a href="(\S+)"> r\/(\S+) <\/a>)? <br\/> <span><a href="(\S+)">\[link\]<\/a><\/span>   <span><a href="(\S+)">\[comments\]<\/a><\/span>$/) | |
if (!data) { MakerWebhooks.makeWebRequest.skip("error") } | |
const postContent: string = data[1].replace(/<\/?strong>/g, "**") | |
.replace(/<\/?em>/g, "*") | |
.replace(/<\/?del>/g, "~~") | |
.replace(/<\/?sup>/g, "__") | |
.replace(/<a href="(\S+?)">(.+?)<\/a>/g, "[$2]($1)") | |
.replace(/<\/?blockquote>/g, "```\n") | |
.replace(/<\/?code>/g, "`") | |
.replace(/<li>/g, "• ") | |
.replace(/<\/p>/g, "\n") | |
.replace(/<.+?>/g, "") | |
const authorUrl: string = data[2] | |
const authorName: string = data[3] | |
const subredditUrl: string = data[4] || `https://www.reddit.com/r/${data[7].match(/reddit\.com\/r\/(.+?)\//i)[1]}` | |
const subredditName: string = data[5] || data[7].match(/reddit\.com\/r\/(.+?)\//)[1] | |
const commentsUrl: string = data[6] | |
const commentsUrl2: string = data[7] | |
body = { // EDITABLE-BEGIN!!! | |
embeds: [{ | |
author: { | |
name: authorName.slice(0, 256), | |
url: authorUrl | |
}, | |
title: entryTitle.slice(0, 256), | |
url: entryUrl, | |
description: postContent.slice(0, 2048), | |
footer: { | |
text: `r/${subredditName}`, | |
icon_url: "https://www.redditstatic.com/desktop2x/img/favicon/favicon-96x96.png" | |
}, | |
color: 0xFF4500 | |
}] | |
} // EDITABLE-END!!! | |
} else if (entryContent.indexOf("  submitted by  ") === 0) { // no preview post | |
if (!noPicPosts) { MakerWebhooks.makeWebRequest.skip("skipped no pic post!") } | |
const data: RegExpMatchArray = entryContent.match(/^  submitted by   <a href="(\S+)"> \/u\/(\S+) <\/a>(?:   to   <a href="(\S+)"> r\/(\S+) <\/a>)? <br\/> <span><a href="(\S+)">\[link\]<\/a><\/span>   <span><a href="(\S+)">\[comments\]<\/a><\/span>$/) | |
if (!data) { MakerWebhooks.makeWebRequest.skip("error") } | |
const authorUrl: string = data[1] | |
const authorName: string = data[2] | |
const subredditUrl: string = data[3] || `https://www.reddit.com/r/${data[6].match(/reddit\.com\/r\/(.+?)\//i)[1]}` | |
const subredditName: string = data[4] || data[6].match(/reddit\.com\/r\/(.+?)\//i)[1] | |
const postUrl: string = data[5] | |
const commentsUrl: string = data[6] | |
body = { // EDITABLE-BEGIN!!! | |
embeds: [{ | |
author: { | |
name: authorName.slice(0, 256), | |
url: authorUrl | |
}, | |
title: entryTitle.slice(0, 256), | |
url: entryUrl, | |
description: `[Link](${postUrl})`, | |
footer: { | |
text: `r/${subredditName}`, | |
icon_url: "https://www.redditstatic.com/desktop2x/img/favicon/favicon-96x96.png" | |
}, | |
color: 0xFF4500 | |
}] | |
} // EDITABLE-END!!! | |
} | |
MakerWebhooks.makeWebRequest.setBody(JSON.stringify(body)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment