亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

重新啟動后設置狀態命令重置(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 關注
  • 94 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號