Up-to-date Node.js module for Discourse API?

(Alkaline Thunder) #1

Hey there. I’m developing the discourse integration bot for Discord. It works perfectly as a Discord bot an a Discourse SSO client - so users can link their forum account to a Discord server.

I need to use the Discourse API to query information about each linked user periodically so I can keep things synchronized. I thought maybe the discourse-sdk NPM package would help me out here but when it goes to fetch user info, it returns a TypeError: cannot read property 'id' of undefined when it gets an API response.

For reference, the code that I’m using to test it is as follows:

module.exports.name = "profile";
module.exports.desc = "View the profile of the specified username. If none is specified, your own profile will be shown.";
module.exports.category = "Users";
module.exports.usage = "[username]";

module.exports.run = function(discord, discourse, message, caller, args)
    const Discord = require('discord.js');
    const Discourse = require('discourse-sdk');
    const fs = require('fs');

    let id = caller.id;

    let userFile = "users.json";

    let userFileExists = fs.existsSync(userFile);

    let users = {};

        users = JSON.parse(fs.readFileSync(userFile));

    let isCallerKnown = users.hasOwnProperty(id);
    let isCallerLoggedIn = false;

        isCallerLoggedIn = users[id].sso_done;

    let username = args.username;

    if(username === false)
            username = users[id].discourse_username;

    discourse.getUser(username, (error, body, httpCode) => {

So, it’s a Discord bot command that takes a username, if none is specified, it uses the caller’s own username if it’s known. Then it queries the Discourse API for the user info.

I investigated, and it throws the TypeError when it tries to check if the user ID is defined in the API’s response - a.k.a, checking if the API sent a user back. I used the debugger to see what URL it was requesting user info from and it was correct. Popped it in my browser and I got JSON - but it wasn’t in the format that the Node module is expecting.

In fact, the JSON I got back shows me EVERYTHING to do with users - badges, badge types, etc., and it has a list of ALL users on the forum as a map of user IDs to user objects.

This seems to suggest that the module I’m using is outdated and thus broken. So, are there any other Node modules out there that are actually up to date that wrap the Discourse API? Or, better yet, is there a better approach to getting a single user’s information either by user ID or username?