From 2261105368591db88a61d35a707e6ca58561040e Mon Sep 17 00:00:00 2001
From: supositware <loic.bersier1@gmail.com>
Date: Mon, 13 Sep 2021 15:46:28 +0200
Subject: [PATCH] use proxy on Hapi too

---
 commands/utility/download.js | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/commands/utility/download.js b/commands/utility/download.js
index f62149a0..38546fea 100644
--- a/commands/utility/download.js
+++ b/commands/utility/download.js
@@ -115,13 +115,23 @@ class DownloadCommand extends Command {
 
 			const params = new URLSearchParams();
 			params.append('url', args.link.href);
+			params.append('proxy', proxy[args.proxy].ip);
 
 			fetch(`${Hapi}/download`, {method: 'POST', body: params})
 				.then(async res => {
 					console.log(`status is ${res.status}`);
 					if (res.status !== 200) {
 						console.log('Status is not 200. Abort');
-						return message.channel.send('An error has occurred.');
+
+						if (args.proxy != null) {
+							args.proxy = args.proxy + 1;
+						} else {
+							args.proxy = 0;
+							args.proxyAuto = true;
+						}
+
+						if (!proxy[args.proxy]) return message.channel.send('`HTTP Error 429: Too Many Requests.`\nThe website you tried to download from probably has the bot blocked along with its proxy');
+						return this.client.commandHandler.runCommand(message, this.client.commandHandler.findCommand('download'), args);
 					}
 
 					if (res.headers.get('content-type') === 'application/json; charset=utf-8') { // If we receive JSON it mean it started compressing the video
@@ -200,6 +210,7 @@ class DownloadCommand extends Command {
 					}
 				});
 		} else { // No Hapi server, let the bot download it
+			console.log('[Download] Hapi is not online.');
 			if (args.proxy && !args.proxyAuto) { // args.proxyAuto is only provided when the command is run after a error 429
 				args.proxy = args.proxy - 1;
 				if (!proxy[args.proxy]) args.proxy = 0;