为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕雪6442864 2022-12-22 09:04:40
我有这段代码可以在您键入时更改我的机器人的状态!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)    }});
查看完整描述

1 回答

?
慕桂英546537

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"

}


查看完整回答
反对 回复 2022-12-22
  • 1 回答
  • 0 关注
  • 76 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号