diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..051d09d --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +eval "$(lorri direnv)" diff --git a/main.go b/main.go index e9a594b..9584470 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "log" "net/http" + "strings" "time" "github.com/turnage/graw/reddit" @@ -20,8 +21,21 @@ var ( webhookFile = flag.String("webhook-file", "./var/webhook.txt", "where the Discord webhook file is located") subreddit = flag.String("subreddit", "tulpas", "the subreddit to monitor") pokeFreq = flag.Duration("poke-frequency", 5*time.Minute, "how often the bot should poke the feed") + allowNSFW = flag.Bool("allow-nsfw", false, "proxy NSFW posts?") ) +func clampLen(data string) string { + if len(data) < 1800 { + return data + } + + var sb strings.Builder + sb.WriteString(data[0:1800]) + sb.WriteString("\n\n[Post truncated for length]") + + return sb.String() +} + func main() { flag.Parse() @@ -53,13 +67,18 @@ func main() { log.Printf("listening for new posts on /r/%s", *subreddit) for post := range stream { - log.Printf("got new post: by /u/%s: %q %s", post.Author, post.URL, post.Title) + log.Printf("got new post: by /u/%s: %q %s, NSFW: %v", post.Author, post.URL, post.Title, post.NSFW) + + if post.NSFW && !*allowNSFW { + continue + } + wh := Webhook{ Embeds: []Embed{ { Title: post.Title, URL: post.URL, - Description: post.SelfText, + Description: clampLen(post.SelfText), Footer: EmbedFooter{ Text: "by /u/" + post.Author, }, diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..51f655c --- /dev/null +++ b/shell.nix @@ -0,0 +1,5 @@ +{ pkgs ? import { } }: + +pkgs.mkShell { + buildInputs = with pkgs; [ go gopls goimports bashInteractive ]; +}