Skip to content

Instantly share code, notes, and snippets.

@bakkot
Created June 27, 2023 07:27
Show Gist options
  • Save bakkot/3afb87b181210ca8d9535f6ec67db3fd to your computer and use it in GitHub Desktop.
Save bakkot/3afb87b181210ca8d9535f6ec67db3fd to your computer and use it in GitHub Desktop.
import fs from 'fs';
import { inspect } from 'util';
import path from 'path';
import { fileURLToPath } from 'url';
let __dirname = path.dirname(fileURLToPath(import.meta.url));
let usersToUpgrade = ['@justingrant:matrix.org'];
let targetPowerLevel = 50;
let roomId = '!nSLHQtIRJQxUJbEuGt:matrix.org'; // editor channel; this ID can be obtained from room settings -> Advanced
/*
Obtain your access token. On the Element.io web app, this is under Settings -> Help & About (yes, really).
Put it in a file in this directory named access_token.txt.
*/
let accessToken = fs.readFileSync(path.resolve(__dirname, 'access_token.txt'), 'utf8').trim();
async function api(path) {
return (await fetch('https://matrix.org/_matrix/client/v3/' + path, {
headers: {
'Authorization': `Bearer ${accessToken}`,
},
})).json();
}
let powerLevels = await api(`rooms/${roomId}/state/m.room.power_levels`);
for (let user of usersToUpgrade) {
if ((powerLevels.users[user] ?? 0) < targetPowerLevel) {
powerLevels.users[user] = targetPowerLevel;
}
}
let response = await (await fetch(`https://matrix.org/_matrix/client/v3/rooms/${roomId}/state/m.room.power_levels`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${accessToken}`,
},
body: JSON.stringify(powerLevels),
})).json();
console.log(response);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment