UNPKG

@exoplay/exobot-plugin-points

Version:
356 lines (293 loc) 15.5 kB
require("regenerator-runtime/runtime"); (function(e, a) { for(var i in a) e[i] = a[i]; }(exports, /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) { /******/ return installedModules[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // identity function for calling harmony imports with the correct context /******/ __webpack_require__.i = function(value) { return value; }; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { /******/ configurable: false, /******/ enumerable: true, /******/ get: getter /******/ }); /******/ } /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 2); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports) { module.exports = require("@exoplay/exobot"); /***/ }), /* 1 */ /***/ (function(module, exports) { module.exports = require("clark"); /***/ }), /* 2 */ /***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "default", function() { return Points; }); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__exoplay_exobot__ = __webpack_require__(0); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__exoplay_exobot___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_0__exoplay_exobot__); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_1_clark__ = __webpack_require__(1); /* harmony import */ var __WEBPACK_IMPORTED_MODULE_1_clark___default = __webpack_require__.n(__WEBPACK_IMPORTED_MODULE_1_clark__); var _createClass = function () { function defineProperties(target, props) { for (var i = 0, descriptor; i < props.length; i++) { descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || !1; descriptor.configurable = !0; if ("value" in descriptor) descriptor.writable = !0; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _dec11, _dec12, _dec13, _dec14, _dec15, _desc, _value, _class, _class2, _temp2; function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg), value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: !1, writable: !0, configurable: !0 } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } 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 = !0; } 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 = void 0; } if (desc.initializer === void 0) { Object['defineProperty'](target, property, desc); desc = null; } return desc; } const nameToId = name => { return name.trim().replace(/\s/g, '').toLowerCase(); }; /* harmony export (immutable) */ __webpack_exports__["nameToId"] = nameToId; const POINT_REGEX = /^((?![\+\-]{2}|\s)\w+):?\s*([\+\-]{2})(?:\s+(?:for|because|cause|cuz) (.*))?$/, REPEAT_REGEX = /^([\+\-]{2})(?:\s+(?:for|because|cause|cuz)\s(.*))?$/; let Points = (_dec = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__exoplay_exobot__["permissionGroup"])('points'), _dec2 = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__exoplay_exobot__["help"])('thing++ or thing-- to add or remove points. Optionally, "thing++ for <reason>"'), _dec3 = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__exoplay_exobot__["listen"])(POINT_REGEX), _dec4 = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__exoplay_exobot__["permissionGroup"])('points'), _dec5 = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__exoplay_exobot__["help"])('++ or -- alone will add (or remove) points from the most recently up/downvoted thing'), _dec6 = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__exoplay_exobot__["listen"])(REPEAT_REGEX), _dec7 = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__exoplay_exobot__["permissionGroup"])('points'), _dec8 = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__exoplay_exobot__["help"])('exobot++ or exobot-- adds or removes points from exobot'), _dec9 = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__exoplay_exobot__["respond"])(REPEAT_REGEX), _dec10 = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__exoplay_exobot__["permissionGroup"])('points'), _dec11 = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__exoplay_exobot__["help"])('/top <n> to show top <n> users.'), _dec12 = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__exoplay_exobot__["respond"])(/^tops?\s*(\d*)?$/i), _dec13 = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__exoplay_exobot__["permissionGroup"])('points'), _dec14 = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__exoplay_exobot__["help"])('/score <user> to show score for a user.'), _dec15 = __webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__exoplay_exobot__["respond"])(/^score(?: for)?(.*)$/i), (_class = (_temp2 = _class2 = function (_Plugin) { _inherits(Points, _Plugin); function Points(...args) { var _temp, _this, _ret; _classCallCheck(this, Points); return _ret = (_temp = (_this = _possibleConstructorReturn(this, (Points.__proto__ || Object.getPrototypeOf(Points)).call(this, ...args)), _this), _this.defaultDatabase = { points: { things: {}, tops: [] } }, _this.recentVotes = {}, _this.lastVote = {}, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(Points, [{ key: 'isSpam', value: function isSpam(userName, thing) { const recentVotes = this.recentVotes[userName]; if (!recentVotes) { this.recentVotes[userName] = {}; } const recentVote = this.recentVotes[userName][thing], now = Date.now(); if (!recentVote) { this.recentVotes[userName][thing] = now; return !1; } if ((now - recentVote) / 1e3 < this.options.spamTimeoutSeconds) { return !0; } } }, { key: 'changePoints', value: (() => { var _ref = _asyncToGenerator(function* ([, name, change, reason], message) { this.lastVote[message.channel] = { name, change, reason }; if (this.isSpam(message.user.name, name)) { return; } const id = nameToId(name); let points = this.bot.db.get(`points.things.${id}`).value(); if (!points) { points = this.buildPoints(name, id); } if (change === '++') { points.points = points.points + 1; } else { points.points = points.points - 1; } const ret = [`${name} has ${points.points} points`]; if (reason) { const reasonId = nameToId(reason), existingReason = points.reasons[reasonId] || { score: 0, reason }; if (change === '++') { existingReason.score++; } else { existingReason.score--; } points.reasons[reasonId] = existingReason; ret.push([`${existingReason.score} of which ${existingReason.score === 1 ? 'is' : 'are'} for ${reason}`]); } this.bot.db.set(`points.things.${id}`, points).value(); this.bot.db.write(); this.updateTops(); return ret.join(', '); }); return function changePoints() { return _ref.apply(this, arguments); }; })() }, { key: 'repeat', value: (() => { var _ref2 = _asyncToGenerator(function* ([, change, reason], message) { const lastVote = this.lastVote[message.channel]; if (!lastVote) { return; } // re-run changepoints with the same name, the new change, and either a new // reason or the old one return yield this.changePoints([void 0, lastVote.name, change, reason || lastVote.reason], message); }); return function repeat() { return _ref2.apply(this, arguments); }; })() }, { key: 'exobotPoints', value: (() => { var _ref3 = _asyncToGenerator(function* ([, change, reason], message) { const lastVote = this.lastVote[message.channel]; if (!lastVote) { return; } // re-run changepoints with the same name, the new change, and either a new // reason or the old one return yield this.changePoints([void 0, this.bot.options.name, change, reason || lastVote.reason], message); }); return function exobotPoints() { return _ref3.apply(this, arguments); }; })() }, { key: 'tops', value: (() => { var _ref4 = _asyncToGenerator(function* ([, n = 10]) { if (n > 25) { n = 25; } const scores = this.bot.db.get('points.things').value(), tops = this.bot.db.get('points.tops').slice(0, n).value(), text = [__WEBPACK_IMPORTED_MODULE_1_clark___default()(tops.map(function (t) { return scores[t].points; }))]; text.push(`Top ${Math.min(n, tops.length)}:`); tops.forEach(function (t) { return text.push(`${scores[t].name}: ${scores[t].points}`); }); return text.join('\n'); }); return function tops() { return _ref4.apply(this, arguments); }; })() }, { key: 'score', value: (() => { var _ref5 = _asyncToGenerator(function* ([, name]) { name = name.trim(); const id = nameToId(name), points = this.bot.db.get(`points.things.${id}`).value(), { reasons } = points, text = [`${name} has ${points.points} points.`], reasonKeys = Object.keys(points.reasons); if (reasonKeys.length > 0) { text.push('Here are some reasons:'); reasonKeys.forEach(function (r) { text.push(`${reasons[r].reason}: ${reasons[r].score} points`); }); } return text.join('\n'); }); return function score() { return _ref5.apply(this, arguments); }; })() }, { key: 'updateTops', value: (() => { var _ref6 = _asyncToGenerator(function* () { const tops = this.bot.db.get('points.things').orderBy('points', 'desc').map('id').value(); this.bot.db.set('points.tops', tops).value(); this.bot.db.write(); }); return function updateTops() { return _ref6.apply(this, arguments); }; })() }, { key: 'buildPoints', value: function buildPoints(name, id) { return { name, id, points: 0, reasons: {} }; } }]); return Points; }(__WEBPACK_IMPORTED_MODULE_0__exoplay_exobot__["Plugin"]), _class2.type = 'points', _class2.propTypes = { spamTimeoutSeconds: __WEBPACK_IMPORTED_MODULE_0__exoplay_exobot__["PropTypes"].number.isRequired }, _class2.defaultProps = { spamTimeoutSeconds: 300 }, _temp2), (_applyDecoratedDescriptor(_class.prototype, 'changePoints', [_dec, _dec2, _dec3], Object.getOwnPropertyDescriptor(_class.prototype, 'changePoints'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'repeat', [_dec4, _dec5, _dec6], Object.getOwnPropertyDescriptor(_class.prototype, 'repeat'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'exobotPoints', [_dec7, _dec8, _dec9], Object.getOwnPropertyDescriptor(_class.prototype, 'exobotPoints'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'tops', [_dec10, _dec11, _dec12], Object.getOwnPropertyDescriptor(_class.prototype, 'tops'), _class.prototype), _applyDecoratedDescriptor(_class.prototype, 'score', [_dec13, _dec14, _dec15], Object.getOwnPropertyDescriptor(_class.prototype, 'score'), _class.prototype)), _class)); /***/ }) /******/ ]))); //# sourceMappingURL=points.js.map