Skip to content

Commit

Permalink
feat(message): (list.#) will return prices for alias and commands
Browse files Browse the repository at this point in the history
Fixes #3726
  • Loading branch information
sogehige committed May 12, 2020
1 parent a9afb2d commit a6b59d7
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 20 deletions.
37 changes: 33 additions & 4 deletions src/bot/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { translate } from './translate';
import { getLocalizedName, isIgnored, parserReply, prepare } from './commons';
import currency from './currency';
import points from './systems/points';
import {default as priceSystem} from './systems/price';
import permissions from './permissions';
import users from './users';

Expand Down Expand Up @@ -451,9 +452,23 @@ class Message {
let listOutput: any = [];
switch (system) {
case 'alias':
return _.size(alias) === 0 ? ' ' : (_.map(alias, (o) => o.alias.replace('!', ''))).sort().join(', ');
return _.size(alias) === 0 ? ' ' : (_.map(alias, (o) => {
const findPrice = prices.find(p => p.command === o.alias);
if (findPrice && priceSystem.enabled) {
return o.alias.replace('!', '') + `(${findPrice.price} ${points.getPointsName(findPrice.price)})`;
} else {
return o.alias.replace('!', '');
}
})).sort().join(', ');
case '!alias':
return _.size(alias) === 0 ? ' ' : (_.map(alias, 'alias')).sort().join(', ');
return _.size(alias) === 0 ? ' ' : (_.map(alias, (o) => {
const findPrice = prices.find(p => p.command === o.alias);
if (findPrice && priceSystem.enabled) {
return o.alias + `(${findPrice.price} ${points.getPointsName(findPrice.price)})`;
} else {
return o.alias;
}
})).sort().join(', ');
case 'command':
if (permission) {
const responses = commands.map(o => o.responses).flat();
Expand All @@ -465,7 +480,14 @@ class Message {
commands = [];
}
}
return _.size(commands) === 0 ? ' ' : (_.map(commands, (o) => o.command.replace('!', ''))).sort().join(', ');
return _.size(commands) === 0 ? ' ' : (_.map(commands, (o) => {
const findPrice = prices.find(p => p.command === o.command);
if (findPrice && priceSystem.enabled) {
return o.command.replace('!', '') + `(${findPrice.price} ${points.getPointsName(findPrice.price)})`;
} else {
return o.command.replace('!', '');
}
})).sort().join(', ');
case '!command':
if (permission) {
const responses = commands.map(o => o.responses).flat();
Expand All @@ -477,7 +499,14 @@ class Message {
commands = [];
}
}
return _.size(commands) === 0 ? ' ' : (_.map(commands, 'command')).sort().join(', ');
return _.size(commands) === 0 ? ' ' : (_.map(commands, (o) => {
const findPrice = prices.find(p => p.command === o.command);
if (findPrice && priceSystem.enabled) {
return o.command + `(${findPrice.price} ${points.getPointsName(findPrice.price)})`;
} else {
return o.command;
}
})).sort().join(', ');
case 'cooldown':
listOutput = _.map(cooldowns, function (o, k) {
const time = o.miliseconds;
Expand Down
30 changes: 15 additions & 15 deletions src/bot/systems/points.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,9 @@ class Points extends System {
username,
command: undoOperation.command,
originalValue: undoOperation.originalValue,
originalValuePointsLocale: await this.getPointsName(undoOperation.originalValue),
originalValuePointsLocale: this.getPointsName(undoOperation.originalValue),
updatedValue: undoOperation.updatedValue,
updatedValuePointsLocale: await this.getPointsName(undoOperation.updatedValue),
updatedValuePointsLocale: this.getPointsName(undoOperation.updatedValue),
}), ...opts,
}];
} catch (err) {
Expand Down Expand Up @@ -332,7 +332,7 @@ class Points extends System {
const response = prepare('points.success.set', {
amount: points,
username,
pointsName: await this.getPointsName(points),
pointsName: this.getPointsName(points),
});
return [{ response, ...opts }];
} catch (err) {
Expand Down Expand Up @@ -370,7 +370,7 @@ class Points extends System {
const response = prepare('points.failed.giveNotEnough'.replace('$command', opts.command), {
amount: points,
username,
pointsName: await this.getPointsName(points),
pointsName: this.getPointsName(points),
});
return [{ response, ...opts }];
} else if (points === 'all') {
Expand All @@ -381,7 +381,7 @@ class Points extends System {
const response = prepare('points.success.give', {
amount: availablePoints,
username,
pointsName: await this.getPointsName(availablePoints),
pointsName: this.getPointsName(availablePoints),
});
return [{ response, ...opts }];
} else {
Expand All @@ -392,7 +392,7 @@ class Points extends System {
const response = prepare('points.success.give', {
amount: points,
username,
pointsName: await this.getPointsName(points),
pointsName: this.getPointsName(points),
});
return [{ response, ...opts }];
}
Expand All @@ -401,7 +401,7 @@ class Points extends System {
}
}

async getPointsName (points): Promise<string> {
getPointsName (points): string {
const pointsNames = this.name.split('|').map(Function.prototype.call, String.prototype.trim);
let single, multi, xmulti;
// get single|x:multi|multi from pointsName
Expand Down Expand Up @@ -486,7 +486,7 @@ class Points extends System {
const response = prepare('points.defaults.pointsResponse', {
amount: this.maxSafeInteger(user.points),
username: username,
pointsName: await this.getPointsName(this.maxSafeInteger(user.points)),
pointsName: this.getPointsName(this.maxSafeInteger(user.points)),
order, count,
});
return [{ response, ...opts }];
Expand All @@ -506,14 +506,14 @@ class Points extends System {
await getRepository(User).increment({}, 'points', points);
response = prepare('points.success.online.positive', {
amount: points,
pointsName: await this.getPointsName(points),
pointsName: this.getPointsName(points),
});
} else {
points = Math.abs(points);
await this.decrement({}, points);
response = prepare('points.success.online.negative', {
amount: -points,
pointsName: await this.getPointsName(points),
pointsName: this.getPointsName(points),
});
};

Expand All @@ -533,14 +533,14 @@ class Points extends System {
await getRepository(User).increment({}, 'points', points);
response = prepare('points.success.all.positive', {
amount: points,
pointsName: await this.getPointsName(points),
pointsName: this.getPointsName(points),
});
} else {
points = Math.abs(points);
await this.decrement({}, points);
response = prepare('points.success.all.negative', {
amount: -points,
pointsName: await this.getPointsName(points),
pointsName: this.getPointsName(points),
});
};

Expand All @@ -565,7 +565,7 @@ class Points extends System {
}
const response = prepare('points.success.rain', {
amount: points,
pointsName: await this.getPointsName(points),
pointsName: this.getPointsName(points),
});
return [{ response, ...opts }];
} catch (err) {
Expand Down Expand Up @@ -602,7 +602,7 @@ class Points extends System {
const response = prepare('points.success.add', {
amount: points,
username: username,
pointsName: await this.getPointsName(points),
pointsName: this.getPointsName(points),
});
return [{ response, ...opts }];
} catch (err) {
Expand Down Expand Up @@ -642,7 +642,7 @@ class Points extends System {
const response = prepare('points.success.remove', {
amount: points,
username: username,
pointsName: await this.getPointsName(points === 'all' ? 0 : points),
pointsName: this.getPointsName(points === 'all' ? 0 : points),
});
return [{ response, ...opts }];
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/* global describe it */
require('../../general.js');

const db = require('../../general.js').db;
const message = require('../../general.js').message;
const user = require('../../general.js').user;

const assert = require('assert');

const Message = require('../../../dest/message').default;
const alias = (require('../../../dest/systems/alias')).default;
const customcommands = (require('../../../dest/systems/customcommands')).default;
const price = (require('../../../dest/systems/price')).default;
const owner = { username: 'soge__', userId: Math.floor(Math.random() * 100000) };

const { getRepository } = require('typeorm');
const { Price } = require('../../../dest/database/entity/price');

describe('Message - #3726 - price should be shown alongside alias and command list', () => {
before(async () => {
await db.cleanup();
await message.prepare();
await user.prepare();
await getRepository(Price).save({ command: '!a', price: 10 });
});
after(() => {
price.enabled = true;
});

describe('(list.alias) should return proper message with prices', () => {
it('enable price system', async () => {
price.enabled = true;
});

for (const aliasToCreate of ['!a', '!b', '!c']) {
it('Add alias ' + aliasToCreate, async () => {
const r = await alias.add({ sender: owner, parameters: `-a ${aliasToCreate} -c !me` });
assert.strictEqual(r[0].response, `$sender, alias ${aliasToCreate} for !me was added`);
});
}

it('(list.alias) should return created aliases with price', async () => {
const r = await new Message('(list.alias)').parse({});
assert.strictEqual(r, 'a(10 points), b, c');
});

it('(list.!alias) should return created aliases with price', async () => {
const r = await new Message('(list.!alias)').parse({});
assert.strictEqual(r, '!a(10 points), !b, !c');
});

it('disable price system', async () => {
price.enabled = false;
});

it('(list.alias) should return created aliases without price', async () => {
const r = await new Message('(list.alias)').parse({});
assert.strictEqual(r, 'a, b, c');
});

it('(list.!alias) should return created aliases without price', async () => {
const r = await new Message('(list.!alias)').parse({});
assert.strictEqual(r, '!a, !b, !c');
});
});

describe('(list.command) should return proper message with prices', () => {
it('enable price system', async () => {
price.enabled = true;
});

for (const command of ['!a', '!b', '!c']) {
it('Add command ' + command, async () => {
const r = await customcommands.add({ sender: owner, parameters: `-c ${command} -r Lorem Ipsum` });
assert.strictEqual(r[0].response, `$sender, command ${command} was added`);
});
}

it('(list.command) should return created commands with price', async () => {
const r = await new Message('(list.command)').parse({});
assert.strictEqual(r, 'a(10 points), b, c');
});

it('(list.!command) should return created commands with price', async () => {
const r = await new Message('(list.!command)').parse({});
assert.strictEqual(r, '!a(10 points), !b, !c');
});

it('disable price system', async () => {
price.enabled = false;
});

it('(list.command) should return created commands without price', async () => {
const r = await new Message('(list.command)').parse({});
assert.strictEqual(r, 'a, b, c');
});

it('(list.!command) should return created commands without price', async () => {
const r = await new Message('(list.!command)').parse({});
assert.strictEqual(r, '!a, !b, !c');
});
});
});
1 change: 0 additions & 1 deletion test/tests/message/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const db = require('../../general.js').db;
const msg = require('../../general.js').message;
const Message = require('../../../dest/message').default;
const assert = require('assert');
const sinon = require('sinon');

describe('Message - api filter', () => {
beforeEach(async () => {
Expand Down

0 comments on commit a6b59d7

Please sign in to comment.