Reddit Bot Example (NodeJS)

Here's a simple example of how we used the API to create a reddit bot in NodeJS.

View on Github

FileredditBot.js

var request = require('request');
const Snoowrap = require('snoowrap');
const Snoostorm = require('snoostorm');
 
const r = new Snoowrap({
    userAgent: 'web:5cc-boardgame-reddit-bot:v0.1 (by /u/boardgame_bot)',
    clientId: process.env.REDDIT_CLIENT,
    clientSecret: process.env.REDDIT_SECRET,
    username: process.env.REDDIT_USER,
    password: process.env.REDDIT_PASS,
});
const client = new Snoostorm(r);
const streamOpts = {
    subreddit: 'boardgames',
    results: 100
};
 
const comments = client.CommentStream(streamOpts);
 
comments.on('comment', (comment) => {
    postReply(comment);
});
 
function postReply(comment) {
    const doesMatchSyntax = comment.body.indexOf('\\\[\\\[') != -1 && comment.body.indexOf('\\\]\\\]') != -1;
    if (doesMatchSyntax) {
        var gameNameList = comment.body.match(/\\\[\\\[(?:.|)*?\\\]\\\]/g).map(e => e.replace('\\\[\\\[', '').replace('\\\]\\\]', ''));
        
        var replyText = "";
 
        for (var i = 0; i < gameNameList.length; i++) {
            request({url: "https://www.5colorcombo.com/api/search?exact=true&name=" + encodeURIComponent(gameNameList[i]) } , function(err, res, jsonString) {
                var json = JSON.parse(jsonString);
                var game = json.games[0];
                var gameInformation = "\n\n* [" + game.name + "](" + game.image_url + ") \- [(5CC)](" + game.url + ")";
                replyText += gameInformation
            });
        }
        comment.reply(replyText.trim());
    }
}

RedditOutput

5 Color Combo Board Game Api Reddit Bot