not-an-aardvark / snoowrap

A JavaScript wrapper for the reddit API
MIT License
1.01k stars 125 forks source link

Getting the username of the user an another user replied to in the Inbox #272

Open SKevo18 opened 4 years ago

SKevo18 commented 4 years ago

Hello everyone,

thanks for putting your time and effort to keep these projects alive.

I am having a trouble obtaining the username of the user I have replied to in the inbox username_mention message. I have a bot that watches the InboxStream for mentions. Whenever a bot gets mentioned, it does stuff.

Let's say we have user-A and user-B. The bot's name is bot. user-A mentioned bot in reply to user-B. bot got an alert in its inbox that it was mentioned. However, how can bot get the username of user-B? I haven't found anything in the Inbox's json reply, so I guess that it's not possible due to this limitation, however I might've missed something.

See https://github.com/MayorMonty/Snoostorm/issues/41 for reference.

Thank you.

Venefilyn commented 4 years ago

Hi,

There should be an author property for each PM that you can access

SKevo18 commented 4 years ago

Thanks for reply, but author.name represents the author of the reply (user-A), not the user I'm replying to (user-B). I do not mean PMs, but username_mention type of message (when you send a comment mentioning the bot).

Venefilyn commented 4 years ago

Ah gotcha, there is a comment.parent_id which points to the parent. You can use that to create a new Comment instance

SKevo18 commented 4 years ago

Thank you, however I am using this code:

const Snoowrap = require("snoowrap");
const Snoostorm = require("snoostorm");
const { InboxStream } = require("snoostorm");

const r = new Snoowrap({
  userAgent: "a-bot",
  clientId: process.env.CLIENT_ID,
  clientSecret: process.env.CLIENT_SECRET,
  username: process.env.USERNAME,
  password: process.env.PASSWORD
});

const inbox = new InboxStream(r);
inbox.on("item", async item => {
  console.log("Scraped: " + r.getComment(item.parent_id).author.name)
});

The line before the last one is important. I tried to log the author.name, however this returns just the following:

image

Here's an example of one Comment (item) from the inbox json (there are more, every returns just empty object):

Comment {
  first_message: null,
  first_message_name: null,
  subreddit: Subreddit { display_name: 'some-subreddit' },
  likes: true,
  replies: Listing [],
  id: '1234abc',
  subject: 'kde ma spomenuli',
  associated_awarding_id: null,
  score: 1,
  author: RedditUser { name: 'user-A' },
  num_comments: 5,
  parent_id: 't3_abc1234',    <----- It's prefixed with t3, however if I try to remove the t3_ part, it still returns just an empty promise.
  subreddit_name_prefixed: 'r/some-subreddit',
  new: false,
  type: 'username_mention',
  body: 'u/bot',
  link_title: 'ah',
  dest: 'bot',
  was_comment: true,
  body_html: '<!-- SC_OFF --><div class="md"><p><a ' +
    'href="/u/bot">u/bot</a></p>\n' +
    '</div><!-- SC_ON -->',
  name: 't1_1234abc',
  created: 1598042633,
  created_utc: 1598013833,
  context: '/r/some-subreddit/comments/abc1234/ah/1234abc/?context=3',
  distinguished: null
}

(the usernames & other private info is censored/replaced by placeholders).

Logging just console.log("Scraped: " + r.getComment(inbox.parent_id)) logs empty object Object.

Venefilyn commented 3 years ago

Sorry for late response. Did you solve the issue?

Otherwise, this seems to be a Promise issue, it didn't fetch the content. You can do something like

console.log("Scraped: " + (await r.getComment(item.parent_id)).author.name)