From 9d7b6b3a574e9ea491a02d3cafa0fdf889519faa Mon Sep 17 00:00:00 2001 From: Prits001 Date: Mon, 11 Jul 2022 02:45:18 +0300 Subject: [PATCH] Added catching errors in case of wrong config data --- main.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/main.js b/main.js index 993b966..848cb01 100644 --- a/main.js +++ b/main.js @@ -7,11 +7,23 @@ const DiscordApi = require('discord-rpc'); const { time } = require('console'); const Api = new DiscordApi.Client({ transport: 'ipc'}) DiscordApi.register(clientId); -function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); +function checkInput(){ + if(config.drawingname.length > 128){console.error("\x1b[31mDrawing name must be less or equal to 128 characters long\x1b[0m")} + if(config.additionalinfo.length > 128){console.error("\x1b[31mAdditional info must be less or equal to 128 characters long\x1b[0m")} + if(config.largeimagetext.length > 128){console.error("\x1b[31mText when hovering above large image icon must be less or equal to 128 characters long\x1b[0m")} + if(config.drawingname.length < 2){console.error("\x1b[31mDrawing name must be more or equal to 2 characters long\x1b[0m")} + if(config.additionalinfo.length < 2){console.error("\x1b[31mAdditional info must be more or equal to 2 characters long\x1b[0m")} + if(config.largeimagetext.length < 2){console.error("\x1b[31mText when hovering above large image icon must be more or equal to 2 characters long\x1b[0m")} + if(config.numberOfButtons < 0 || config.numberOfButtons > 2){console.log(`\x1b[31mYou can't have ${config.numberOfButtons} buttons! Please abort and fix the issue!\x1b[0m`)} + if(config.numberOfButtons > 0){ + if(config.button1Text.length > 32 || config.button1Text.length < 2){console.error("\x1b[31mName of the 1st button can't be less than 2 or more than 32 characters long\x1b[0m")} + if(config.numberOfButtons === 2){if(config.button2Text.length > 32 || config.button2Text.length < 2){console.error("\x1b[31mName of the 2nd button can't be less than 2 or more than 32 characters long\x1b[0m")}} + } } + function checkConfig(){ - if(config.clientId === '' || config.drawingname === '' || config.additionalinfo === '' || config.largeimagetext === ''){return false} + if(config.clientId === '' || config.drawingname === '' || config.additionalinfo === '' || config.largeimagetext === ''){console.error("\x1b[31mSome data is missing!\x1b[0m")} + checkInput() console.log('\x1b[36m%s\x1b[0m', "[!]", "\x1b[32mCurrent config settings:\x1b[0m") console.log('\x1b[36m%s\x1b[0m',"[#]", "Application id: ", "\x1b[32m"+config.clientId+"\x1b[0m") console.log('\x1b[36m%s\x1b[0m',"[#]", "Drawing name: ", "\x1b[32m"+config.drawingname+"\x1b[0m") @@ -26,7 +38,6 @@ function checkConfig(){ console.log('\x1b[36m%s\x1b[0m',"[#]", "Url of the 2nd button: ","\x1b[32m"+config.button2Link+"\x1b[0m") } } - if(config.numberOfButtons < 0 || config.numberOfButtons > 2){console.log(`\x1b[31mYou can't have ${config.numberOfButtons} buttons! Please abort and fix the issue!\x1b[0m`)} console.log("Starting in 10 seconds..") console.log("\x1b[46mPress Ctrl+C to abort\x1b[0m") Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 10000); @@ -34,8 +45,8 @@ function checkConfig(){ } function start(){ console.log("\x1b[35mFireAlpaca Rich Presence - by Prits#2138\x1b[0m") + checkConfig() console.log("-----------------------------------------------------") - if(!checkConfig()){throw new Error("Config Settings were terminated!");} console.log("Connecting to Discord...") console.clear() console.log("\x1b[35mFireAlpaca Rich Presence - by Prits#2138\x1b[0m \n-----------------------------------------------------") @@ -100,4 +111,4 @@ async function setActivity() { start() Api.on('ready', async () => { setActivity()}) -Api.login(({ clientId })).catch(err => console.error(err)).then() \ No newline at end of file +Api.login(({ clientId })).catch(err => console.error(err)) \ No newline at end of file