Make Discord bot send picture with message with NodeJS
ClientUser.sendMessage
is deprecated, as is the file
parameter in its options. You should be using Channel.send(message, options)
, with files
as an array of strings or FileOptions.
bot.on('messageCreate' message => {
message.channel.send("My Bot's message", {files: ["https://i.imgur.com/XxxXxXX.jpg"]});
});
If you want to stick to your deprecated methods, ClientUser.sendFile
might be something of interest to you, though I do recommend you move over to the stuff that's more current.
send an image using discord.js
You can use
files: [{ attachment: "YourImage.jpg" }] });
You can also rename the image with
files: [{ attachment: <images>.toBuffer(), name: 'newName.png' }] });
Example:
message.channel.send({ files: [{ attachment: 'YourImage.png' }] });
the <images>
is your image variable
const images = blablabla
Make my discord.js bot to send a random picture from an array of images from my computer
setImage
only allows you to add an image hosted online (i.e. you need to provide a URL that starts with http
or https
). Luckily, you can upload a file from your computer using the attachFiles()
method instead of the setImage
.
const random = (arr) => arr[Math.floor(Math.random() * arr.length)];
const images = [
'./images/avatar1.png',
'./images/avatar2.png',
'./images/avatar3.png',
'./images/avatar4.png',
];
const embed = new MessageEmbed()
.setTitle('Attach file')
.setAuthor('joBas')
.attachFiles([random(images)]);
message.channel.send(embed);
The problem with this is that it's not the same as setting the image using .setImage()
. The image is not displayed in the embed, but right before it:
To solve this you can set the image to the newly uploaded image that can be accessed via attachment://fileName.extension
. As you will need both the filename and the extension, you need to store the filenames without the path in the array and store the path in a different variable.
Check out the code below:
const random = (arr) => arr[Math.floor(Math.random() * arr.length)];
const images = [
'avatar1.png',
'avatar2.png',
'avatar3.png',
'avatar4.png',
];
// path where the images are
const path = './images/';
const randomImage = random(images);
const embed = new MessageEmbed()
.setTitle('Attach file')
.setAuthor('joBas')
.attachFiles([`${path}${randomImage}`])
.setImage(`attachment://${randomImage}`);
message.channel.send(embed);
As you can see on the image below, the attached image is no longer outside of the embed:
Make Discord Bot post image from the link from the message
Here's a example how you could do it:
const Discord = require('discord.js');
const imageUrl = msg.content.split(' ').slice(1);
const embed = new Discord.RichEmbed().setImage(imageUrl)
msg.channel.send{ embed }
discord bot is not sending image of collected URL while using collector
just needed to use MessageAttachment
and you can send the image without link. reference
const { MessageAttachment } = require('discord.js')
//collector
const collector = message.channel.createMessageCollector({
filter,
max : 1 ,
time: 15000});
collector.on('collect', m => {
const attachment = new MessageAttachment('https://i.imgur.com/XATplyx.jpeg')// your URL
message.channel.send({ content: "Hello", files: [attachment] })
console.log(`Collected ${m.content}`);
});
collector.on('end', collected => {
console.log(`Collected ${collected.size} items`);
});
Send message with multiple local images in a single line - discord.js
There is no way to send images inline, instead you should use custom emojis.
You can use a private server for the bot that has all these letters as custom emojis. Then to get the emoji string put a backslash in front of it to get a string like this: <a:THIS:930734808169086986>
Related Topics
Pdf Blob - Pop Up Window Not Showing Content
How to Trigger a Click Event on Disabled Elements
Get Previous Value on Input Event
Javascript Submit Multiple Forms With One Button
Disable Prev Control on First Slide and Disable Next Control on Last Slide
Background-Image in React Component
Get Array of Values from Multiple Inputs Using Jquery
How to Unmute Html5 Video With a Muted Prop
Replace All Chars With #, Except for Last 4
How to Get Convert an Object to HTML Markup
Trigger JavaScript Event If Div Innertext Change
Jquery Datatables: Hide Search Bar
Displaying Navbar on Multiple HTML Pages Using Bootstrap
Split String in JavaScript and Detect Line Break
Change Value of a Variable Inside Foreach Loop? Make It Available Outside the Loop
How to Refresh the Data in a Jqgrid