[英]How do I search a Discord user input for a value and assign it to a var?
我正在嘗試搜索 Discord 用戶輸入附加到屬性(攻擊次數、攻擊技能、強度、韌性、保存)的整數,然后通過一些計算運行這些參數。
目前 function 在命令中使用 args 的順序:
!賠率 5 3 4 5 3
哪個有效,但我想要這樣的東西:
:賠率: 5 sk: 3 st: 4 t: 5 sv: 3
機器人將以任何順序在用戶輸入中搜索附加到這些屬性的值,並使用這些整數進行計算。
此外,如果屬性不存在,機器人將不會將它們包含在 output (或分配默認值)中,並通過添加沒有附加 integer 的附加單短語屬性(例如fnp或res )然后添加另一個標准計算和 output。
這是我到目前為止所擁有的:
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.`)
}}
非常感謝任何和所有幫助!
// 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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.