He tenido este problema con mi bot de Discord durante un tiempo en el que simplemente se desconecta a pesar de que el proceso aún se está ejecutando.
Intenté usar nohup node index.js & luego disown -a y finalmente exit , pero el bot aún muere.
Tenga en cuenta que no muere INSTANTÁNEAMENTE, sino entre 30 minutos y una hora después de que cierro mi terminal, y no hay ningún mensaje de error en nohup.out . Pero cuando uso top , el proceso sigue ejecutándose.
Estoy en Ubuntu-20.04 usando Google Cloud Service VM, discord.js 13.6.0, Node.js v17.9.0
Y mi código se ve así:
index.js
const discord = require(“discord.js”); const allIntents = new discord.Intents(32767); const client = new discord.Client({intents: [allIntents]}); const sleep = (ms) => new Promise(r => setTimeout(r, ms)); require(“dotenv”).config(); client.on(“ready”, async () => { console.log(`Logged in as ${client.user.tag}!`); client.user.setStatus(“idle”); createSlashCommands(client); var stats = [“hi”, “hello”]; const length = stats.length; for (let i = 0, true, i = (i+1)%length) { client.user.setActivity(stats[i], {type: 3}); await sleep(10000); } }); message.on(“messageCreate”, async (message) => { if (client.isReady() && !message.author.bot) { if (message.content === “t!ping”) { message.reply({content: `Pong! ${Date.now() - message.createdTimestamp}ms`}); } } }); client.on(“interactionCreate”, async (interaction) => { if (client.isReady() && !message.author.bot) { if (interaction.commandName === “ping”) { interaction.reply({content: `Pong! ${Date.now() - interaction.createdTimestamp}ms`}); } } }); client.login(process.env.mybot);¿Hay alguna otra manera de evitar que mi bot se desconecte? ¿O hay algo que hice mal?
La mejor solución que he encontrado y uso (y es gratis) es pm2
npm i pm2 -g // navigate to the folder with your main bot.js file (in this example it is called index.js) pm2 start index.js // followed by pm2 save // Optionally you can monitor the whole server as well with pm2-server-monitViene con un tablero gratis también
En cuanto a su código, haría un par de cambios
// change: client.on(“ready”, async () => { // to: client.once("ready", async () => {Además, estos nunca serán realmente necesarios porque si el cliente no está listo, no capturará el evento.
rasca esto por todas partes
client.isReady() && // Lastly, this needs to be client.on("messageCreate" // rather than message.on("messageCreate"EDITAR: no deje la depuración activa en el código todo el tiempo a menos que solo lo desee. Coloque esto en su código como se muestra
// prev code require(“dotenv”).config(); // Error Logging client.on('error', (e) => { console.log(e) }) client.on('warn', (e) => { console.log(e) }) // comment the below one out when not using it to debug client.on('debug', (e) => { console.log(e) }) process.on('unhandledRejection', (error) => { console.log('Unhandled promise rejection:', error) }) client.once(“ready”, async () => { // code following