Skip to content
Extraits de code Groupes Projets
Valider c212b1aa rédigé par Will Hunt's avatar Will Hunt
Parcourir les fichiers

Add adminme tool, for giving users powers on bridge rooms.

Tools are now written in typescript
parent 1322037b
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -9,7 +9,8 @@ ...@@ -9,7 +9,8 @@
"coverage": "istanbul --include-all-sources cover -x build/src/discordas.js _mocha -- build/test/ -R spec", "coverage": "istanbul --include-all-sources cover -x build/src/discordas.js _mocha -- build/test/ -R spec",
"build": "tsc", "build": "tsc",
"start": "npm run-script build && node ./build/src/discordas.js -p 9005 -c config.yaml", "start": "npm run-script build && node ./build/src/discordas.js -p 9005 -c config.yaml",
"getbotlink": "node ./tools/addbot.js" "getbotlink": "node ./build/tools/addbot.js",
"adminme": "node ./build/tools/adminme.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
...@@ -33,6 +34,8 @@ ...@@ -33,6 +34,8 @@
"@types/node": "^7.0.5", "@types/node": "^7.0.5",
"@types/sqlite3": "^2.2.32", "@types/sqlite3": "^2.2.32",
"bluebird": "^3.4.7", "bluebird": "^3.4.7",
"command-line-args": "^4.0.1",
"command-line-usage": "^4.0.0",
"discord.js": "^11.0.0", "discord.js": "^11.0.0",
"js-yaml": "^3.8.1", "js-yaml": "^3.8.1",
"marked": "^0.3.6", "marked": "^0.3.6",
......
const yaml = require("js-yaml"); /* tslint:disable:no-bitwise no-console no-var-requires */
const fs = require("fs"); /**
const flags = require("../node_modules/discord.js/src/util/Constants.js").PermissionFlags; * Generates a URL you can use to authorize a bot with a guild.
*/
import * as yaml from "js-yaml";
import * as fs from "fs";
const flags = require("../../node_modules/discord.js/src/util/Constants.js").PermissionFlags;
const yamlConfig = yaml.safeLoad(fs.readFileSync("config.yaml", "utf8")); const yamlConfig = yaml.safeLoad(fs.readFileSync("config.yaml", "utf8"));
if (yamlConfig === null) { if (yamlConfig === null) {
console.error("You have an error in your discord config."); console.error("You have an error in your discord config.");
} }
const client_id = yamlConfig.auth.clientID; const clientId = yamlConfig.auth.clientID;
const perms = flags.READ_MESSAGES | const perms = flags.READ_MESSAGES |
flags.SEND_MESSAGES | flags.SEND_MESSAGES |
flags.CHANGE_NICKNAME | flags.CHANGE_NICKNAME |
...@@ -15,4 +21,5 @@ const perms = flags.READ_MESSAGES | ...@@ -15,4 +21,5 @@ const perms = flags.READ_MESSAGES |
flags.ATTACH_FILES | flags.ATTACH_FILES |
flags.READ_MESSAGE_HISTORY; flags.READ_MESSAGE_HISTORY;
console.log(`Go to https://discordapp.com/api/oauth2/authorize?client_id=${client_id}&scope=bot&permissions=${perms} to invite the bot into a guild.`); const url = `https://discordapp.com/api/oauth2/authorize?client_id=${clientId}&scope=bot&permissions=${perms}`;
console.log(`Go to ${url} to invite the bot into a guild.`);
/* tslint:disable:no-console */
/**
* Allows you to become an admin for a room the bot is in control of.
*/
import { Cli, Bridge, AppServiceRegistration, ClientFactory } from "matrix-appservice-bridge";
import * as log from "npmlog";
import * as yaml from "js-yaml";
import * as fs from "fs";
import * as args from "command-line-args";
import * as usage from "command-line-usage";
import { DiscordBridgeConfig } from "../src/config";
const optionDefinitions = [
{
name: "help",
alias: "h",
type: Boolean,
description: "Display this usage guide."},
{
name: "config",
alias: "c",
type: String,
defaultValue: "config.yaml",
description: "The AS config file.",
typeLabel: "<config.yaml>" },
{
name: "roomid",
alias: "r",
type: String,
description: "The roomid to modify"},
{
name: "userid",
alias: "u",
type: String,
description: "The userid to give powers"},
{
name: "power",
alias: "p",
type: Number,
defaultValue: 100,
description: "The power to set",
typeLabel: "<0-100>" },
];
const options = args(optionDefinitions);
if (options.help) {
/* tslint:disable:no-console */
console.log(usage([
{
header: "Admin Me",
content: "A tool to give a user a power level in a bot user controlled room."},
{
header: "Options",
optionList: optionDefinitions,
},
]));
process.exit(0);
}
if (!options.roomid) {
console.error("Missing roomid parameter. Check -h");
process.exit(1);
}
if (!options.userid) {
console.error("Missing userid parameter. Check -h");
process.exit(1);
}
const yamlConfig = yaml.safeLoad(fs.readFileSync("discord-registration.yaml", "utf8"));
const registration = AppServiceRegistration.fromObject(yamlConfig);
const config: DiscordBridgeConfig = yaml.safeLoad(fs.readFileSync(options.config, "utf8")) as DiscordBridgeConfig;
if (registration === null) {
throw new Error("Failed to parse registration file");
}
const clientFactory = new ClientFactory({
appServiceUserId: "@" + registration.sender_localpart + ":" + config.bridge.domain,
token: registration.as_token,
url: config.bridge.homeserverUrl,
});
const client = clientFactory.getClientAs();
client.startClient();
client.on("sync", (state, prevState, data) => {
switch (state) {
case "ERROR":
console.error("Sync failed.", data);
break;
case "SYNCING":
console.log("Syncing.");
break;
case "PREPARED":
const room = client.getRoom(options.roomid);
if (room === null) {
console.error("Room not found.");
process.exit(1);
}
const levels = room.getLiveTimeline().getState("f").getStateEvents("m.room.power_levels")[0];
client.setPowerLevel(options.roomid, options.userid, options.power, levels).then(() => {
console.log("Power levels set");
process.exit(0);
}).catch((err) => {
console.error("Could not apply power levels: ", err);
process.exit(1);
})
break;
default:
break;
}
});
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
"compileOnSave": true, "compileOnSave": true,
"include": [ "include": [
"src/**/*", "src/**/*",
"test/**/*" "test/**/*",
"tools/**/*"
] ]
} }
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter