Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const util = require('util');
- const res = require("res");
- const rawjs = require("raw.js");
- const reddit = new rawjs("raw.js example script");
- const http = require("http");
- var snoowrap = require('snoowrap');
- var r = new snoowrap({
- userAgent: 'script',
- clientId: 'dfnM6gmHmbBGrQ',
- clientSecret: 'kkhUDLI4S7ztJPwUsuF0LWFrpzE',
- username: 'xxx',
- password: 'xxx'
- });
- //HTTP SERVER
- http.createServer(function(request, response) {
- console.log("I got kicked");
- response.writeHeader(200, {
- "Content-Type": "text/plain"
- });
- response.end();
- }).listen(8080);
- console.log("Server Running on 8080");
- //OAUTH2 Setup
- reddit.setupOAuth2("xxx", "xxx");
- //global variables and objects
- var LastPostRun = {
- name: ''
- };
- function criticalError(message) {
- console.log(message);
- return process.exit();
- };
- //GET LATEST POSTS
- function newPosts(sub) {
- reddit.new({
- r: sub,
- limit: 1
- }, function(err, res) {
- //error handling
- if (err || res.children == null || res.children.length == 0) return CriticalError('Unable to scan for posts in /r/' + sub + ' . Error: ' + err);
- //set the latest post's unique name to global variable
- LastPostRun.name = res.children[0].data.name;
- //checking if a new post has been made
- if (res.children[0].data.name != LastPostRun.name) {
- console.log('new post has been made');
- grabSubmissionCount(res.children[0].data.author, sub);
- }
- });
- }
- ///COUNT NUMBER OF SUBMISSIONS
- function grabSubmissionCount(author, sub) {
- reddit.userLinks({
- user: author,
- limit: 100
- }, function(err, res) {
- if (err || res.children == null || res.children.length == 0) return CriticalError('Cound not retrieve links: ' + err);
- var postCount;
- for (x = 0; x < res.children.length; x++) {
- if (res.children[x].data.subreddit === sub) {
- postCount++;
- }
- }
- addFlair(author, sub, postCount);
- });
- }
- //add flair -- snoowrap
- function addFlair(user, sub, postCount) {
- if (postCount >= 3 && postCount < 25) {
- r.getSubreddit(sub).setMultipleUserFlairs([{
- name: user,
- text: 'Novice',
- cssClass: 'novice'
- },
- ]);
- console.log('added');
- LastPostRun.name = res.children[0].data.name;
- } else {
- console.log('no flair assigned');
- }
- }
- //REDDIT AUTHORIZATION
- reddit.auth({
- "username": "xxx",
- "password": "xxx"
- }, function(err, response) {
- if (err) return criticalError('Could not authneticate. Error message: ' + err);
- setInterval(function() {
- newPosts('BannedFromCoC');
- }, 5000);
- });
Add Comment
Please, Sign In to add comment