Reload command discord.js - javascript

I'm making a discord bot with discord.js, but my reset command isn't working.
//!reload
if(command === `${botsettings.prefix}reload`) {
console.clear();
bot.destroy()
bot.login(botsettings.token);
message.channel.send("Reloaded");
return;
}
It doesn't give any error; it restarts and goes back on but the commands don't update!
I tried so many different things I found, but it doesn't work.
I only have one file for all commands.

If you use a nodemon system and a local batch file it will automatically reload the file for every change saved in it, and will output the new file changes.
If you need more help on this, watch a few videos from TheSourceCode on YouTube and you'll see in around 3-4 episodes on him using nodemon.
If you end up hosting it on Heroku, which is frowned upon, but I do, it will automatically reload for every change in your GitHub repository, just make sure to replace your token with process.env.token and make sure you have it set on your Heroku.

So i am guessing you have your various files set out (config etc) and one central bot.js file, and you load the bot by using something like
node bot.js
on your host machine, and obviously part of your .js file sets up the bot something like this:
const bot = new Discord.Client();
From what I can tell, your
bot.destroy()
and
bot.login(botsettings.token);
is just refreshing the const 'bot', but what it is not doing is reloading your bot.js file (with the updated commands and code).
What you would need to do, is have it set up to run a batch file or something on your host machine that terminates the entire process bot.js, and then restarts it. As this would then use the new and updated bot.js file.
The only problem is I am still figuring out how to run a batch file from my JS file, as understandably for security that feature isn't built in (other wise most websites that use JS would be vunerable to getting it to run things like format C:\)
I imagine it will involve using WSH in my JS, and I will update here if I do get it going.
I hope this was clear? let me know if you still have a question :)

In the code you provided, it seems like your only refreshing the const 'bot' instead of reloading the bot.js file.
I recommend that you use a batch file.

client.destroy();
client.login();
That's not possible since there's a dead socket.
Instead, you're able to make a Shell script and have a background Daemon service the needs that you want.

Related

Running Puppeteer in the Cloud and Passing It's Return to JS

I'm running a puppeteer script on Heroku. I've got buildpacks sorted and it's running fine. Eventually I intend to move this to my own server and run it on a 5 minute loop. My issue is that it encounters a timeout (H12 on Heroku, takes more than 30000ms to send any data back to server). Now, I've decided to try and run the puppeteer on the back-end and then return a JSON that is passed to the front-end JS and used to shape the resulting webpage.
My issue is this. I cannot use a require statement in my front-end JS as it's a caveat to node, not to "web" JS. I can't use Browserify on it as Browserify naturally doesn't support puppeteer and I can't include the puppeteer in the Express routing file as that would place the puppeteer script on the front-end which, as stated before, causes a timeout AND won't allow me to change the way the html looks.
Basic Desired Function Structure:
Call made to localhost:PORT/puppet
Puppet script invoked
Puppet script checks if pages are up/scrapes data/does automated testing etc
Script returns a JSON which is then saved to a variable
Express serves HTML file with script and css
Script uses returned JSON to dictate page structure/style/classes
File Structure:
puppet.js (puppeteer script)
server.js (handles routes and express)
index.html (served by server.js)
master.css
package.json
package-lock.json
Node Modules:
colors
express
puppeteer
I've spent a good 4-5 days solid on this, any and all help is greatly appreciated
EDIT: For clarification, I need a way of passing a JSON from the server-side puppeteer script to the javascript that the html served by Express uses so I can use the values in the JSON to change the webpage.
I.E if the JSON was (a: true, b: false) then I'd give div#a the class .success and div#b the class .warn
EDIT 2: New Issue, can't use fetch on localhost. Got an api set up but can't fetch from it. Every solution garners a new problem or so it seems
Well you can't increase the timeout for Heroku, so if you really want to trigger your puppeteer script from a request you'll need to do something like this:
send a 200 status
Get the puppeteer script running
Save the .JSON result in a DB (because you cannot res.send() anymore)
Fetch the results from the DB in your Front-End (so maybe query it every X seconds to see if your results are in.)
Fetch the desired HTML,CSS etc. now that you don't have to wait for the puppeteer script to end.
It's not especially elegant, but a request taking more than 30 seconds is a special case.
You could send an ID along with the 200 status code at step 1 which you can then use to query the DB at step 4.
Also, you probably know this, but you can use something like heroku-scheduler or the npm package node-schedule to make your puppeteer script run every X minutes, but that would also imply saving the results to a DB.

NodeJs temporary file creation, serve and deletion using tmp or something else

I am trying to make a compiler in which users make code at abc.com/newProject and their output is in an iframe, that iframe need to be served files that are made at the abc.com/newProject. So I will be doing a POST of JSON obj at abc.com/compile-project that will create files and those will be used by the iframe, after being used those should get deleted. Files are basically JS files that iframe will fetch using script in header.
So a pseudo-code will look something like this:-
app.post('/compile-project', function(req, res){
//Directory created using node tmp
//files created in the directory
//These files are accessible using <script src="/js/file1.js"></script>
//when the current connection requests the files they get deleted
});
Any help will be appreciated thanks.....
I am trying to make a compiler [...] Any help will be appreciated thanks.....
I would strongly discourage you from doing that if you don't know what you're doing (and considering the fact that you're asking how to save a file then apparently you don't).
The requirements that you described are extremely simple but you need to have much deeper understanding of everything that's going on to avoid serious security problems that you will encounter with no doubt along the way.
What you describes can be done without even using a file system, since all your files are served only once so it doesn't make much sense to store them in actual files. But even if you insist on the file system then all you need is to use fs.mkdtemp to create a temporary directory, use something like the uuid module for unique IDs to use in the filenames, then use fs.writeFile to write a file. This is all you need for the file upload endpoint. Now in the download endpoint all you need is to use fs.readFile to read the file and fs.unlink to remove it. That's it.
Now, it will surely get you into trouble of failures on browser reloads, back button not working, and finally security issues of people being able to serve any random code from your servers leading to vulnerabilities too numerous to even list here.
Take a look at the source code of repl.it and JS Bin on GitHub:
https://github.com/replit/repl.it
https://github.com/jsbin/jsbin
to appreciate the scope of the project that you are willing to undertake.

External javascript in html is sent incorrectly to the server

I'm running a Node.js server along with an Angular frontend. One of the Angular dependencies I'm using requires me to import a javascript file into my html page, by the name of swing.js. However, when I try to do this, it sends the required file as an http request to the server, resulting in requests that look like the following:
http://localhost:3000/home/me/app/node_modules/angular-swing/dist/swing.js
Obviously, this comes up as a 404. As an alternative, I've tried changing
<script src="/home/me/app/node_modules/angular-swing/dist/swing.js"></script>
into
<script src="swing.js"></script>
and then on the server-side, doing:
app.get('swing.js', function(req, res){
res.sendFile('home/me/app/node_modules/angular-swing/dist/swing.js');
});
This works a little more, but then the file doesn't run properly, as I'm assuming it's no longer in the npm environment it needs to be in. I've tried multiple iterations of changing
<script src="/home/me/app/node_modules/angular-swing/dist/swing.js"></script>
into something that uses periods (.) to represent more relative paths, but that doesn't appear to work either. Overall, I'm very stuck, and would appreciate any insight. If it's of any use, I'm also using:
app.use(express.static(__dirname+'/public'));
Making my comments into an answer...
node.js does not serve any files by default so any script files that you need sent from your server to the client upon request need an explicit route to handle them or they need some generic route that knows how to handle all the requested script files.
swing.js in the client is not part of any "NPM environment". It's running the browser at that point, not in any NPM enviornment. It's possible that swing.js itself needs some other scripts or resources that also need routes and that's why it doesn't work after you make an explicit route for it. You can probably examine the specific errors in the console to give you a clue why it isn't working.
You may also want to read this: How to include scripts located inside the node_modules folder?

Save file Python - IOError Permission Denied

I am making a post request to a python script and then I want to save an image file to the server, in the html folder here is what I am doing
fh = open("/var/www/html/images/logo.png", "wb")
fh.write(image_data.decode('base64'))
fh.close()
but then I get this error, IOError: [Errno 13] Permission denied: I have found out that it means this file doest have permission to save the image, and I could get around it by doing chmod to 777 but that is dangerous and bad, so how else can I save the file but not let the file be edited by anyone?
Thanks
You could either change the permissions or run the script as a user with the correct permissions. I don't see many other options. You could have your script edit the permissions, write, and then change the permissions back although it would still be editable for a moment, and it just seems like a bad idea. The script could fail for some reason and leave the permissions allowing anyone to edit. Edit: If you can change the permission you should be able to write, making my last idea pointless.
It may appear a bit convoluted but I suggest that you might want to investigate the /etc/sudoers file.
For your purposes, you would create the file somewhere that you do have permissions like /tmp and then move it to /var/www/html/images/ using a script that is named as a sudoers Cmnd_Alias.
You still need to envoke the Cmnd_Alias script with sudo but if you have declared it with NOPASSWD then it should just function without asking for the sudo password.
Always access the /etc/sudoesrs file using visudo
an example of the entries in that file:
# Cmnd alias specification
Cmnd_Alias WEB = /home/test.s
and then later in the file:
# User privilege specification
root ALL=(ALL:ALL) ALL
rolf ALL=(ALL:ALL) NOPASSWD:WEB
with this, the /home/test.sh will perform whatever task the user rolf sets it, such as moving a file from /tmp to /var/www/html/images/ when the command line, sudo /home/test.sh is issued, without asking for the password.

Call PHP file via BATCH

I want to create a BATCH File which is like a Cron Job. It runs in a loop and should call a PHP File which is on my server. I don't want my BATCH file to open hundrets of Web Browser Windows.
Any idea how to get this working?
Thanks
in your batch file, you can call pathToYourPhpInstallation/php yourphpfile.php the same way you can do it in your command line / terminal.
For instance, if by BATCH you mean a .bat file on your windows system, it could look like this:
dosomething.bat:
c:
cd C:\www\myApp\backend
delete *.tmp
"C:\php\php.exe" ajax-backend.php
If you have a mor complex site that you need to start or do something with, some of the common test frameworks (i.e. selenium) are able, even thou not intended to do so.

Categories

Resources