'use strict';

var fs = require('fs');
var path = require('path');
var request = require('request');

const AVATAR_PATH = path.join(__dirname, '../config/avatars/');

function download_image(image_url, name, extension) {
	request
		.get(image_url)
		.on('error', function (err) {
			console.error(err);
		})
		.on('response', function (response) {
			if (response.statusCode !== 200) return;
			var type = response.headers['content-type'].split('/');
			if (type[0] !== 'image') return;

			response.pipe(fs.createWriteStream(AVATAR_PATH + name + extension));
		});
}

function load_custom_avatars() {
	fs.readdir(AVATAR_PATH, function (err, files) {
		files
			.filter(function (file) {
				return ['.jpg', '.png', '.gif'].indexOf(path.extname(file)) >= 0;
			})
			.forEach(function (file) {
				var name = path.basename(file, path.extname(file));
				Config.customavatars[name] = file;
			});
	});
}

load_custom_avatars();

exports.commands = {
	customavatar: {
		set: function (target, room, user) {
			if (!this.can('customavatar')) return false;

			var parts = target.split(',');

			if (parts.length < 2) return this.parse('/help customavatar');

			var name = toId(parts[0]);
			var image_url = parts[1];
			if (image_url.match(/^https?:\/\//i)) image_url = 'http://' + image_url;
			var ext = path.extname(image_url);

			if (!name || !image_url) return this.parse('/help customavatar');
			if (['.jpg', '.png', '.gif'].indexOf(ext) < 0) {
				return this.errorReply("Image url must have .jpg, .png, or .gif extension.");
			}

			Config.customavatars[name] = name + ext;

			download_image(image_url, name, ext);
			this.sendReply(parts[0] + "'s avatar has been set.");
		},

		delete: function (target, room, user) {
			if (!this.can('customavatar')) return false;

			var userid = toId(target);
			var image = Config.customavatars[userid];

			if (!image) {
				return this.errorReply("This user does not have a custom avatar");
			}

			delete Config.customavatars[userid];

			fs.unlink(AVATAR_PATH + image, function (err) {
				if (err && err.code === 'ENOENT') {
					this.errorReply("This user's avatar does not exist.");
				} else if (err) {
					console.error(err);
				}
				this.sendReply("This user's avatar has been successfully removed.");
			}.bind(this));
		},

		'': 'help',
		help: function (target, room, user) {
			this.parse('/help customavatar');
		}
	},
	customavatarhelp: ["Commands for /customavatar are:",
	"/customavatar set [username], [image link] - Set a user's avatar.",
	"/customavatar delete [username] - Delete a user's avatar."]
};

@@ -8,19 +8,21 @@
 rict';%0A%0A
-var
+const
  fs = re
@@ -34,19 +34,21 @@
 ('fs');%0A
-var
+const
  path = 
@@ -64,19 +64,21 @@
 path');%0A
-var
+const
  request
@@ -394,19 +394,21 @@
 urn;%0A%09%09%09
-var
+const
  type = 
@@ -799,19 +799,21 @@
 ) %7B%0A%09%09%09%09
-var
+const
  name = 
@@ -1060,19 +1060,21 @@
 se;%0A%0A%09%09%09
-var
+const
  parts =
@@ -1164,19 +1164,21 @@
 ');%0A%0A%09%09%09
-var
+const
  name = 
@@ -1192,27 +1192,29 @@
 rts%5B0%5D);%0A%09%09%09
-var
+const
  image_url =
@@ -1303,19 +1303,21 @@
 url;%0A%09%09%09
-var
+const
  ext = p
@@ -1793,19 +1793,21 @@
 se;%0A%0A%09%09%09
-var
+const
  userid 
@@ -1829,11 +1829,13 @@
 %0A%09%09%09
-var
+const
  ima

Use const in customavatar