Home > Mobile >  Discord Bot is auto stopping
Discord Bot is auto stopping

Time:06-02

My discord bot is auto stopping for some reason. It turns off after around an hour of inactivity. I don't know what code to send for that so if you need code to answer the question tell me and I will post which files you need. Thanks Here is the code, For some reason I have to add more words to post this. bot.js

var Discord = require('discord.io');

var logger = require('winston');

var auth = require('./auth.json');

var fs = require('fs')


// Configure logger settings

logger.remove(logger.transports.Console);

logger.add(new logger.transports.Console, {

colorize: true
});

logger.level = 'debug';

// Initialize Discord Bot

var bot = new Discord.Client({

token: auth.token,

autorun: true

});

bot.on('ready', function (evt) {

logger.info('Connected');

logger.info('Logged in as: ');

logger.info(bot.username   ' - ('   bot.id   ')');
});

var cmd
let ndata = fs.readFileSync('./lore.json');
var data = require('./lore.json');
const { threadId } = require('worker_threads');
const { syncBuiltinESMExports } = require('module');
let oldCmd, place
const d = new Date()
bot.on('message', function (user, userID, channelID, message, guild, evt,) {
    var args = message.substring(0).split('poiafhdkadhf');
    fs.writeFile('./lore.json', JSON.stringify(data, null, 2), function writeJSON(err) {})
    cmd = args[0]
    args = args.splice(0);
    oldCmd = cmd
    cmd = cmd.toLowerCase()
    place = 5
    ndata = fs.readFileSync('./lore.json');
if(oldCmd.substring(0, 5) == "sage:"){
if(attribute() == "sage:age"){
    editUser(userID, {"years": oldCmd.slice(place   5)}, channelID = channelID)
    editUser(userID, {"years": oldCmd.slice(place   5)})
    
}
if(attribute() == "name"){
    editUser(userID, {"name": oldCmd.slice(place   4)}, channelID = channelID)
    editUser(userID, {"name": oldCmd.slice(place   4)})
}
if(attribute() == "gender"){
    editUser(userID, {"gender": oldCmd.slice(place   6)}, channelID = channelID)
    editUser(userID, {"gender": oldCmd.slice(place   6)})
}
if(attribute() == "pronouns"){
    editUser(userID, {"pronouns": oldCmd.slice(place   8)}, channelID = channelID)
    editUser(userID, {"pronouns": oldCmd.slice(place   8)})
}
if(attribute() == "personality"){
    editUser(userID, {"personality": oldCmd.slice(place   11)}, channelID = channelID)
    editUser(userID, {"personality": oldCmd.slice(place   11)})
}
if(attribute() == "race"){
    editUser(userID, {"race": oldCmd.slice(place   4)}, channelID = channelID)
    editUser(userID, {"race": oldCmd.slice(place   4)})
}
if(attribute() == "family"){
    editUser(userID, {"family": oldCmd.slice(place   6)}, channelID = channelID)
    editUser(userID, {"family": oldCmd.slice(place   6)})
}
if(attribute() == "extra"){
    editUser(userID, {"extra": oldCmd.slice(place   5)}, channelID = channelID)
    editUser(userID, {"extra": oldCmd.slice(place   5)})
}
if(attribute() == "get"){
    if(oldCmd.substring(9).trim() == ''){
        var gotten = get(userID)
        if(gotten != null){
            bot.sendMessage({
                to: channelID,
                message: "name: "   gotten.name   "\nage: "   gotten.years   "\ngender: "    gotten.gender   "\npronouns: "   gotten.pronouns   "\npersonality: "   gotten.personality   "\nrace: "   gotten.race   "\nfamily: "   gotten.family   "\nextra: "   gotten.extra
            })
        }
    } else {
        var gotten = get(oldCmd.substring(9).trim().replace("<", '').replace("@", '').replace(">", ''))
        bot.sendMessage({
            to: channelID,
            message: "name: "   gotten.name   "\nage: "   gotten.years   "\ngender: "    gotten.gender   "\npronouns: "   gotten.pronouns   "\npersonality: "   gotten.personality   "\nrace: "   gotten.race   "\nfamily: "   gotten.family   "\nextra: "   gotten.extra
        })
    }
}
if(attribute() == "help"){
    bot.sendMessage({
        to: channelID,
        message: "Do Sage:name followed by your name, Sage:gender for gender, Sage:pronouns for pronouns, Sage:personality for personality, Sage:race for race, Sage:family for family, Sage:extra for extra, and Sage:age for your age\n\nUse Sage:get followed by a ping of whoever's lore you want to get\n\nUse Sage:help for these instructions"
    })
}
}
});

function editUser(identity, changes, channelID = null){
    data = require('./lore.json');
    console.log("edited")
    var count = 0
    var userE = false
    sleep(50)
    for(i = 0; i < data.length; i  ){
        if(data[i].id == identity){
            if(changes.name != null){
                data[i].name = changes.name
            }
            if(changes.years != null){
                data[i].years = changes.years
            } 
            if(changes.gender != null){
                data[i].gender = changes.gender
            }
            if(changes.pronouns != null){
                data[i].pronouns = changes.pronouns
            }
            if(changes.personality != null){
                data[i].personality = changes.personality
            }
            if(changes.race != null){
                data[i].race = changes.race
            }
            if(changes.family != null){
                data[i].family = changes.family
            }
            if(changes.extra != null){
                data[i].extra = changes.extra
            }
            userE = true
        }
    }
    if(!userE){
        data.push({id: String(identity),name: " ", years:" ", gender:" ", pronouns:" ", personality:" ", race:" ",family:" ", extra:" "})
    }
    fs.writeFile('./lore.json', JSON.stringify(data, null, 2), function writeJSON(err) {})
    if(channelID != null){
    bot.sendMessage({
        to: channelID,
        message: "Saved"
    })
}
}
function attribute(){
    const attributes = ["name", "sage:age", "gender", "pronouns", "personality", "race", "family", "relationship", "extra", "get", "help"]
    for(let x in attributes){
        if(cmd.indexOf(attributes[x]) != -1){
            return attributes[x]
        }
    }
}
function sleep(milliseconds) {
    const date = Date.now();
    let currentDate = null;
    do {
      currentDate = Date.now();
    } while (currentDate - date < milliseconds);
  }

function get(identity){
    for(i = 0; i < data.length; i  ){
        if(data[i].id == identity){
            return data[i]
        }
    }
    return null
}

package.json

{"name":"Sage Lore","version":"1.0.0","description":"My First Discord Bot","main":"bot.js","author":"Your Name","dependencies":{"@discordjs/rest":"^0.4.1","discord-api-types":"^0.33.1","discord.io":"https://github.com/woor/discord.io/tarball/gateway_v6","winston":"^3.7.2"}}

auth.json

{

"token": not putting my token

}

CodePudding user response:

Your hosting does not provide 24/7 hosting

The issue must be coming from your hosting provider. From what you've stated, it sounds like you're running your code on Replit. This service can host projects 24/7 but does not do this for free. This is because after an hour of inactivity, the repl will automatically enter a sleeping state. The documentation states the following:

Once deployed, the server will continue to run in the background, even after you close the browser tab. The server will stay awake and active until an hour after its last request, after which it will enter a sleeping stage.

This sleeping state will result in the repl shutting down. This also means that it will stop sending data to Discord, which in turn, stops the bot from running.

Always-on repls

To avoid an issue like this, you must purchase a Hacker plan from them. As stated in the documentation:

Repls typically go to sleep after a period of inactivity. To make sure that your repl is restarted, you can use our Always-on functionality.

Note that this function is currently only available on our Hacker plan.

Therefore, unless you have the plan, Always-on is not available.

CodePudding user response:

Turns out it's because command prompt auto closes, to prevent it you can fix it by opening command prompt with windows r and then typing cmd /K

  • Related