Skip to content

Instantly share code, notes, and snippets.

@ticklemynausea
Created February 9, 2017 12:12
Show Gist options
  • Select an option

  • Save ticklemynausea/d42f8a750472d4f8354a97aedc73a18d to your computer and use it in GitHub Desktop.

Select an option

Save ticklemynausea/d42f8a750472d4f8354a97aedc73a18d to your computer and use it in GitHub Desktop.

terms module

Dictionary and definition database

'use strict';
const _ = require('lodash');
const models = include('app/models');
module.exports = (bot, plugin) => ({
id: 'definitions',
description: 'A definitions database',
messageTemplates: {
unknown: 'I don\'t know about {term}',
added: 'Added definition for {term}',
removed: 'Removed definition of {term}',
invalid: 'Invalid slot {position}'
},
commands: [
{
id: 'definitions_search',
description: [
'Searches stored descriptions',
'?? term'
],
triggers: [
/^\?\? (\S+)$/
],
callback: (data) => {
let term = data.match[1];
return bot.model.getDefinitions({
where: {
term: term
},
order: [['updatedAt', 'ASC']]
})
.then((definitions) => {
if (definitions.length === 0) {
return bot.client.say(data.reply, plugin.templates.unknown({ term: term }));
}
if (definitions.length === 1) {
return bot.client.say(data.reply, definitions[0].description);
}
return bot.client.say(data.reply, definitions.map((definition, n) => {
return `(${n+1}) ${definition.description}`;
}).join(', '));
});
}
},
{
id: 'definitions_add',
description: [
'Adds a term description',
'!define <term> <description>'
],
triggers: [
/^!define (\S+) (.+)$/,
/^!learn (\S+) (.+)$/,
/^!learn+ (\S+) (.+)$/
],
level: 90,
callback: (data) => {
let term = data.match[1];
let description = data.match[2];
let definition = models.Definition.build({
term: term,
description: description,
UserId: _.get(data, 'botUser.User.id'),
BotId: _.get(data, 'botUser.Bot.id'),
});
return definition.save()
.then(() => bot.client.say(data.reply, plugin.templates.added({ term: term })));
}
},
{
id: 'definitions_del',
description: [
'Removes a term description',
'!undefine <term> [n]'
],
triggers: [
/^!undefine (\S+)( (\d+))?$/,
/^!unlearn (\S+)( (\d+))?$/,
/^!learn- (\S+)( (\d+))?$/
],
level: 90,
callback: (data) => {
let term = data.match[1];
let position = data.match[3];
return bot.model.getDefinitions({
where: {
term: term
},
order: [['updatedAt', 'ASC']]
})
.then((definitions) => {
if (definitions.length === 0) {
return bot.client.say(data.reply, plugin.templates.unknown({ term: term }));
}
if (!position) {
position = definitions.length;
}
if (position <= 0 || position > definitions.length) {
return bot.client.say(data.reply, plugin.templates.invalid({ position: position }));
}
let definition = definitions[position-1];
return definition.destroy()
.then(() => bot.client.say(data.reply, plugin.templates.removed({ term: term })));
});
}
}
]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment