JavaScript discord bot keeps going offline with keep alive - javascript

I have a Javascript discord bot, and it is getting bigger and bigger, the problem is that every time I close my replit. In about 30 minutes my bot just goes offline.
I use this code in my keep_alive.js file
http.createServer(function (req, res) {
res.write("Online:)");
res.end();
}).listen(8080);```
//I have this in index.js: `const keep_alive = require('./keep_alive.js')`

The best solution that I have found and use (and it is free) is pm2. It requires no code in your file at all
https://pm2.keymetrics.io/
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-monit
Comes with a free dashboard as well

You could use freshping to keep the bot online if you use replit or something like that. You just need this like website you can monitor.

Related

wont let me run node . in vsc terminal (making discord bot)

So im facing a problem where for me to cd to desktop i have to cd to onedrive first. Also in visual studio code when i try to get my bot online by doing node . nothing happens or no errors.
Well for the onedrive problem i disable it (well i think i did.) Still had to cd into onedrive to cd into desktop. So then i tried running account on local which didnt work also. For node . i tried googling it and watching videos nothing worked.
const Discord = require('discord.js');
const client = new Discord.client();
const token = '(my token)';
bot.on('ready', () =>{
console.log('Bot is Online');
})
bot.login(token);
nothing happen this is what the terminal said when trying to get my discord bot online in vsc.
PS C:\Users\dfkj1\OneDrive\Desktop\Discord Bot> node .
PS C:\Users\dfkj1\OneDrive\Desktop\Discord Bot>
Try to run node with the actual filename instead of just '.'. node filename.js will allow node to know which file you would like to run.
As for your desktop problem, I don't really know where to start on that one. Saying that you have to go through One Drive before Desktop does not make sense. If you have a directory named Desktop inside of OneDrive, then maybe that is the reason. Normal directory structure should be C:\Users\username\Desktop for the desktop folder and C:\Users\username\OneDrive for One Drive. You might want to check that there isn't a Desktop folder inside of the OneDrive folder and that's what you are using.
Taking a closer look at the file you posted, it looks like you import Discord, then create the client variable with new Discord.create() but then never use client. Are the commands with bot in them supposed to be client.on and client.token(token)
Something like this works, just tested it on my machine. Don't have the token so it fails at authentication. You also want to use Client instead of create and capitalize Client in the Discord.Client() line.
const Discord = require('discord.js');
const client = new Discord.Client();
const token = '(my token)';
client.on('ready', () =>{
console.log('Bot is Online');
})
client.login(token);

Route file failing to update in Express.js

I am new to using both Node and Express.
Express.js looked great, so I created a local deployment of a vanilla install.
Visiting http://localhost:3000/users prints the expected message:
respond with a resource
I try changing the text to this instead:
respond with no resource
But the change is not made. This is the contents of users.js, almost exactly the same as default:
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
res.send('respond with no resource');
});
module.exports = router;
And here is a screenshot. Top left is the above code. Bottom left demonstrates a grep for "respond with a resource" failing to produce any results. The right hand side is my browser displaying old data.
What am I doing wrong that the data is not updating?
Your application will not automatically update if you use node <path/to/main>.js to run your application. node will run your application in the form it was when initially started, until that process is terminated.
The most common way to address this is by using a process manager, the most popular one used during development is nodemon. To use nodemon, install it globally on your machine using npm i -g nodemon (you'll probably need admin rights to do this). Then, run your application using nodemon instead of node.
nodemon <path/to/main>.js

MONGO_URL for running multiple Meteor apps on one server

I have one Meteor application running on my Ubuntu server (Digital Ocean). I use Meteor Up (MUP) to deploy and keep the app running. Everything works fine.
However, when I try to deploy a second app on the same server, something goes wrong in connecting to the MongoDB. I get a long and unreadable error message that starts "Invoking deployment process: FAILED" and then ends with
Waiting for MongoDB to initialize. (5 minutes)
connected
myapp start/running, process 25053
Waiting for 15 seconds while app is booting up
Checking is app booted or not?
myapp stop/waiting
myapp start/running, process 25114
And the app refuses to run. I have tried a number of things to fix this and will edit this post if more info is requested, but I'm not sure what's relevant. Essentially I don't understand the Error message, so I need to know what the heck is going on?
EDIT:
I want to add that my app runs fine if I go into the project folder and use the "meteor" command. Everything runs as expected. It is only when I try to deploy it for long-term production mode with MUP that I get this error.
EDIT:
I moved on to trying mupx instead of mup. This time I can't even get past the installation process, I get the following error message:
[Neal] x Installing MongoDB: FAILED
-----------------------------------STDERR-----------------------------------
Error response from daemon: no such id: mongodb
Error: failed to remove containers: [mongodb]
Error response from daemon: Cannot start container c2c538d34c15103d1d07bcc60b56a54bd3d23e50ae7a8e4f9f7831df0d77dc56: failed to create endpoint mongodb on network bridge: Error starting userland proxy: listen tcp 127.0.0.1:27017: bind: address already in use
But I don't understand why! Mongod is clearly already running on port 27017 and a second application should just add a new database to that instance, correct? I don't know what I'm missing here, why MUP can't access MongoDB.
It's tricky without your mup.json to see what's going on here. Given what you said, it looks like your 2nd app deployment tries to override/boot mongodb over the 1st one which is locked, the mongodb environment fails to boot, causing then the fail. You should tackle this different ways:
If your objective is to share your mongoDB, point the MONGO_URL from your 2nd mup.jon on your first mongodb instance. It's generally something along the 2701X ports. As it's a shared DB, changes in one database could affect the other.
meteor-up oversees the deployment of your app from a meteor-nice-to-test thing to a node+mongodb environment. You can spawn another mongod instance with :
mongod --port 2701X --dbpath /your/dbpath --fork --logpath /log/path on your DO server and then point MONGO_URL there.
Last but not least, mupx having docker under the hood. Using mupx for your deployments should isolate both apps from each other.

Why can't I run node.js & socket.io in a screen or with Forever?

I've got some annoying problem and that is I have trouble in running nodejs in a screen. Because as fast as I leave the screen and no sockets are connected the next whom is to connect will be faced by an error message until the screen is opened again. As soon as the screen is open with screen -R node (for example) it will start accepting connections again.
However notice that if the screen is closed (running in the background) and someone already has a socket open, it will continue to answer new connections.
When I try to start the application again, with the command node app I get the following message:
module.js:340
throw err;
^
Error: Cannot find module '(unreachable)/square_defense/app'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.runMain (module.js:492:10)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
Server side code:
var app = require('express')()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server);
server.listen(3000);
app.get('/', function (req, res) {
console.log(__dirname);
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
socket.on('user-message', function (data) {
console.log(data);
sendMessage.call(socket, data.message);
});
});
var sendMessage = function(message) {
this.emit('server-message', {message: message});
this.broadcast.emit('server-message', {message: message});
}
I have tried a lot of things like npm install app in directory .. from the app.js. Can't understand why it dont want to work. I really just want a server which I can use for testing purposes that do not crash.
EDIT: I think this has something to do with an encrypted home folder?
I think the unreachable folder is where the problem resides. Error: Cannot find module '(unreachable)/square_defense/app'. But I don't know why this is happening. Right now I am running the application with Forever and as soon as I have an ssh-session on it will work. But as soon as I exit the connection and reload the page. It will generate an error.
As you said, your home directory is encrypted, and according to the document from Ubuntu:
This directory is automatically mounted on login, and unmounted on logout.
So your guess is correct, this is exact what's causing the error. Running a detached screen session won't be considered by the OS as the user are still logged in*. The time all shell sessions of the user ends, the encrypted home directory got unmounted.
Solution: You can move your app to a directory other than $HOME, then I think either GNU Screen or forever will do the job without errors.†
* You can test this by your self: login, start a screen session, run something like vim, detach; use command w to see who is logged in and what they are doing
† I think if you write your own upstart scripts for this, it is still crucial that the app files are not placed in auto-encrypted home directory.
Since you already are on an Ubuntu box, i recommend using Upstart and monit as described in this post: http://howtonode.org/deploying-node-upstart-monit
The only thing i added into the mix was a frontend proxy-server (HAProxy in my case, but nginx works as well) in order to serve my application on port 80.
I deployed a socket.io/express app using this technology stack on Amazon EC2 myself about a month ago, with amazing results!
EDIT:
Here are some resources i collected over time concerning a proper production setup using node.js/nginx/monit/git:
http://cuppster.com/2011/05/12/diy-node-js-server-on-amazon-ec2/ (an awesome guide, helped me a lot!)
http://howtonode.org/deploying-node-upstart-monit
I really recommend the first one, you might have to skip some parts (like npm module installation or system setup), but the parts about nginx/git and supervisor are worth a read.

Where do I need to save javascript files for Node.js in windows 8

I have JUST downloaded node.js and am having to work with their command line for the first time. It appears that every tutorial on the planet gives the same starter app.
It uses the code...
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8124);
console.log('Server running at http://127.0.0.1:8124/');
and wants me to save it to the file example.js.
The file can then be run by typing...
node example.js
but all the command line gives me is
...
The tutorials do not say WHERE I should save the file. To my C drive? To the same file as node.js? Anywhere?
I have tried all three and they don't seem to work. If there is any other solution, or simply something else I should be looking into and asking about, that help would be appreciated too. But at this point I honestly have no idea what the problem is, and there appears to be very few resources to help me here.
When you run node example.js from your prompt, it's assuming that example.js is in the current working directory.
To change your current working directory, use the change directory (cd) command:
cd C:\Projects\MyProject
node example.js
This is equivalent to
node C:\Projects\MyProject\example.js
Correct! you can save it any where and change to that directory by the (cd) command on node.js command prompt which will then give message says: "Server running at: 127.0.0.1:8124.
So point your browser to that address and you see your file.

Categories