3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-28 05:32:50 +01:00
dbot/modules/words/words.js

110 lines
3.9 KiB
JavaScript
Raw Normal View History

var Wordnik = require('wordnik');
var words = function(dbot) {
this.commands = {
'~define': function(event) {
var query = event.params[1];
2013-07-28 23:26:04 +02:00
this.wn.definitions(encodeURIComponent(query), function(err, defs) {
if(!err && defs[0]) {
2014-10-31 18:02:17 +01:00
event.reply(dbot.t('def', {
'word': query,
'definition': defs[0].text
}));
} else {
2014-10-31 18:02:17 +01:00
event.reply(dbot.t('no_def', { 'word': query }));
}
});
2013-07-08 13:02:27 +02:00
},
2013-12-27 17:24:16 +01:00
2014-10-31 05:19:05 +01:00
'~like': function(event) {
var query = event.params[1];
this.wn.word(query, {}, function(err, word) {
2014-10-31 05:27:08 +01:00
if(!err && word) {
2014-10-31 05:19:05 +01:00
word.related({
'limit': 10
}, function(err, related) {
2014-10-31 05:27:08 +01:00
if(related[0]) {
2014-10-31 18:02:17 +01:00
event.reply(dbot.t('def', {
'word': 'Words related to ' + query,
'definition': related[0].words.join(', ') + '.'
}));
2014-10-31 05:27:08 +01:00
} else {
2014-10-31 18:02:17 +01:00
event.reply(dbot.t('no_similar', { 'word': query }));
2014-10-31 05:27:08 +01:00
}
2014-10-31 05:19:05 +01:00
});
2014-10-31 05:21:33 +01:00
} else {
2014-10-31 18:02:17 +01:00
event.reply(dbot.t('no_word', { 'word': query }));
2014-10-31 05:19:05 +01:00
}
});
2014-10-31 18:02:17 +01:00
},
'~example': function(event) {
var query = event.params[1];
this.wn.word(query, {}, function(err, word) {
if(!err && word) {
word.topExample({}, function(err, example) {
if(!err && example) {
var rep = new RegExp(query, 'g');
event.reply(dbot.t('def', {
'word': query + ' example',
'definition': example.text.replace(rep, '\u00033'+query+'\u000f')
}));
} else {
event.reply(dbot.t('no_example', { 'word': query }));
}
});
} else {
event.reply(dbot.t('no_word', { 'word': query }));
}
});
},
'~rw': function(event) {
this.wn.randomWord(function(err, word) {
if(!err && word) {
this.wn.definitions(encodeURIComponent(word.word), function(err, defs) {
if(!err && defs[0]) {
event.reply(dbot.t('def', {
'word': word.word,
'definition': defs[0].text
}));
} else {
event.reply(dbot.t('no_def', { 'word': query }));
}
});
}
}.bind(this));
2014-10-31 05:19:05 +01:00
},
2013-12-27 17:24:16 +01:00
'~etymology': function(event) {
var query = event.params[1];
2013-12-27 17:38:59 +01:00
this.wn.etymologies(encodeURIComponent(query), {
'format': 'json'
}, function(err, defs) {
2013-12-27 17:24:16 +01:00
if(!err && defs[0]) {
2013-12-27 17:37:37 +01:00
event.reply(query + ' etymology: ' + defs[0]);
2013-12-27 17:24:16 +01:00
} else {
event.reply('No etymology found for ' + query);
}
});
},
2013-07-08 13:02:27 +02:00
'~jimble': function(event) {
event.reply(event.params[1].split('').sort(function() {
return (Math.round(Math.random()) - 0.5);
}).join(''));
}
};
2013-12-29 19:38:24 +01:00
this.commands['~jimble'].regex = [/^jimble (.+)$/, 2];
this.onLoad = function() {
this.wn = new Wordnik({
'api_key': this.config.api_key
});
}.bind(this);
};
exports.fetch = function(dbot) {
return new words(dbot);
};