I'm trying to get the URL of any attachments in a message. I can't seem to find a way to do this: whenever I try to run console.log(message.attachments.url), it just outputs undefined. What am I doing wrong?
I've tried reading the docs and other Stack Overflow questions but nothing worked.
I expect the output to be a URL of the attachment, i.e. 'https://cdn.discordapp.com/attachments/serverid/channelid/file.png' However, it just outputs undefined.
message.attachments is a Collection (a Map with additional Utility functions) so you either have to get the specific attachment via message.attachments.get('ID') or if you are sure that the message only has one attachment you can use message.attachments.first(). Otherwise you have to iterate through the Collection via
message.attachments.forEach(attachment => {
// do something with the attachment
const url = attachment.url;
});
I linked to the Collection docs of Discord.js. You also have access to the typical Map functions as well.
Related
I'm trying to use a REST web service from Geonames.org. When I try to manually put in the url with the parameters, it would only return the Country Code. I've tried to search for ways to implement it, but most of what I've seen return JSON text with multiple keys and data. I feel like the answer should be pretty simple, but I'm unsure.
I'm trying to use this for a React project I'm working on.
Here is an example of what the url returns
Just looked into the docs of that API, and it says if you want to receive JSON responses simply add JSON keyword to the endpoint. Like here for given endpoint you have:
http://api.geonames.org/countryCodeJSON?formatted=true&lat=47.03&lng=10.2&username=demo
so just change countryCode to countryCodeJSON.
Source: http://www.geonames.org/export/JSON-webservices.html
I am using puppeteer, and I want to send a screenshot of the webpage if there is an error. The screenshot is taken correctly, but I cannot send the screenshot. I keep getting [object Object] in the channel instead of the image.
let message = new Discord.MessageAttachment(await page.screenshot({
quality:10,
type:'jpeg'
}),"ERRORIMAGE.png")
mainChannel.send({files:[message]});
I have also tried .send(message), .send('error image',message), and .send({attachments:[message]}).
I also tried saving the image as a file then giving the path but that also just gave [object Object].
Your best option is sending it through a Discord.messageEmbed using the setImage method on it .
const embed = new Discord.MessageEmbed().setImage('insert the url of the image here')
mainChannel.send(embed)
If it is an image you will be using frequently you might need to host it somewhere for better response times.
here is a link for a guide on Discord.MessageEmbed
it also includes stuff related to Discord.MessageAttachments which you can check out
you are looking for a slightly different syntax
//I will not include the screenshot code, since you said that was working fine, and I will therefore assume you do not need help with that
let message = new Discord.MessageAttachment(await page.screenshot({quality:10,type:'jpeg'}),"ERRORIMAGE.png");
message.channel.send('Message that goes above image - can be removed for no message', {
files: [message.path] //can attach multiple files, simply add more commma delimited filepaths
});
This should work - make sure to add the filepath of the image, not just its name
Edit: the [object, Object] error occurs when you try to use an object without specifying what part you are using. This means that most likely you need to do message.path or something similar, since message is an object.
You read that right, this is AngularJS, the old one.
I have an Angular $http.post that returns an object with various values including a fairly large dictionary of more objects that map to a bunch of fields on a spreadsheet.
The magic:
If I debug the backend (.NET) and look at the object being returned all is well and as expected. If I log out the res.data object right after it is returned in the JS callback there is one particular (possibly more) string value within that dictionary of objects I mentioned that is set from "Some text" to "". No errors, no warnings...
But get this... if I look at the response in Fiddler THE VALUE IS THERE.
The discovery:
If I do a Request for only that Dictionary of Objects rather than it plus other data the value does indeed appear. Cool, so I can work around this... But WHY?
Why would this happen? Is there some size cap on response objects to Angulars $http.post? I am passing in no options to the post and I cannot find any info on this.
Thanks for any ideas!
UPDATE: (Code has been requested)
$http.post(baseUrl + "TESTCompData?p1=" + p1 + "&c1=" + c1).then(function (res) {
console.log(res);
})
The dictionary object I'm referring to:
public Dictionary<string, Dictionary<string,object>> ControlData;
The object in question:
{txt_SomeTexty: {
LineItemDataID=998,
LineItemMetaDataID=325,
SectionID=11,
Value=Some Text for XX (not a abc): $999,999,999}}
Value here comes in blank.
Figured it out though.
Turns out I was just accidentally clearing the field during a transformation method I had.
But what I don't understand: I was console.log'ing the object BEFORE this took place yet I saw it changed there. See I expected the logged object to be what it was when the response was returned (since that's where the log was) but somehow it isn't... it's the updated version.
Trying to do something which is very simple but am getting strange results.
I have an image stored as an attachment which I want to display in a VF Page.
If I do this it works fine
<apex:image url="{!URLFOR($Action.Attachment.Download,'00PR0000008Q3YmMAK')}"/>
However that was for testing purposes that I hardcoded the id. If I try reference the Id in the object then it fails. Even though the value contained in the object is exactly the same as above.
<apex:image url="{!URLFOR($Action.Attachment.Download,model.PreviewImageAttachId)}"/>
When I load the page I get an error in URLFOR param!!!
I thought my problem was because model.PreviewImageAttachId was a String and not an Id so I created a wrapper to return it as an Id - same error.
I then decided Salesforce must have some strange requirement that you can only pass in the REAL object so I did that and passed in model.Attach.Id and it still fails!!
Please can someone explain this to me and more importantly suggest a solution??!?
Once again if I output to the page
{!model.PreviewImageAttachId} i get 00PR0000008Q3YmMAK
So I just cant explain this!
Thanks!
My bad...
Was using apex:repeat and turns out SOME of the id values were null.
Hence the error
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.