1 回答

TA贡献1848条经验 获得超10个赞
在 Javascript 中,变量仅在脚本运行时存在,然后被删除,直到您再次运行脚本。在脚本未运行时存储值的一种方法是使用.json文件。在您的应用程序或 index.js 文件所在的目录中,您可以添加一个status.json文件。这是它如何工作的一个例子。
const fs = require("fs")
var status = require("./status.json")
client.on('ready', () => {
client.user.setActivity(status.status, { //write msg here
type: "WATCHING", //LISTENING or PLAYING
name: "itt"
});
})
client.on('message', message => {
if (message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(' ');
const command = args.shift().toLowerCase();
const text = args.join(' ');
if (command === 'status') {
if (!args.length) {
return message.channel.send(`Please tell the bot what to say, ${message.author}`);
}
client.user.setActivity(text, { //write msg here
type: "WATCHING", //LISTENING or PLAYING
name: "itt"
});
message.channel.send('Changed status to ' + text)
status.status = text
fs.writeFile("./status.json", JSON.stringify(status, null, 4), "utf8", err => {
if (err) throw err
})
}
});
status.json:
{
"status": "bot status"
}
添加回答
举报