Discord JS - Downloading an image from attachment - discord.js

Basic thing I just need fixed; wanting to download a file from attachment but i'm not sure how to describe the attachment as an object/file.
I just have this:
let jimage = message.attachments.first()
fs.writeFileSync(`./${jimage.filename}`, jimage)
Only issue is that, for FS's second argument, discord doesn't have a selection for just the file. Reason i'm trying this way is because I took this idea from here:
how to save an image from discord using discord bot node js
But "attachment.file" isn't a real thing. Other threads show just really overly complicated methods with new calls, like something called request, I just wanna do this straight off fs and discord.

I found a pretty simple solution using this line over here:
request(message.attachments.first().url).pipe(fs.createWriteStream('image.png'))
It basically finds where file is stored on the discord website and using "request" gets it from there. To keep file's name you can use message.attachments.first.name if you need to. Unfortunately I haven't found out how to save file type. If any questions, I will try to help!
P.S. don't forget to define and download the "request" module.

Related

How to add copy button to Discord Webhook embed message

In the image above, you can see the text with in blue color and the copy button.
I want to create the same thing, but don't know how to do it.
Does anyone know how to do it?
I just checked by joining the discord you sent. And the copy icon is just a simple icon with no use.
You can't at the moment make a "copy to clipboard" action using any sort of text inside an embed. (It was already suggested a while ago here)
Maybe Discord will add this feature someday.
If you just need to copy the exact same thing, they are doing something like:
[click me](https://google.com) 📋
Discord has not added this functionality yet. The icon there is likely just a small image.. However, you can use an external library called pyperclip. Then, run the following code with an interaction.(interactions are discord 2.0 stuff but are pretty stable in my opinion)
import pyperclip pyperclip.copy('The text to be copied to the clipboard.')

send all messages to a specific channel in discord js

I started coding my discord bot, and have just tried adding a feature that logs specific raw events to a specific channel. I looked countless times on Reddit and this site to try and find a way that isn't deprecated, but no luck. I was hoping that I would answer on this website. To summarize, I need to send messages to a specific channel. Another thing is pulling information from things inside JSON files that look like this:
info.json: {t: 'PRESCENCE_UPDATE,
{other_thing_i_want_to_get: 'blah blah blah'}}
how do I get the other thing with a format of info.t.other_thing_i_want_to_get, or something like that?
i would recommend creating a json file and importing it.
using the npm module fs you can create WriteFileStreams to change the values stored in the file
const info = require('./path_to_json/info.json');
your json will have to look like:
{t: 'PRESCENCE_UPDATE', other_thing_i_want_to_get: 'blah blah blah'}
with this code you can get it using info.other_thing_i_want_to_get

Where can I find the simple information of the format for uploading questions?

Situation:
I want to train and simple configure the retrieve and rank service.
I just uploaded some PDFs and now I want to upload some questions.
In the documentation I do not find a simple information how the csv file must be structured and which are the must fields and which are not must files.
Something like: "[YOUR QUESTION (MUST)]",[DOCUMENT ID (MUST)], [RANKING (OPTIONAL)]
The document ID you will find in xyz in section xyz.
Inside the help I can not find such kind of help.
https://www.ibm.com/watson/developercloud/doc/retrieve-rank/training_data.shtml#script
Impact:
There is no chance to get a "real" documentation of the configuration outside the tutorial.
Possible Solution:
Provide additional documenation.
Maybe I was not able to find it and someone can guide me to the right place?
Ok, I found the solution for me, by try and error. Following steps do work for me:
1) You need a plain text file and the ending should be *.txt
2) Inside the file you have to write your questions like this:
What is the best place to be?
Why should I travel to the USA?
-> Don't do it like
"What is the best place to be?"
For me the help was missleading, because saying something about CSV files.
You can take a look also in the comment of #dalelane he is right, and highlight the entry text for the upload of the file.

Getting $http.put() to send correctly formatted data, instead of JSON object

So, I spent some time and built a quick API for a project that I'm doing for myself.
I used the Postman add-on for Chrome to mimic PUT and DELETE quests to make sure everything worked correctly. Really happy I did that, as I learned a lot about PHP's shortcomings with PUT and DELETE requests.
Using the API I've had working with Postman, I started moving everything over to AngularJs controllers and such.
I'm trying to get a user to claim a row in a database as the login information for the users is different than this particular information. I couldn't figure out why the put requests to claim the row in my database wasn't working. Lo and behold, the data being parsed from my parsestr(file_get_contents('php://input')) had 1 array key, which was a JSON string.
I've looked, and I can't seem to find a solid answer either through Stackoverflow or Google (maybe I missed it somewhere in the config options), So my question is this: is there any way I can get the $http.put call send the data to the server correctly?
Thanks to user Chandermani for pointing me to the link at this URL which answered the base of my question.
From the above link, I found myself on This Blog post submitted by another user. In the end, what I ended up doing was the following:
taking param() function from the above link, as well as implementing these lines of code:
var app = angular.module('ucpData', [] , function($httpProvider){
$httpProvider.defaults.transformRequest = [function(data) {
return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
}];
});
Is how I worked around the problem. For some developers, you may actually want to keep the default transformRequest settings, but for the project I am doing I know that I will end up forgetting to call param() at some point, and my server doesn't naturally accept json data anyway. I would caution future developers to consider what they are attempting to do before they alter the transformRequest array directly.

Receive multi file post with google app engine

I want to receive multi file post from image uploader.(i use this)
Most examples show how to receive one image from post.
I tried many ways but never got the results.
For example
self.request.POST['Filename']
gives only first filename.
What to do when there are multiple files/images in post?
The reason for this is to resize before upload images, that are too big for google app engine
to upload.
EDIT:
self.request.POST.multi.__dict__
shows
{'_items':
[('Filename', 'camila1.jpg'),
('Filedata[]', FieldStorage('Filedata[]', 'camila1.jpg')),
('Upload', 'Submit Query\r\n--negpwjpcenudkacqrxpleuuubfqqftwm----negpwjpcenudkacqrxpleuuubfqqftwm\r\nContent-Disposition: form-data; name="Filename"\r\n\r\nbornToBeWild1.jpg'),
('Filedata[]', FieldStorage('Filedata[]', 'bornToBeWild1.jpg')),
('Upload', 'Submit Query')]}
Your flash uploader is designed to work with PHP and sends multiple Filedata[] fields (php interprets this as an array for easy access)
So you need to iterate and get them all:
def post(self):
for file_data in self.request.POST.getall('Filedata[]'):
logging.info(file_data.filename)
data should be file_data.value
Are you using the Django libraries available to you? If so, check this out.
Call self.request.POST.getall('Filename') to get a list of FieldStorage objects; each one contains one file. You can access the file data with .value, the name with .name, and the mimetype with .type.
I have no idea how that multi uploader works, I have made one in the past however and I just added a number on the end of input field name, then hide it. Then add a new file input field to add another file. The reason for this is that they don't let you play around with input file fields to much because you could make it upload files they didn't want you uploading.
Using my conventions, in your example the 2 files in your example would be "Filename0" and "Filename1". You could also use firebug to see what it renaming the input file fields to.
Edit: I had a look, and it's using flash. So i have no idea how it works.

Resources