我是 JavaScript 新手,我正在 Github 页面上为 Discord 机器人运行 wiki,当我尝试使用预制 JSON 文件中的代码更新页面时,它必须运行两次才能正常运行,它目前在这里,我使用控制台运行该功能。当我运行该addCommands();函数时,它返回 undefined 并且一开始什么也不做,但是当我第二次运行它时,它可以工作
命令.js
var commands_json;
function addCommands() {
$(function () {
$.getJSON("./commands.json", function (data) {
commands_json = data.Commands;
});
for (var command in commands_json) {
console.log(command);
command_div = document.getElementById("commands");
command_div.innerHTML +=
"<div class=" +
command +
' id="command"><h1>' +
command +
"</h1><h2>" +
commands_json[command]["desc"] +
"</h2><h3>Known Errors: " +
commands_json[command]["errs"] +
"</h3></div>";
if (commands_json[command].subcommands != undefined) {
for (var subcommand in commands_json[command]["subcommands"]) {
document.getElementsByClassName(command)[0].innerHTML +=
"<h4>" +
subcommand +
"</h4><h5>" +
commands_json[command]["subcommands"][subcommand]["desc"] +
"</h5><h6>Known Errors:" +
commands_json[command]["subcommands"][subcommand]["errs"] +
"</h6>";
}
}
}
});
}
命令.json
{
"Commands": {
"$decrypt": {
"desc": "Decrypts a given message with the given key",
"errs": "There is no error handling to stop it from creating an error if the key fails to decrypt the message"
},
"$encrypt": {
"desc": "Encrypts a message with the given key",
"errs": "None known"
},
"$generatekey": {
"desc": "Generates an encryption key for encrypting messages",
"errs": "None known"
},
"$eval": {
"desc": "Evaluates a given expression",
"errs": "Very strict notation, doesn't accept most symbols such as 2^2 not resulting in 4, while 2**2 results in 4"
},
"$meme": {
"desc": "Using the PRAW library, this command receives the newest post from r/memes",
"errs": "If the poster deletes their post, only the image is deleted, and the post still remains, therefore it returns an empty embed"
},
"$dankmeme": {
"desc": "Using the PRAW library, this command receives the newest post from r/memes",
"errs": "Same error as the $memes command"
},
"$inventory": {
"desc": "Opens the user data JSON file and creates an embedding of the items in the user's inventory",
"errs": "None known"
},
"$reset": {
"desc": "Completely erases the user's account from the user data JSON file",
"errs": "None known"
},
"$shutdown": {
"desc": "Intended for shutting down the bot while notifying people who are on the \"notifiy\" list",
"errs": "None known"
},
"$ping": {
"desc": "Returns the bot's responsiveness in seconds, aka ping",
"errs": "None known"
},
"$twitter": {
"desc": "Part of a list of sub commands that receives tweets from Twitter using Tweepy",
"errs": "None Known",
"subcommands": {
"$twitter trending": {
"desc": "Takes the WOE (Where On Earth) ID and produces a list of tweets that are trending in that area",
"errs": "No exception handling for when the WOE ID is incorrect"
},
"$twitter user": {
"desc": "Takes a Twitter username and gets tweets from that person's account",
"errs": "None known"
},
"$twitter hashtags": {
"desc": "Takes a hashtag and searches any usages of the hashtag",
"errs": "None known"
}
}
},
"$reddit": {
"desc": "Part of a list of sub commands that receives a list of posts from Reddit from the subreddit using PRAW",
"errs": "None known",
"subcommands": {
"$reddit subreddit": {
"desc": "Takes posts from a subreddit using PRAW",
"errs": "None known"
}
}
},
"$pypi": {
"desc": "Scrapes https://www.pypi.org/ for a packages info",
"errs": "None known"
},
"$status": {
"desc": "Shows the statuses of the bot and the Discord API",
"errs": "None known"
}
}
}