如何遍历整个脚本来捕获所有全局变量的列表。
我知道_G是环境表,它包含所有全局变量,但实际上包含所有全局变量。我只想要脚本中的全局变量
答案 0 :(得分:2)
module.exports.run = async (bot, message, args) => {
console.log(`${message.member} ran the "GiveAway" command`);
var item = "";
var time;
var winnerCount
let messageArray = message.content.split(" ");
//-- f!giveaway 1(winnerCount) 60(seconds) Item Giveaway
for (var i = 3; i < messageArray.length; i++){
item += (messageArray[i] + " ");
}
winnerCount = Number(messageArray[1]);
time = Number(messageArray[2]);
var giveEmbed = new Discord.RichEmbed();
giveEmbed.setDescription(item);
var embedSent = await message.channel.send(giveEmbed);
embedSent.react("");
setTimeout(function() {
var peopleReacted = embedSent.reactions.get("").users;
var index = Math.floor(Math.random() * peopleReacted.length);
var winners = [];
var winnerMsg = "";
for (var i = 0; i < winners.length; i++){
winnerMsg += (winners[i].toString() + " ");
}
var haveHas = "has";
if (winners.length == 1){
haveHas = "has";
}
else{
haveHas = "have";
}
message.channel.send(winnerMsg + " " + haveHas + ` won ${item}`);
}, time * 1000); //--conversting seconds into miliseconds
}
本身不是全局变量,_G
是运行时表(因此只能在特定运行期间的某个时刻列出)。没错,它将列出由脚本以及Lua状态初始化和使用的其他脚本设置的全局变量。
找到编译器将作为全局变量绑定的变量标识符的一种方法是使用_G
命令行工具:
luac
第二列给出行号。最后一列(注释)提供了标识符。