установить сброс команды статуса после перезапуска (discord. js) - PullRequest
0 голосов
/ 05 августа 2020

У меня есть этот код, который меняет статус моего бота, когда вы набираете !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 Ответ

0 голосов
/ 05 августа 2020

В 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"
}
...