重新启动后设置状态命令重置(discord.js)

我有这段代码可以在您键入时更改我的机器人的状态!status <status>并且它工作正常,但是当我重新启动我的机器人时,状态重置为我在开始时定义的任何内容,有什么办法可以解决这个问题吗?


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)

    }

});


慕雪6442864
浏览 103回答 1
1回答

慕桂英546537

在 Javascript 中,变量仅在脚本运行时存在,然后被删除,直到您再次运行脚本。在脚本未运行时存储值的一种方法是使用.json文件。在您的应用程序或 index.js 文件所在的目录中,您可以添加一个status.json文件。这是它如何工作的一个例子。const fs = require("fs")var status = require("./status.json")client.on('ready', () => {&nbsp; &nbsp; client.user.setActivity(status.status, { //write msg here&nbsp; &nbsp; &nbsp; &nbsp; type: "WATCHING", //LISTENING or PLAYING&nbsp; &nbsp; &nbsp; &nbsp; name: "itt"&nbsp; &nbsp; });})client.on('message', message => {&nbsp; &nbsp; if (message.author.bot) return;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; const args = message.content.slice(prefix.length).trim().split(' ');&nbsp; &nbsp; const command = args.shift().toLowerCase();&nbsp; &nbsp; const text = args.join(' ');&nbsp; &nbsp; if (command === 'status') {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (!args.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return message.channel.send(`Please tell the bot what to say, ${message.author}`);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; client.user.setActivity(text, { //write msg here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: "WATCHING", //LISTENING or PLAYING&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: "itt"&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; message.channel.send('Changed status to ' + text)&nbsp; &nbsp; &nbsp; &nbsp; status.status = text&nbsp; &nbsp; &nbsp; &nbsp; fs.writeFile("./status.json", JSON.stringify(status, null, 4), "utf8", err => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (err) throw err&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }});status.json:{&nbsp; &nbsp; &nbsp;"status": "bot status"}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript