Skip to content

Commit

Permalink
Added catching errors in case of wrong config data
Browse files Browse the repository at this point in the history
  • Loading branch information
Prits001 committed Jul 10, 2022
1 parent 94f5a79 commit 9d7b6b3
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -26,16 +38,15 @@ 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);
return true
}
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-----------------------------------------------------")
Expand Down Expand Up @@ -100,4 +111,4 @@ async function setActivity() {
start()
Api.on('ready', async () => {
setActivity()})
Api.login(({ clientId })).catch(err => console.error(err)).then()
Api.login(({ clientId })).catch(err => console.error(err))

0 comments on commit 9d7b6b3

Please sign in to comment.