Search code examples
javascriptnode.jsbotsreddit

Reddit API (Node Js) : How to retrieve a parent comment in reddit using snoowrap and snoostorm?


So I am creating a reddit bot. The scenario is that A posts a comment. B replies to that comment by invoking the bot. Typically snoostorm provides a comment object for B containing info about B and the original post. How do I get a comment object for A?

const Snoowrap = require('snoowrap');
const { CommentStream } = require('snoostorm');

const client = new Snoowrap({
  userAgent: 'rpffdgfh',
  clientId: 'Ddhjhfjsh',
  clientSecret: 'kRHXydsgjgkjkjsjkgl',
  username: 'botname',
  password: 'botpass'
});

const canSummon = (msg) => {
  return msg && msg.toLowerCase().includes('u/botname');
};

const comments = new CommentStream(client, {
  subreddit: 'testingground4bots',
  limit: 10,
  pollTime: 10000
});

//info about original comment (in this case B)
comments.on('item', (item) => {
  if (!canSummon(item.body)) return;
  console.log(item);
}); 

I've already read the docs of snoowrap. I cant find one for snoostorm. In short there is a lack of documentation or guides for creating complex reddit bots using javascript/node.js, while there are many readily available for python.


Solution

  • The Comment object has a property parent_id. You have to fetch the parent Comment to get the object.

    comments.on('item', (item) => {
        if (!canSummon(item.body)) return;
        console.log(item);
        client.getComment(item.parent_id).fetch().then(parentComment => {
            console.log(parentComment.body);
        });
    });
    

    Snoostorm is just a wrapper for Snoowrap.