discord-coc-bot
Version:
A Discord bot that contains commands useful for Call of Cthulu text roleplaying, based on RPbot by Gawdl3y (https://github.com/Gawdl3y/discord-rpbot/)
409 lines (348 loc) • 11.8 kB
JavaScript
'use babel';
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _discordGraf = require('discord-graf');
var _diceExpressionEvaluator = require('dice-expression-evaluator');
var _diceExpressionEvaluator2 = _interopRequireDefault(_diceExpressionEvaluator);
var _commonTags = require('common-tags');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
Function.prototype.$asyncbind = function $asyncbind(self, catcher) {
"use strict";
if (!Function.prototype.$asyncbind) {
Object.defineProperty(Function.prototype, "$asyncbind", {
value: $asyncbind,
enumerable: false,
configurable: true,
writable: true
});
}
if (!$asyncbind.trampoline) {
$asyncbind.trampoline = function trampoline(t, x, s, e, u) {
return function b(q) {
while (q) {
if (q.then) {
q = q.then(b, e);
return u ? undefined : q;
}
try {
if (q.pop) {
if (q.length) return q.pop() ? x.call(t) : q;
q = s;
} else q = q.call(t);
} catch (r) {
return e(r);
}
}
};
};
}
if (!$asyncbind.LazyThenable) {
$asyncbind.LazyThenable = function () {
function isThenable(obj) {
return obj && obj instanceof Object && typeof obj.then === "function";
}
function resolution(p, r, how) {
try {
var x = how ? how(r) : r;
if (p === x) return p.reject(new TypeError("Promise resolution loop"));
if (isThenable(x)) {
x.then(function (y) {
resolution(p, y);
}, function (e) {
p.reject(e);
});
} else {
p.resolve(x);
}
} catch (ex) {
p.reject(ex);
}
}
function _unchained(v) {}
function thenChain(res, rej) {
this.resolve = res;
this.reject = rej;
}
function Chained() {}
;
Chained.prototype = {
resolve: _unchained,
reject: _unchained,
then: thenChain
};
function then(res, rej) {
var chain = new Chained();
try {
this._resolver(function (value) {
return isThenable(value) ? value.then(res, rej) : resolution(chain, value, res);
}, function (ex) {
resolution(chain, ex, rej);
});
} catch (ex) {
resolution(chain, ex, rej);
}
return chain;
}
function Thenable(resolver) {
this._resolver = resolver;
this.then = then;
}
;
Thenable.resolve = function (v) {
return Thenable.isThenable(v) ? v : {
then: function (resolve) {
return resolve(v);
}
};
};
Thenable.isThenable = isThenable;
return Thenable;
}();
$asyncbind.EagerThenable = $asyncbind.Thenable = ($asyncbind.EagerThenableFactory = function (tick) {
tick = tick || typeof process === "object" && process.nextTick || typeof setImmediate === "function" && setImmediate || function (f) {
setTimeout(f, 0);
};
var soon = function () {
var fq = [],
fqStart = 0,
bufferSize = 1024;
function callQueue() {
while (fq.length - fqStart) {
try {
fq[fqStart]();
} catch (ex) {}
fq[fqStart++] = undefined;
if (fqStart === bufferSize) {
fq.splice(0, bufferSize);
fqStart = 0;
}
}
}
return function (fn) {
fq.push(fn);
if (fq.length - fqStart === 1) tick(callQueue);
};
}();
function Zousan(func) {
if (func) {
var me = this;
func(function (arg) {
me.resolve(arg);
}, function (arg) {
me.reject(arg);
});
}
}
Zousan.prototype = {
resolve: function (value) {
if (this.state !== undefined) return;
if (value === this) return this.reject(new TypeError("Attempt to resolve promise with self"));
var me = this;
if (value && (typeof value === "function" || typeof value === "object")) {
try {
var first = 0;
var then = value.then;
if (typeof then === "function") {
then.call(value, function (ra) {
if (!first++) {
me.resolve(ra);
}
}, function (rr) {
if (!first++) {
me.reject(rr);
}
});
return;
}
} catch (e) {
if (!first) this.reject(e);
return;
}
}
this.state = STATE_FULFILLED;
this.v = value;
if (me.c) soon(function () {
for (var n = 0, l = me.c.length; n < l; n++) STATE_FULFILLED(me.c[n], value);
});
},
reject: function (reason) {
if (this.state !== undefined) return;
this.state = STATE_REJECTED;
this.v = reason;
var clients = this.c;
if (clients) soon(function () {
for (var n = 0, l = clients.length; n < l; n++) STATE_REJECTED(clients[n], reason);
});
},
then: function (onF, onR) {
var p = new Zousan();
var client = {
y: onF,
n: onR,
p: p
};
if (this.state === undefined) {
if (this.c) this.c.push(client);else this.c = [client];
} else {
var s = this.state,
a = this.v;
soon(function () {
s(client, a);
});
}
return p;
}
};
function STATE_FULFILLED(c, arg) {
if (typeof c.y === "function") {
try {
var yret = c.y.call(undefined, arg);
c.p.resolve(yret);
} catch (err) {
c.p.reject(err);
}
} else c.p.resolve(arg);
}
function STATE_REJECTED(c, reason) {
if (typeof c.n === "function") {
try {
var yret = c.n.call(undefined, reason);
c.p.resolve(yret);
} catch (err) {
c.p.reject(err);
}
} else c.p.reject(reason);
}
Zousan.resolve = function (val) {
if (val && val instanceof Zousan) return val;
var z = new Zousan();
z.resolve(val);
return z;
};
Zousan.reject = function (err) {
if (err && err instanceof Zousan) return err;
var z = new Zousan();
z.reject(err);
return z;
};
Zousan.version = "2.3.3-nodent";
return Zousan;
})();
}
function boundThen() {
return resolver.apply(self, arguments);
}
var resolver = this;
switch (catcher) {
case true:
return new $asyncbind.Thenable(boundThen);
case 0:
return new $asyncbind.LazyThenable(boundThen);
case undefined:
boundThen.then = boundThen;
return boundThen;
default:
return function () {
try {
return resolver.apply(self, arguments);
} catch (ex) {
return catcher(ex);
}
};
}
};
const pattern = /^(.+?)(?:(>{1,2}|<{1,2})\s*([0-9]+?))?\s*$/;
class RollDiceCommand extends _discordGraf.Command {
constructor(bot) {
super(bot, {
name: 'roll',
aliases: ['dice', 'roll-dice', 'dice-roll', '(roll: xxxx)'],
module: 'dice',
memberName: 'roll',
description: 'Rolls specified dice.',
usage: 'roll [dice expression]',
details: _commonTags.oneLine`
Dice expressions can contain the standard representations of dice in text form (e.g. 2d20 is two 20-sided dice), with addition and subtraction allowed.
You may also use a single \`>\` or \`<\` symbol at the end of the expression to add a target for the total dice roll - for example, \`2d20 + d15 > 35\`.
You can count the number of successes using \`>>\` or \`<<\`, but only on a single dice expression - for example, \`4d30 >> 20\`.
When running the command with no dice expression, it will default to a D100.
When just a single plain number is provided, it will be interpreted as a single die with that many sides.
`,
examples: ['roll 2d20', 'roll 3d20 - d10 + 6', 'roll d20 > 10', 'roll 6d20 >> 14', 'roll', 'roll 30', 'Billy McBillface attempts to slay the dragon. (Roll: d20 > 10)'],
patterns: [/\(\s*(?:roll|dice|rolldice|diceroll):\s*(.+?)(?:(>{1,2}|<{1,2})\s*([0-9]+?))?\s*\)/i]
});
}
run(message, args, fromPattern) {
return new Promise(function ($return, $error) {
// eslint-disable-line complexity
const firstArgIndex = fromPattern ? 1 : 0;
if (!args[firstArgIndex]) {
args[firstArgIndex] = 'd100';
} else {
const rawNumber = parseInt(args[firstArgIndex]);
if (!isNaN(rawNumber) && String(rawNumber) === args[firstArgIndex]) args[firstArgIndex] = `d${rawNumber}`;
}
try {
const matches = fromPattern ? args : pattern.exec(args[0]);
const dice = new _diceExpressionEvaluator2.default(matches[1]);
// Restrict the maximum dice count
const totalDice = dice.dice.reduce((prev, die) => prev + (die.diceCount || 1), 0);
if (totalDice > 1000) return $return({ plain: `${message.author} might hurt themselves by rolling that many dice at once!` });
// Roll the dice
const rollResult = dice.roll();
this.bot.logger.debug('Dice rolled.', { dice: dice.dice, result: rollResult, totalDice: totalDice });
if (matches[2]) {
// Deal with target operations
const target = parseInt(matches[3]);
let response;
// Target for total roll
if (matches[2] === '>' || matches[2] === '<') {
const success = matches[2] === '>' ? rollResult.roll >= target : rollResult.roll <= target;
const hardBoundary = matches[2] === '>' ? Math.ceil((dice.max() - target) / 2) + target : Math.floor(target / 2);
const extremeBoundary = matches[2] === '>' ? Math.ceil((dice.max() - target) / 5 * 4) + target : Math.floor(target / 5);
const hardSuccess = matches[2] === '>' ? rollResult.roll >= hardBoundary + target : rollResult.roll <= hardBoundary;
const extremeSuccess = matches[2] === '>' ? rollResult.roll >= extremeBoundary : rollResult.roll <= extremeBoundary;
const diceList = this.buildDiceList(rollResult, totalDice);
response = _commonTags.oneLine`
${message.author} has **${success ? 'succeeded' : 'failed'}**.
Rolled ${rollResult.roll}, ${!success ? 'not ' : ''}${matches[2] === '>' ? 'greater than or equal to' : 'less than or equal to'} ${target}${diceList ? `; ${diceList}` : ''}.
${hardSuccess && !extremeSuccess ? `**Hard success reached (${hardBoundary}/${matches[2] === '>' ? dice.max() : target}).**` : ''} ${extremeSuccess ? `**Extreme success reached (${extremeBoundary}/${matches[2] === '>' ? dice.max() : target}).**` : ''}
`;
// Target for individual dice (success counting)
} else if (matches[2] === '>>' || matches[2] === '<<') {
if (rollResult.diceRaw.length !== 1) return $return({ plain: `${message.author} tried to count successes with multiple dice expressions.` });
const successes = rollResult.diceRaw[0].reduce((prev, die) => prev + (matches[2] === '>>' ? die >= target : die <= target), 0);
response = _commonTags.oneLine`
${message.author} has **${successes > 0 ? `succeeded ${successes} time${successes !== 1 ? 's' : ''}` : `failed`}**.
${rollResult.diceRaw[0].length > 1 && rollResult.diceRaw[0].length <= 100 ? `(${rollResult.diceRaw[0].join(', ')})` : ''}
`;
// Oh dear.
} else {
throw new Error('Unknown target operator. This should not ever happen.');
}
return $return({ plain: response, editable: false });
} else {
const diceList = this.buildDiceList(rollResult, totalDice);
return $return({
plain: `${message.author} rolled **${rollResult.roll}**.${diceList ? ` (${diceList})` : ''}`,
editable: false
});
}
} catch (err) {
return $return({ plain: `${message.author} specified an invalid dice expression.` });
}
return $return();
}.$asyncbind(this));
}
buildDiceList(result, totalDice) {
let diceList = '';
if (totalDice <= 100 && (result.diceRaw.length > 1 || result.diceRaw.length > 0 && result.diceRaw[0].length > 1)) {
diceList = result.diceRaw.map((res, i) => this.bot.util.nbsp(res.length > 1 ? `${res.join(' + ')} = ${result.diceSums[i]}` : res[0])).join(', ');
}
return diceList;
}
}
exports.default = RollDiceCommand;
//# sourceMappingURL=roll.js.map