fromNetwork static method

Future<AttachedFile> fromNetwork(
  1. String url,
  2. String name, {
  3. bool? spoiler,
  4. String? proxyUrl,
  5. int? height,
  6. int? width,
  7. String? contentType,
  8. String? description,
})

Creates an AttachedFile by fetching a file from a network URL.

This method downloads the file from the specified url and prepares it for attachment to a Discord message. The file is automatically converted to use Discord's attachment:// protocol with the provided name.

Example:

final attachedFile = await AttachedFile.fromNetwork(
  'https://example.com/data/data.json',
  'data.json',
  spoiler: true,
);

final builder = MessageBuilder()
  ..addText('Check out these datas!')
  ..addFile(attachedFile);

Implementation

static Future<AttachedFile> fromNetwork(
  String url,
  String name, {
  bool? spoiler,
  String? proxyUrl,
  int? height,
  int? width,
  String? contentType,
  String? description,
}) async {
  // Fetch bytes from the network URL
  final uri = Uri.parse(url);
  final response = await http.get(uri);

  // Create a MediaItem with the fetched bytes
  final media = MediaItem.fromNetwork(
    'attachment://$name',
    spoiler: spoiler,
    proxyUrl: proxyUrl,
    height: height,
    width: width,
    contentType: contentType,
    description: description,
  )
    ..bytes = response.bodyBytes
    ..spoiler = spoiler;

  return AttachedFile._(media);
}