Skip to content

Instantly share code, notes, and snippets.

@crookedneighbor
Last active September 26, 2016 16:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crookedneighbor/5c8e1f8de9f6d370899b88c593d93751 to your computer and use it in GitHub Desktop.
Save crookedneighbor/5c8e1f8de9f6d370899b88c593d93751 to your computer and use it in GitHub Desktop.
Populate Guilds
'use strict'
var uuid = require('uuid').v4
var Habitica = require('habitica')
var ENDPOINT = 'http://localhost:3000' // whatever your endpoint url is
var GUILD_ID = 'your-public-guild-id'
var NUMBER_OF_MEMBERS = 100
var count = 0
function makeNewUser () {
var api = new Habitica({
endpoint: ENDPOINT
})
var name = uuid()
var email = name + '@example.com'
var password = 'password'
return api.register(name, email, password).then(function () {
return api
})
}
function joinGuild (api) {
return api.post('/groups/' + GUILD_ID + '/join').then(function (res) {
var guildName = res.data.name
console.log(++count + ' user(s) have joined ' + guildName)
})
}
function logError (err) {
if (err.message) {
console.error(err.message)
} else {
console.error(err)
}
}
var promise = Promise.resolve()
for (var i = 0; i < NUMBER_OF_MEMBERS; i++) {
promise = promise.then(makeNewUser).then(joinGuild)
}
promise.catch(logError)
{
"name": "guilds_script",
"version": "1.0.0",
"description": "",
"main": "guilds.js",
"repository": {
"type": "git",
"url": "git+ssh://git@gist.github.com/5c8e1f8de9f6d370899b88c593d93751.git"
},
"keywords": [],
"author": "Blade Barringer <blade@crookedneighbor.com> (http://crookedneighbor.com/)",
"license": "MIT",
"bugs": {
"url": "https://gist.github.com/5c8e1f8de9f6d370899b88c593d93751"
},
"homepage": "https://gist.github.com/5c8e1f8de9f6d370899b88c593d93751",
"dependencies": {
"habitica": "^4.0.0-beta.1",
"uuid": "^2.0.3"
}
}
@Ozzystrasza
Copy link

The code actually makes sense to me, incredible as it may seem (except that I don't know how promise works), but I'm having trouble running it. I copied the files to my Habitica root folder (above the habitrpg folder) and ran the npm install habitica@4.0.0-beta.1 uuid command and it worked fine, created a node_modules folder and everything. But when I do node guilds.js nothing happens, did I have to do something else? Oh and I did change the GUILD_ID value, and http://localhost:3000 is the right url.
Just to make sure I understood the code, it would still work if I changed var name = uuid() to var name = 'Test' + count right?

@crookedneighbor
Copy link
Author

I've adjusted the script a little to catch and log any errors you might be receiving. Update it with the new file and see if it tells you anything.

In theory you can change the name section to var name = 'Test' + count, but since usernames and emails need to be unique, it'll fail to register a user if you run it a second time.The uuid function assures that it will be a random string of numbers and characters, so it won't fail to register the new users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment