Tweak: Add --all to !List command (#39)

* tweaked on list command

* added more tests
This commit is contained in:
2021-05-03 21:44:05 +02:00
committed by GitHub
parent 9bcd195afd
commit 895f47847e
2 changed files with 64 additions and 26 deletions

View File

@ -0,0 +1,12 @@
require('module-alias/register');
const { isString } = require('@dsabot/isString');
it('should return true statements', () => {
expect(isString({})).toBeFalsy();
expect(isString()).toBeFalsy();
expect(isString('')).toBeTruthy();
expect(isString([])).toBeFalsy();
expect(isString({ key: 'value' })).toBeFalsy();
expect(isString([null])).toBeFalsy();
expect(isString([''])).toBeFalsy();
});

View File

@ -4,19 +4,8 @@ const { isEmpty } = require('@dsabot/isEmpty');
const { db } = require('../globals'); const { db } = require('../globals');
const { Werte } = require('../globals'); const { Werte } = require('../globals');
async function findUser(request = '') { function printHeader(attributes) {
return db return `${''.padStart(31)}${attributes
.findOne({
$or: [
{ user: request.replace('@', '') },
{ uid: request.replaceAll(/[<>!@]/gi, '') },
{ character: { name: request } },
],
})
.then(doc => doc);
}
function doHeading(attributes) {
return `${''.padStart(25)}${attributes
.map(a => `${a.Short}`.padEnd(4).padStart(6)) .map(a => `${a.Short}`.padEnd(4).padStart(6))
.join('|')}\n`; .join('|')}\n`;
} }
@ -38,20 +27,64 @@ function getStats(user) {
user.character.attributes.forEach(attribute => { user.character.attributes.forEach(attribute => {
Attributes.push(getAttribute(attribute)); Attributes.push(getAttribute(attribute));
}); });
Attributes.sort((a, b) => (a.id > b.id ? 1 : a.id < b.id ? -1 : 0)); Attributes.sort((a, b) => (a.id > b.id ? 1 : -1));
return Attributes; return Attributes;
} }
function returnResult(message, Characters) {
if (isEmpty(Characters)) return message.reply(findMessage('NO_CHARACTERS'));
Characters.sort((a, b) => (a.Name > b.Name ? 1 : -1));
let Reply = `\`\`\`\n${printHeader(Characters[0].Attributes)}`;
Characters.forEach(c => {
Reply += `${c.Name.toString().padEnd(30)} ${listStats(c.Attributes)}`;
});
Reply += `\`\`\``;
return message.reply(Reply);
}
async function findUser(request = '') {
return db
.findOne({
$or: [
{ user: request.replace('@', '') },
{ uid: request.replaceAll(/[<>!@]/gi, '') },
{ character: { name: request } },
],
})
.then(doc => doc);
}
async function findUsers(message) {
const Characters = [];
db.find({})
.limit(10)
.then(users => {
users.forEach(user => {
Characters.push({
Name: user.character.name,
Attributes: getStats(user),
});
});
})
.then(() => returnResult(message, Characters));
}
module.exports = { module.exports = {
name: 'list', name: 'list',
description: 'Gibt eine Liste von Mitspielern aus.', description: 'Gibt eine Liste von Mitspielern aus.',
aliases: ['liste'], aliases: ['liste'],
usage: '', usage: '[@Mention / Benutzername]',
needs_args: false, needs_args: false,
exec: async (message, args) => { exec: async (message, args) => {
if (!args) return; if (!args) return null;
console.log(args[0].replaceAll(/[<>!@]/gi, '')); if (args[0] === '--all') {
return findUsers(message);
}
const Characters = []; //?+ const Characters = []; //?+
Promise.all( Promise.all(
args.map(arg => { args.map(arg => {
@ -64,15 +97,8 @@ module.exports = {
} }
}); });
}) })
).then(() => { ).then(() => returnResult(message, Characters));
if (isEmpty(Characters)) return findMessage('NO_CHARACTERS'); return null;
let Reply = `\`\`\`\n${doHeading(Characters[0].Attributes)}`;
Characters.forEach(c => {
Reply += `${c.Name.toString().padEnd(24)} ${listStats(c.Attributes)}`;
});
Reply += `\`\`\``;
return message.reply(Reply);
});
}, },
}; };
/* /*