commit 1c5caab5f81b9dff3e3d3e23e32f923e433757be
parent 416c9c437bdfcc01beb5c497353226c2c66b4834
Author: MTRNord <mtrnord1@gmail.com>
Date: Tue, 20 Jun 2017 16:01:15 +0200
Add !Projects command
Signed-off-by: MTRNord <mtrnord1@gmail.com>
Diffstat:
4 files changed, 33 insertions(+), 15 deletions(-)
diff --git a/src/commands.js b/src/commands.js
@@ -1,4 +1,6 @@
import Weblate from './weblate';
+import config from '../config.json';
+
export default class Commands {
constructor(client){
this.client = client;
@@ -21,4 +23,18 @@ export default class Commands {
console.log(err);
});
};
+
+ projects = room => {
+ return this.weblate.getProjects().then(body => {
+ let message = 'Projects on the Server:\n';
+ for (let key in body['results']) {
+ message += body['results'][key]["name"] + '\n'
+ message += 'Direct Link: '+ config["weblateApiRoot"].replace('api/', 'projects/') + body['results'][key]["slug"] + '\n'
+ }
+ return this.client.sendNotice(room.roomId, message).catch(error => console.log(error));
+ })
+ .catch(err => {
+ console.log(err);
+ });
+ };
}
diff --git a/src/index.js b/src/index.js
@@ -13,11 +13,6 @@ mx.getClient().then(client => {
if (event.getSender() === "@MTRNord:matrix.org") return;
if (room.roomId !== "!EDuQpgunAOTYiUKIcU:matrix.org" && room.roomId !== "!OYyXUbcTKVsDBUniNn:matrix.eclabs.de") return;
const message = "" + event.getContent().body;
- /*console.log(
- // the room name will update with m.room.name events automatically
- "(%s) %s :: %s", room.name, event.getSender(), message
- );
- client.sendHtmlNotice(room.roomId, message, message).catch(error => console.log(error));*/
if (message.startsWith("!help")){
console.log(
"(%s) %s :: %s", room.name, event.getSender(), message
@@ -28,6 +23,11 @@ mx.getClient().then(client => {
"(%s) %s :: %s", room.name, event.getSender(), message
);
commands.languages(room);
+ } else if (message.startsWith("!projects")){
+ console.log(
+ "(%s) %s :: %s", room.name, event.getSender(), message
+ );
+ commands.projects(room);
}
});
client.startClient();
diff --git a/src/weblate.js b/src/weblate.js
@@ -11,13 +11,15 @@ export default class Weblate {
json: true // Automatically parses the JSON string in the response
};
return rp(options);
- /*.then(body => {
- for (let key in body['results']) {
- console.log(body['results'][key]['code']);
- }
- })
- .catch(err => {
- console.log(err);
- });*/
+ };
+ getProjects = () => {
+ const options = {
+ uri: config["weblateApiRoot"] + "projects",
+ headers: {
+ 'Authorization': 'Token ' + config['weblate_token']
+ },
+ json: true // Automatically parses the JSON string in the response
+ };
+ return rp(options);
}
}
\ No newline at end of file
diff --git a/test/commands_test.js b/test/commands_test.js
@@ -9,16 +9,16 @@ chai.should();
describe('Commands', () => {
const mx = new Login;
+ const room = {roomId: '!EDuQpgunAOTYiUKIcU:matrix.org'};
mx.getClient().then(client => {
+ client.sendTextMessage(room.roomId, "Test messages are incoming!\n").catch(error => console.log(error));
it('should send help', done => {
const CommandHandler = new Commands(client);
- const room = {roomId: '!EDuQpgunAOTYiUKIcU:matrix.org'};
const help = CommandHandler.help(room);
help.should.be.fulfilled.and.notify(done);
});
it('should send languages', done => {
const CommandHandler = new Commands(client);
- const room = {roomId: '!EDuQpgunAOTYiUKIcU:matrix.org'};
const languages = CommandHandler.languages(room);
languages.should.be.fulfilled.and.notify(done);
});