【问题标题】:How do I search a Discord user input for a value and assign it to a var?如何在 Discord 用户输入中搜索值并将其分配给 var?
【发布时间】:2021-03-25 03:17:25
【问题描述】:

我正在尝试在 Discord 用户输入中搜索附加到属性(攻击次数、攻击技能、强度、韧性、保存)的整数,然后通过一些计算运行这些参数。

目前该函数使用命令中args的顺序:

!赔率 5 3 4 5 3

这行得通,但我想要这样的东西:

!赔率: 5 sk: 3 st: 4 t: 5 sv: 3

机器人会在用户输入中以任意顺序搜索附加到这些属性的值,并使用这些整数进行计算。

此外,如果属性不存在,机器人将不会将它们包含在输出中(或分配默认值),并通过添加不附加整数的附加单短语属性(例如 fnpres) 然后它会添加另一个标准计算和输出。

这是我目前所拥有的:

const { Message } = require('discord.js')
module.exports = {
    name: 'odds',
    aliases: ['stats', 'probability'],
    usage: '[#attacks (1 = auto hit)] [skill] [strength] [toughness] [save (7 = no save)]',
    description: 'What are the odds?',

    execute(message, args) {
        let attack = args[0]
        let skill;
        if (args[1]>=7) skill = 7;
        else skill = args[1];
        let strength = args[2]
        let toughness = args[3]
        let save;
        if (args[4]<=6) save = args[4];
            else save = 7;
        let hits = parseFloat((attack*((7-skill)/6)).toFixed(2))
        let hitSixes = parseFloat((attack*(1/6)).toFixed(2))
        let woundFactor; 
         if (strength>=(2*toughness)) woundFactor = 2;
        else 
        if (strength>toughness) woundFactor = 3;
        else
        if (strength==toughness) woundFactor = 4;
        else
        if (toughness>=(2*strength)) woundFactor = 6;
        else
        if (strength<toughness) woundFactor = 5;

        let wounds = parseFloat((hits*((7-woundFactor)/6)).toFixed(2))
        let woundSixes = parseFloat((hits*(1/6)).toFixed(2))
        let unsavedWounds = parseFloat(((wounds-(wounds*((7-save)/6))).toFixed(2)))

    message.channel.send(`On average you will make:\n${hits} successful hits (with ${hitSixes} sixes)\n${wounds} successful wounds (with ${woundSixes} sixes)\nGiving a total of ${unsavedWounds} unsaved wounds.`)
    }}

非常感谢任何和所有帮助!

【问题讨论】:

    标签: javascript arguments discord discord.js


    【解决方案1】:
    // what you accept:
    const inputs = {
        at: 0,
        sk: 0,
        st: 0,
        t: 0,
        sv: 0,
    }
    
    for (let i = 0, arg; arg = args[i]; i++) {
        const index = arg.indexOf(':'); // it must have an ":" as last character
        if (index === -1) {
            continue;
        }
        const keyword = arg.substr(0, index - 1); // get the word
        if (!inputs.hasOwnProperty(keyword)) { // check if it's an element of inputs
            continue;
        }
        if (i + 1 === args.length) { // make sure enough args are available
            continue;
        }
        inputs[keyword] = isNaN(args[i + 1]) ? 0 : +args[i + 1]; // make sure it's a number
        i++; // skip the next element
    }
    
    let attack = inputs.at;
    let skill = inputs.sk >= 7 ? 7 : inputs.sk;
    let strength = inputs.st;
    let toughness = inputs.t;
    let save = inputs.sv <= 6 ? inputs.sv : 7;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多