You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

57 lines
1.7 KiB
JavaScript

const commands = require('./commands.js');
const fs = require('fs');
const sdk = require('matrix-js-sdk');
const yaml = require('js-yaml');
global.config = yaml.safeLoad(fs.readFileSync('config.yaml'));
global.matrixClient = sdk.createClient({
baseUrl: config.matrix.baseurl,
accessToken: config.matrix.accesstoken,
userId: config.matrix.userid
});
//captures unhandled rejections for easier debugging
process.on('unhandledRejection', (reason) => {
console.log('Reason: ' + reason);
});
matrixClient.on('Room.timeline', function(event, room, resetTimeline) {
if(resetTimeline) {
return;
}
if(event.getType() !== 'm.room.message') {
return;
}
const body = event.getContent().body;
commands.expandCommand(body, commands.projects)
.then((expansion) => {
let subcommand = commands.projects[expansion.project][expansion.service][expansion.subcommand];
const args = expansion.args.match(subcommand.regex);
if(args === null && !subcommand.regex.test(expansion.args)) {
matrixClient.sendNotice(room.roomId, 'usage: ' + subcommand.usage);
return;
}
subcommand.exec(args, room, event);
}, (fail) => { // command not found
let tmp = commands.suggestFix(body, commands.projects);
if(tmp !== null){ //when the regex matches nothing
let suggestions = "";
for(let i = 0; i < tmp.length; i++){
suggestions += "\n"+tmp[i];
}
matrixClient.sendNotice(room.roomId, 'Unrecognized command.\n' +
'Did you mean:'+suggestions);
}
});
});
matrixClient.startClient();