3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-27 06:19:24 +01:00

Merge pull request #674 from Scritches/master

Bunch of little fixes and style updates
This commit is contained in:
Luke Slater 2018-04-14 20:14:57 +01:00 committed by GitHub
commit 944827c4d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 25 deletions

View File

@ -3,12 +3,12 @@
* Description: Interacts with the GoodReads API to provide book-oriented functionality to dbot
*/
const util = require('util'),
var util = require('util'),
_ = require('underscore')._,
rp = require('request-promise-native'),
parseString = util.promisify(require('xml2js').parseString);
const GoodReads = function(dbot) {
var GoodReads = function(dbot) {
this.apiRoot = 'https://www.goodreads.com';
this.internalAPI = {
@ -37,7 +37,7 @@ const GoodReads = function(dbot) {
this.api = {
'findBook': async term => {
//https://www.goodreads.com/search/index.xml
const body = await rp({
var body = await rp({
uri: this.apiRoot + '/search/index.xml',
qs: {
key: this.config.api_key,
@ -45,10 +45,10 @@ const GoodReads = function(dbot) {
}
});
const response = await parseString(body, { explicitArray: false });
var response = await parseString(body, { explicitArray: false });
if(!_.has(response, 'GoodreadsResponse')) throw 'goodreads-error';
const result = response.GoodreadsResponse.search.results;
var result = response.GoodreadsResponse.search.results;
if(!result || !_.has(result, 'work')) throw 'book-not-found';
if(!result.work[0]) throw 'book-not-found';
@ -62,7 +62,7 @@ const GoodReads = function(dbot) {
'getSummaryForBook': async id => {
//https://www.goodreads.com/book/show.xml
const body = await rp({
var body = await rp({
uri: this.apiRoot + '/book/show.xml',
qs: {
key: this.config.api_key,
@ -70,10 +70,10 @@ const GoodReads = function(dbot) {
}
});
const response = await parseString(body, { explicitArray: false });
var response = await parseString(body, { explicitArray: false });
if(!_.has(response, 'GoodreadsResponse')) throw 'goodreads-error';
const result = response.GoodreadsResponse.book;
var result = response.GoodreadsResponse.book;
if(!result) throw 'book-not-found';
if(!_.has(result, 'description')) throw 'no-description';
@ -82,17 +82,17 @@ const GoodReads = function(dbot) {
'findAuthor': async term => {
//https://www.goodreads.com/api/author_url/<ID>
const body = await rp({
var body = await rp({
url: this.apiRoot + '/api/author_url/' + term,
qs: {
key: this.config.api_key
}
});
const response = await parseString(body, {explicitArray: false });
var response = await parseString(body, {explicitArray: false });
if(!_.has(response, 'GoodreadsResponse')) throw 'goodreads-error';
const result = response.GoodreadsResponse.author;
var result = response.GoodreadsResponse.author;
if(!result) throw 'author-not-found';
return {
@ -121,10 +121,10 @@ const GoodReads = function(dbot) {
throw e;
}
const response = await parseString(body, { explicitArray: false });
var response = await parseString(body, { explicitArray: false });
if(!_.has(response, 'GoodreadsResponse')) throw 'goodreads-error';
const result = response.GoodreadsResponse.user;
var result = response.GoodreadsResponse.user;
if(!result) throw 'user-not-found';
return this.internalAPI.formatProfile(result);
@ -150,10 +150,10 @@ const GoodReads = function(dbot) {
throw e;
}
const response = await parseString(body, { explicitArray: false });
var response = await parseString(body, { explicitArray: false });
if(!_.has(response, 'GoodreadsResponse')) throw 'goodreads-error';
const result = response.GoodreadsResponse.user;
var result = response.GoodreadsResponse.user;
if(!result) throw 'user-not-found';
return this.internalAPI.formatProfile(result);
@ -171,7 +171,7 @@ const GoodReads = function(dbot) {
}
});
const response = await parseString(body, { explicitArray: false });
var response = await parseString(body, { explicitArray: false });
if(!_.has(response, 'GoodreadsResponse')) throw 'goodreads-error';
let result = response.GoodreadsResponse.reviews.review;
@ -193,7 +193,7 @@ const GoodReads = function(dbot) {
this.commands = {
'~book' : async evt => {
try {
const book = await this.api.findBook(evt.input[1]);
var book = await this.api.findBook(evt.input[1]);
evt.reply(dbot.t('gr_book', {
author: book.author,
title: book.title,
@ -206,9 +206,8 @@ const GoodReads = function(dbot) {
'~booksummary': async evt => {
try {
console.log(evt.input[1]);
const book = await this.api.findBook(evt.input[1]);
const summary = await this.api.getSummaryForBook(book.id);
var book = await this.api.findBook(evt.input[1]);
var summary = await this.api.getSummaryForBook(book.id);
evt.reply(dbot.t('gr_summary', {
title: book.title,
summary: summary,
@ -228,7 +227,7 @@ const GoodReads = function(dbot) {
'~reading': async (evt, profile) => {
try {
let books = await this.api.getShelfForUserId(profile.id, 'currently-reading');
const booksCount = books.length;
var booksCount = books.length;
if(!booksCount) {
evt.reply(dbot.t('gr_not_reading', { user: evt.rUser.currentNick }));
return;
@ -259,7 +258,7 @@ const GoodReads = function(dbot) {
_.each(this.commands, ((cmd, cmdName) => {
if(cmd.requiresProfile) {
this.commands[cmdName] = (async evt => {
const grUsername = evt.rProfile.goodreads;
var grUsername = evt.rProfile.goodreads;
if(!grUsername) {
evt.reply(evt.rUser.currentNick + ': Set a Goodreads username with "~set goodreads username"');

View File

@ -297,7 +297,7 @@ var commands = function(dbot) {
event.reply(orderedKickLeague(dbot.db.kicks, 'Kicked'));
event.reply(orderedKickLeague(dbot.db.kickers, 'Kickers'));
},
'~votequiet': function(event) {
var target = event.input[1],
reason = event.input[2];
@ -353,6 +353,11 @@ var commands = function(dbot) {
if(vq.yes.length == 4) {
event.reply('Attempt to quiet ' + target + ' succeeded. Count: Yes (' + vq.yes.length + '). No (' + vq.no.length + ').');
this.api.quietUser(event.server, event.rUser, '10m', event.channel, target, reason + '[votequiet]', function(response) {
clearTimeout(vq.timer);
vq.spent = true;
setTimeout(function() {
delete this.voteQuiets[user.id];
}.bind(this), 600000);
event.reply(response);
});
}
@ -391,6 +396,10 @@ var commands = function(dbot) {
event.reply('Attempt to quiet ' + target + ' succeeded. Count: Yes (' + vq.yes.length + '). No (' + vq.no.length + ').');
this.api.quietUser(event.server, event.rUser, '10m', event.channel, target, vq.reason + '[votequiet]', function(response) {
clearTimeout(vq.timer);
vq.spent = true;
setTimeout(function() {
delete this.voteQuiets[user.id];
}.bind(this), 600000);
event.reply(response);
});
}

View File

@ -155,7 +155,7 @@ var lastfm = function(dbot) {
event.reply(dbot.t('lfm_profile', {
'user': user.currentNick,
'plays': profile.playcount.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,"),
'date': moment(profile.registered['#text']).format('DD/MM/YYYY'),
'date': moment(profile.registered['#text'] * 1000).format('DD/MM/YYYY'),
'link': profile.url
}));
} else {

View File

@ -27,7 +27,7 @@ var link = function(dbot) {
page = request(link.replace('https', 'http'), function(error, response, body) {
if(!error && response.statusCode == 200) {
body = body.replace(/(\r\n|\n\r|\n)/gim, " ");
var title = body.valMatch(/<title>(.*?)<\/title>/, 2);
var title = body.valMatch(/<title>(.*?)<\\?\/title>/, 2);
if(title && title.length < 140) {
callback(ent.decode(title[1]).trim());
}