diff --git a/commands/Attribute.js b/commands/Attribute.js index 2cc433c..295363f 100644 --- a/commands/Attribute.js +++ b/commands/Attribute.js @@ -1,10 +1,6 @@ const globals = require('../globals'); -const { - CountOccurences -} = require('@dsabot/CountOccurences'); -const { - findMessage -} = require('@dsabot/findMessage'); +const { CountOccurences } = require('@dsabot/CountOccurences'); +const { findMessage } = require('@dsabot/findMessage'); const Random = require('random'); const db = globals.db; @@ -16,18 +12,23 @@ module.exports = { needs_args: true, async exec(message, args) { try { - let level = 8; - let attributename; - + let Attribute; + let AttributeName; + let Level = 8; await db.find({ user: message.author.tag, - }, async function (err, docs) { + }, async (err, docs) => { - // user calls with text. need to gather info from database + // user calls with text, let's look him up in the database. if (isNaN(args[0])) { - HandleNamedAttributes(); + Attribute = HandleNamedAttributes({ + Character: docs[0].character, + args: args + }); + AttributeName = Attribute.Name; + Level = Attribute.Level; } else { - level = args[0]; + Level = args[0]; } Random.use(message.author.tag); @@ -44,19 +45,19 @@ module.exports = { message.reply('Du hast einen Patzer (' + dice.join(', ') + ')! 😭 Viel Erfolg beim nÀchsten mal!'); return; } - if ((dice.length == 2 && dice[0] != 20 && dice[1] <= level) || (dice.length == 1 && dice[0] <= level)) { - if (attributename) { - message.reply('Du hast die Probe auf ' + attributename + ' (Stufe ' + level + ') bestanden.\n' + + if ((dice.length == 2 && dice[0] != 20 && dice[1] <= Level) || (dice.length == 1 && dice[0] <= Level)) { + if (AttributeName) { + message.reply('Du hast die Probe auf ' + AttributeName + ' (Stufe ' + Level + ') bestanden.\n' + 'Deine 🎲: ' + dice.join(', ')); } else { - message.reply('Du hast die Probe (Stufe ' + level + ') bestanden.\n' + + message.reply('Du hast die Probe (Stufe ' + Level + ') bestanden.\n' + 'Deine 🎲: ' + dice.join(', ')); } - } else if (attributename) { - message.reply('Du hast die Probe auf ' + attributename + ' (Stufe ' + level + ') leider nicht bestanden 😒.\n' + + } else if (AttributeName) { + message.reply('Du hast die Probe auf ' + AttributeName + ' (Stufe ' + Level + ') leider nicht bestanden 😒.\n' + 'Deine 🎲: ' + dice.join(', ')); } else { - message.reply('Du hast die Probe (Stufe ' + level + ') leider nicht bestanden 😒.\n' + + message.reply('Du hast die Probe (Stufe ' + Level + ') leider nicht bestanden 😒.\n' + 'Deine 🎲: ' + dice.join(', ')); } }); @@ -70,25 +71,26 @@ const HandleCrits = (dice) => { }; -const HandleNamedAttributes = () => { - if (docs.length === 0) { - message.reply(findMessage('NOENTRY')); - return; +const HandleNamedAttributes = ({Character: Character = [], args: args = []} = {}) => { + + let Attributes = globals.Werte; + let Level = 8; // This is the minimum attributes value. + let AttributeName; + let AttributeId; + + if (args[0].length == 2) { + AttributeId = Attributes.find(attribute => attribute.kuerzel === args[0].toUpperCase()).id; } else { - // try to get id of short formatted attributes. - if (args[0].length == 2) { - for (const i in globals.Werte) { - if (globals.Werte[i].kuerzel == args[0].toUpperCase()) { - attributename = globals.Werte[i].id; - } - } - } else { - attributename = args[0].toLowerCase(); - } - for (const i in docs[0].character.attributes) { - if (docs[0].character.attributes[i].id == attributename) level = docs[0].character.attributes[i].level; - } + AttributeId = args[0].toLowerCase() || + Attributes.find(attribute => attribute.name.toLowerCase() === args[0].toLowerCase()).id; } -}; -//const docs = []; -//console.log(HandleNamedAttributes()) \ No newline at end of file + + Level = Character.attributes.find(attribute => attribute.id === AttributeId).level; + AttributeName = Attributes.find(attribute => attribute.id === AttributeId).name; + + return { + Name: AttributeName, + Level: Level + }; + +}; \ No newline at end of file