나는 불화 봇을 만들고 있으며 다음과 같이 config.json이라는 구성 파일이 있습니다.
{
"token":"TokenPlaceholder",
"prefix":"a.",
"devPrefix":"+."
}
내 기본 index.js 파일에는 다른 명령 파일에서 동적으로 접두사 유형을 가져 오는 다음 코드가 있습니다.
const commandPrefixType = botClient.commands.get(commandName).prefixType;
다음과 같이 config.json이 필요합니다. const CONFIG = require('./config.json');
상수의 값은 commandPrefixType
어느 것 prefix
또는 devPrefix
. 이 상수를 사용하여 config.json에서 해당 prefixType의 값을 동적으로 가져올 수 있습니까?
예를 들어 명령의 prefixType 값이 'devPrefix'인 경우 '+'값을 어떻게 얻을 수 있습니까? if / else 또는 switch 블록을 사용하지 않고? (앞으로 더 많은 접두사를 추가 할 계획이므로 동적이기를 원합니다).
감사
문제에 대한 최선의 해결책은 대괄호 ([])를 사용하여 개체 값에 액세스하는 것입니다.
const CONFIG = require('./config.json');
const commandPrefixType = botClient.commands.get(commandName).prefixType;
// Access to the prefix
const commandPrefix = CONFIG[commandPrefixType];
console.log(commandPrefix)
도움이 되었기를 바랍니다!
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다