Skip to content

Instantly share code, notes, and snippets.

@turt2live
Created September 28, 2017 04:45
Show Gist options
  • Save turt2live/f3de58d79aff02da0b2291031c87a6ec to your computer and use it in GitHub Desktop.
Save turt2live/f3de58d79aff02da0b2291031c87a6ec to your computer and use it in GitHub Desktop.
var jsyaml = require("js-yaml");
var request = require("request");
var fs = require("fs");
// Usage:
// node adminme.js <room id> <user id> <power level>
// ex: node adminme.js '!curbf:matrix.org' '@travis:t2l.io' 100
var registration = jsyaml.safeLoad(fs.readFileSync("discord-registration.yaml", "utf8"));
var config = jsyaml.safeLoad(fs.readFileSync("config.yaml", "utf8"));
var roomId = process.argv[2];
var userId = process.argv[3];
var powerLevel = process.argv[4] || 0;
console.log("Setting " + userId + " to PL" + powerLevel + " in room " + roomId);
if (!roomId || !userId) {
console.error("Usage: node adminme.js <roomid> <userid> <powerlevel>");
return;
}
var accessToken = registration.as_token;
var homeserverUrl = config.bridge.homeserverUrl;
if (homeserverUrl.endsWith("/")) homeserverUrl = homeserverUrl.substring(0, homeserverUrl.length - 1);
var opts = {
uri: homeserverUrl + "/_matrix/client/r0/rooms/" + roomId + "/state/m.room.power_levels",
qs: {access_token: accessToken}
};
request(opts, function(err, response, body) {
if (err) {
console.error(err);
return;
}
console.log(body);
var eventBody = JSON.parse(body);
if (!eventBody.users) eventBody.users = {};
eventBody.users[userId] = powerLevel;
opts = {
method: 'PUT',
uri: homeserverUrl + "/_matrix/client/r0/rooms/" + roomId + "/state/m.room.power_levels",
qs: {access_token: accessToken},
json: eventBody
};
request(opts, function(err, response, body) {
if (err) {
console.error(err);
return;
}
console.log(body);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment