UNPKG

munar-plugin-emotes

Version:

Munar plugin for sharing reaction GIFs.

187 lines (150 loc) 6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _munarCore = require("munar-core"); var _got = _interopRequireDefault(require("got")); var _bluebird = _interopRequireDefault(require("bluebird")); var _models = require("./models"); var _serve = require("./serve"); var _dec, _dec2, _dec3, _dec4, _class, _class2, _temp; function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; } const debug = require('debug')('munar:emotes'); const cleanId = id => id.toLowerCase(); const IMGUR = /^https?:\/\/i\.imgur\.com\//; const argEmoteName = _munarCore.command.arg.string().description('Emote Name'); let Emotes = (_dec = (0, _munarCore.command)('addemote', { role: _munarCore.permissions.MODERATOR, ninjaVanish: true, description: 'Add a new emote.', arguments: [argEmoteName.required(), _munarCore.command.arg.string().uri().description('Image URL').required()] }), _dec2 = (0, _munarCore.command)('delemote', { role: _munarCore.permissions.MODERATOR, description: 'Remove an emote.', arguments: [argEmoteName.required()] }), _dec3 = (0, _munarCore.command)('emotes', { description: 'Show a list of emotes.' }), _dec4 = (0, _munarCore.command)('emote', 'e', { description: 'Use an emote.', arguments: [argEmoteName.required(), _munarCore.command.arg.user().description('User to send the emote to.')] }), (_class = (_temp = _class2 = class Emotes extends _munarCore.Plugin { constructor(bot, options) { super(bot, options); this.models({ Emote: _models.EmoteModel }); } sendEmote(message, target, emote) { if (target) { message.send(`@${target.username} ${emote}`); } else { message.reply(emote); } } saveEmote(user, id, url) { const Emote = this.model('Emote'); return Emote.findByIdAndUpdate(id, { url, addedBy: user.id }, { upsert: true }); } async upload(form) { const { body } = await _got.default.post('https://api.imgur.com/3/image', { headers: { Authorization: `Client-ID ${this.options.key}` }, body: form, form: true, json: true }); if (body && body.success) { return body.data.link.replace('http:', 'https:'); } else { console.log(body); } } reupload(title, url, cb) { return this.upload({ type: 'url', title: title, image: url }); } async addemote(message, id, url) { const User = this.model('User'); const user = await User.from(message.user); id = cleanId(id); debug('addemote', id, url); let emote = null; if (this.options.reupload && this.options.key && !IMGUR.test(url)) { message.reply(':satellite: Reuploading to imgur', 10 * 1000); try { const imgurUrl = await this.reupload(id, url); emote = await this.saveEmote(user, id, imgurUrl); } catch (e) { message.reply(':warning: Could not reupload to imgur, ' + 'using the original URL instead...', 3 * 1000); } } if (!emote) { emote = await this.saveEmote(user, id, url); } debug('added', id, url); message.reply(`Emote "${id}" updated!`, 10 * 1000); } async delemote(message, id) { debug('delemote', id); await this.model('Emote').remove({ _id: id }); debug('deleted', id); message.reply(`Emote "${id}" removed!`, 10 * 1000); } async emotes(message) { debug('listing emotes'); let url; const server = this.bot.getPlugin('serve'); if (server) { url = server.getUrl('emotes'); } if (this.options.url) { url = this.options.url; } if (url) { message.reply(`Emotes can be found at ${url} !`); return; } const emotes = await this.model('Emote').find().sort('_id'); const emoteIds = emotes.map(emote => emote.id); message.reply(`Emotes: ${emoteIds.join(', ')}`); } async emote(message, id, username) { if (!id) return; let target; if (username) { target = message.source.getUserByName(username); } if (!target) { target = message.user; } const emote = await this.model('Emote').findById(cleanId(id)); if (emote) { this.sendEmote(message, target, emote.url); } } async serve(req, res) { res.setHeader('content-type', 'text/html'); const emotes = await this.model('Emote').find().sort('_id'); return (0, _serve.renderEmotesList)(emotes); } }, _class2.description = 'Reaction GIF repository', _class2.defaultOptions = { url: false, reupload: false, key: '' }, _temp), (_applyDecoratedDescriptor(_class.prototype, "addemote", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "addemote"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "delemote", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "delemote"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "emotes", [_dec3], Object.getOwnPropertyDescriptor(_class.prototype, "emotes"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "emote", [_dec4], Object.getOwnPropertyDescriptor(_class.prototype, "emote"), _class.prototype)), _class)); exports.default = Emotes;