From 73581ce89f24d41981fff7d28dad439aa81f7645 Mon Sep 17 00:00:00 2001 From: loicbersier Date: Mon, 14 Sep 2020 15:37:28 +0200 Subject: [PATCH] Automatically switch proxy Signed-off-by: loicbersier --- commands/fun/ytp.js | 21 ++++++++++++++++++--- commands/utility/download.js | 22 +++++++++++++++++++--- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/commands/fun/ytp.js b/commands/fun/ytp.js index bbe86c0f..6500965c 100644 --- a/commands/fun/ytp.js +++ b/commands/fun/ytp.js @@ -164,7 +164,7 @@ class ytpCommand extends Command { } if (args.add) { - if (args.proxy) { + 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; } @@ -187,9 +187,24 @@ class ytpCommand extends Command { return downloader(url, options, `./asset/ytp/userVid/${message.id}.mp4`) .on('error', (err) => { - loadingmsg.delete(); - if (err.includes('HTTP Error 429: Too Many Requests')) return message.channel.send('`HTTP Error 429: Too Many Requests.`\nThe website you tried to download from probably has the bot blocked, you can try again with the `--proxy #` option ( can see the list of proxy with --listproxy ) and hope it work.'); + if (err.includes('HTTP Error 429: Too Many Requests')) { + 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'); + + loadingmsg.delete(); + return this.client.commandHandler.runCommand(message, this.client.commandHandler.findCommand('download'), args); + } + + if (err.includes('Error: status code 403')) return message.channel.send('`HTTP Error 403: Forbidden.`\nThe video you tried to download is not publicly available therefor the bot can\'t download it.'); + return message.channel.send(err, { code: true }); + }) .on('end', async output => { const hash = md5File.sync(output); diff --git a/commands/utility/download.js b/commands/utility/download.js index a3c6748c..c8fd7cd7 100644 --- a/commands/utility/download.js +++ b/commands/utility/download.js @@ -51,6 +51,7 @@ class DownloadCommand extends Command { } async exec(message, args) { + console.log(args.proxy); if (args.listproxy) { let proxys = []; @@ -74,8 +75,8 @@ class DownloadCommand extends Command { let loadingmsg = await message.channel.send('Downloading '); let filename = `${message.id}_video`; - if (args.proxy) { - args.proxy = args.proxy -1; + 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; } @@ -91,7 +92,22 @@ class DownloadCommand extends Command { downloader(args.link.href, args.proxy != null ? ['--proxy', proxy[args.proxy].ip] : null, `${os.tmpdir()}/${filename}.mp4`) .on('error', async err => { - if (err.includes('HTTP Error 429: Too Many Requests')) return message.channel.send('`HTTP Error 429: Too Many Requests.`\nThe website you tried to download from probably has the bot blocked, you can try again with the `--proxy #` option ( can see the list of proxy with --listproxy ) and hope it work.'); + if (err.includes('HTTP Error 429: Too Many Requests')) { + 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'); + + loadingmsg.delete(); + return this.client.commandHandler.runCommand(message, this.client.commandHandler.findCommand('download'), args); + } + + if (err.includes('Error: status code 403')) return message.channel.send('`HTTP Error 403: Forbidden.`\nThe video you tried to download is not publicly available therefor the bot can\'t download it.'); + return message.channel.send(err, { code: true }); }) .on('end', async output => {