diff --git a/bot/modules/rolesetter.js b/bot/modules/rolesetter.js index a7ba2b6..af7351e 100644 --- a/bot/modules/rolesetter.js +++ b/bot/modules/rolesetter.js @@ -13,47 +13,46 @@ exports.addrole = { usage: '', description: 'Adds you to specified role', process: function(bot, msg, suffix) { - //rolelist.allowedroles = rolelist.allowedroles.map(v => v.toLowerCase()); - // Here the bot,msg and suffix is avaible, this function can be async if needed. - let newrole = msg.guild.roles.find('name', suffix); + // Here the bot, msg and suffix is avaible, this function can be async if needed. + // Make sure to eliminate case sensitivity, do this here to only perform the sweep once. + let newrole = msg.guild.roles.find(role => role.name.toLowerCase() === suffix.toLowerCase()); + // Baserole is assumed to already be case accurate as it's handled in the config itself. let baserole = msg.guild.roles.find('name', rolelist.baserole); - // Checks if the user put a role in the message. - if (inPrivate(msg)) { - msg.channel.send('You can not set roles in DMs! Please go to the Discord server to do this.'); + + // Provide shortened syntax for the sake of code cleanliness + let send = (msgTxt) => { msg.channel.send(msgTxt); return; }; + let rolecmd = botconfig.prefix + "roles"; + + // Checks if the user has messaged the bot via Direct Message + if (inPrivate(msg)) + return send('You can not set roles in DMs! Please go to the Discord server to do this.'); + // Checks if the user included a role in their message + if (!suffix) + return send("Please specify a role. Type " + rolecmd + " to see which you may add yourself!"); + // Checks if there is a matching role found on the server + if(!newrole) + return send("The role specified `" + suffix + "` does not exist on this server!"); + // Checks that the allowed roles and base role against the matched role's name, since this eliminates case sensitivity issues + if(!rolelist.allowedroles.includes(newrole.name) && !rolelist.baserole.includes(newrole.name)) + return send("That role isn't one you can add yourself to! Type " + rolecmd + " command to find out which ones are allowed."); + // Use the matched name to check against the member's existing roles + if(msg.member.roles.find('name', newrole.name)) + return send("It seems you already have the " + newrole.name + "role"); + + // Assuming all these factors succeed, add the role + msg.member.addRole(newrole).then(send(msg.member + " has been added to the " + newrole.name + " role!")); + + // Check if a baserole is actually set + if(!rolelist.baserole) return; - } else { - if (suffix) { - //suffix = suffix.toLowerCase(); - // Checks if the role mentioned in the message is in the allowed roles listed in the wunderbot config. - if (rolelist.allowedroles.includes(suffix) || rolelist.baserole.includes(suffix)) { - // Checks if the role even exists in the discord server - if (newrole !== null) { - // Checks if the member has the role that they are trying to add - if (!msg.member.roles.find('name', suffix)) { - msg.member.addRole(newrole).then(msg.channel.send(msg.member + ' has been added to the ' + suffix + ' role!')); - if (rolelist.baserole !== ' ') { - if (baserole !== null) { - // Checks if Member has the baserole, and also checks if they just added the baserole - if (!msg.member.roles.find('name', rolelist.baserole) && suffix !== rolelist.baserole) { - msg.member.addRole(baserole).then(msg.channel.send(msg.member + ' has been added to the ' + rolelist.baserole + ' role!')); - } - } else { - msg.channel.send('The ' + rolelist.baserole + " Role doesn't exist. Please add that role first!"); - } - } - } else { - msg.channel.send('It seems that you already have that role! Try removing it first with the ' + botconfig.prefix + 'delrole command!'); - } - } else { - msg.channel.send('The role ' + '`' + suffix + '`' + ' does not exist!'); - } - } else { - msg.channel.send("That role isn't one you can add yourself to! Please run the " + botconfig.prefix + 'roles command to find out which ones are allowed.'); - } - } else { - msg.channel.send('Please specify a role. Type ' + botconfig.prefix + 'roles to see which you may add!'); - } - } + // Confirm that the role exists on the server and if not then be sure to send a nag message + if(!baserole) + return send("The base role of " + rolelist.baserole + " has been set in config but is missing from the server"); + // Confirm if the user has the baserole already, including if it was added just now + if(msg.member.roles.find('name', baserole.name)) + return; + // Add the base role and avoid spamming the user by only mentioning them in the previous message + msg.member.addRole(baserole).then(send("We also added the base " + rolelist.baserole + " role for you!")); } }; exports.delrole = { @@ -61,33 +60,30 @@ exports.delrole = { description: 'Deletes the specified role from your account', process: function(bot, msg, suffix) { // Here in the bot, msg and suffix are available, this function can be async if needed. - let oldrole = msg.guild.roles.find('name', suffix); - // Checks if the user put a role in the message. - if (inPrivate(msg)) { - msg.channel.send('You can not set roles in DMs! Please go to the Discord server to do this.'); - return; - } else { - if (suffix) { - // Checks if the role mentioned in the message is in the allowed roles listed in the Wunderbot config. - if (rolelist.allowedroles.includes(suffix)) { - // Checks if the role exists in the Discord server - if (oldrole !== null) { - // Checks if the member has the role that they are trying to add - if (msg.member.roles.find('name', suffix)) { - msg.member.removeRole(oldrole).then(msg.channel.send(msg.member + ' has been removed from the ' + suffix + ' role!')); - } else { - msg.channel.send("You don't seem to have that role! Try adding it first with the " + botconfig.prefix + 'addrole command!'); - } - } else { - msg.channel.send('The role ' + '`' + suffix + '`' + ' does not exist!'); - } - } else { - msg.channel.send("That role isn't one you can add yourself to! Please run the " + botconfig.prefix + 'roles command to find out which ones are allowed.'); - } - } else { - msg.channel.send('Please specify a role. Type ' + botconfig.prefix + 'roles to see which you may add!'); - } - } + // Make sure to eliminate case sensitivity, do this here to only perform the sweep once. + let oldrole = msg.guild.roles.find(role => role.name.toLowerCase() === suffix.toLowerCase()); + // Provide shortened syntax for the sake of code cleanliness + let send = (msgTxt) => { msg.channel.send(msgTxt); return; }; + let rolecmd = botconfig.prefix + "roles"; + + // Checks if the user has messaged the bot via Direct Message + if (inPrivate(msg)) + return send('You can not set roles in DMs! Please go to the Discord server to do this.'); + // Checks if the user included a role in their message + if (!suffix) + return send("Please specify a role. Type " + rolecmd + " to see which you may remove yourself!"); + // Checks if there is a matching role found on the server + if(!newrole) + return send("The role specified `" + suffix + "` does not exist on this server!"); + // Checks that the allowed roles against the matched role's name, since this eliminates case sensitivity issues + if(!rolelist.allowedroles.includes(oldrole.name)) + return send("That role isn't one you can remove yourself! If you need it removed, please ask a moderator!"); + // Use the matched name to check against the member's existing roles + if(!msg.member.roles.find('name', oldrole.name)) + return send("It seems you don't actually have the " + oldrole.name + "role! Mission accomplished!"); + + // Assuming all these factors succeed, add the role + msg.member.removeRole(oldrole).then(send(msg.member + " has been removed from the " + oldrole.name + " role!")); } }; exports.roles = { diff --git a/bot/modules/stats.js b/bot/modules/stats.js index 8dd774e..e30e447 100644 --- a/bot/modules/stats.js +++ b/bot/modules/stats.js @@ -1,107 +1,112 @@ let needle = require('needle'); let statsurl = 'https://coinmarketcap.com/currencies/library-credit/'; exports.commands = [ - 'stats' // command that is in this file, every command needs it own export as shown below + 'stats' // command that is in this file, every command needs it own export as shown below ]; exports.stats = { - usage: '', - description: 'Displays list of current Market stats', - process: function(bot, msg) { - needle.get('https://api.coinmarketcap.com/v2/ticker/1298/?convert=BTC', function(error, response) { - if (error || response.statusCode !== 200) { - msg.channel.send('coinmarketcap API is not available'); - } else { - let data = response.body.data; - let rank = data.rank; - let price_usd = Number(data.quotes.USD.price); - let price_btc = Number(data.quotes.BTC.price); - let market_cap_usd = Number(data.quotes.USD.market_cap); - let circulating_supply = Number(data.circulating_supply); - let total_supply = Number(data.total_supply); - let percent_change_1h = Number(data.quotes.USD.percent_change_1h); - let percent_change_24h = Number(data.quotes.USD.percent_change_24h); - let volume24_usd = Number(data.quotes.USD.volume_24h); - let dt = new Date(); - let timestamp = dt.toUTCString(); - let hr_indicator = ':thumbsup:'; - let day_indicator = ':thumbsup:'; - if (percent_change_1h < 0) { - hr_indicator = ':thumbsdown:'; - } - if (percent_change_24h < 0) { - day_indicator = ':thumbsdown:'; - } + usage: '', + description: 'Displays list of current Market stats', + process: function(bot, msg) { + needle.get('https://api.coingecko.com/api/v3/coins/markets?vs_currency=btc&ids=lbry-credits&order=market_cap_desc&per_page=100&page=1&sparkline=false&price_change_percentage=24h%2C1h%2C7d', function(error, response) { + if (error || response.statusCode !== 200) { + msg.channel.send('coingecko API is not available'); + } else { + let data = response.body.data; + let rank = data.market_cap_rank; + let price_btc = Number(data.current_price); + let market_cap_btc = Number(data.quotes.market_cap); + let circulating_supply = Number(data.circulating_supply); + let total_supply = Number(data.total_supply); + let percent_change_1h = Number(data.price_change_percentage_1h_in_currency); + let percent_change_24h = Number(data.price_change_percentage_24h_in_currency); + let volume24_btc = Number(data.total_volume); + let dt = new Date(); + let timestamp = dt.toUTCString(); + let hr_indicator = ':thumbsup:'; + let day_indicator = ':thumbsup:'; + if (percent_change_1h < 0) { + hr_indicator = ':thumbsdown:'; + } + if (percent_change_24h < 0) { + day_indicator = ':thumbsdown:'; + } - needle.get('https://api.coinmarketcap.com/v2/ticker/1298/?convert=GBP', function(error, response) { - if (error || response.statusCode !== 200) { - msg.channel.send('coinmarketcap API is not available'); - } else { - data = response.body.data; - let price_gbp = Number(data.quotes.GBP.price); - needle.get('https://api.coinmarketcap.com/v2/ticker/1298/?convert=EUR', function(error, response) { - if (error || response.statusCode !== 200) { - msg.channel.send('coinmarketcap API is not available'); - } else { - data = response.body.data; - let price_eur = Number(data.quotes.EUR.price); - let description = `**Rank: [${rank}](${statsurl})** -**Data** -Market Cap: [$${numberWithCommas(market_cap_usd)}](${statsurl}) -Total Supply: [${numberWithCommas(total_supply)} LBC](${statsurl}) -Circulating Supply: [${numberWithCommas(circulating_supply)} LBC](${statsurl}) -24 Hour Volume: [$${volume24_usd}](${statsurl}) + needle.get('https://api.coingecko.com/api/v3/coins/markets?vs_currency=gbp&ids=lbry-credits&order=market_cap_desc&per_page=100&page=1&sparkline=false', function (error, response) { + if (error || response.statusCode !== 200) { + msg.channel.send('coingecko API is not available'); + } else { + data = response.body.data; + let price_gbp = Number(data.current_price); + needle.get('https://api.coingecko.com/api/v3/coins/markets?vs_currency=eur&ids=lbry-credits&order=market_cap_desc&per_page=100&page=1&sparkline=false', function (error, response) { + if (error || response.statusCode !== 200) { + msg.channel.send('coingecko API is not available'); + } else { + data = response.body.data; + let price_eur = Number(data.current_price); + needle.get('https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=lbry-credits&order=market_cap_desc&per_page=100&page=1&sparkline=false', function (error, response) { + if (error || response.statusCode !== 200) { + msg.channel.send('coingecko API is not available'); + } else { + data = response.body.data; + let price_usd = Number(data.current_price); + let description = `**Rank: [${rank}](${statsurl})** + **Data** + Market Cap: [$${numberWithCommas(market_cap_usd)}](${statsurl}) + Total Supply: [${numberWithCommas(total_supply)} LBC](${statsurl}) + Circulating Supply: [${numberWithCommas(circulating_supply)} LBC](${statsurl}) + 24 Hour Volume: [$${volume24_usd}](${statsurl}) + + **Price** + BTC: [₿${price_btc.toFixed(8)}](${statsurl}) + USD: [$${price_usd.toFixed(2)}](${statsurl}) + EUR: [€${price_eur.toFixed(2)}](${statsurl}) + GBP: [£${price_gbp.toFixed(2)}](${statsurl}) + + **% Change** + 1 Hour: [${percent_change_1h}](${statsurl}) ${hr_indicator} + + 1 Day: [${percent_change_24h}](${statsurl}) ${day_indicator}`; + const embed = { + description: description, + color: 7976557, + footer: { + text: 'Last Updated: ' + timestamp + }, + author: { + name: 'Coin Market Cap Stats (LBC)', + url: statsurl, + icon_url: 'https://spee.ch/2/pinkylbryheart.png' + } + }; + msg.channel.send({embed}); + } + }); + } + }); + } + }); -**Price** -BTC: [₿${price_btc.toFixed(8)}](${statsurl}) -USD: [$${price_usd.toFixed(2)}](${statsurl}) -EUR: [€${price_eur.toFixed(2)}](${statsurl}) -GBP: [£${price_gbp.toFixed(2)}](${statsurl}) + function parse_obj(obj) { + let array = []; + let prop; + for (prop in obj) { + if (obj.hasOwnProperty(prop)) { + let key = parseInt(prop, 10); + let value = obj[prop]; + if (typeof value == 'object') { + value = parse_obj(value); + } + array[key] = value; + } + } + return array; + } -**% Change** -1 Hour: [${percent_change_1h}](${statsurl}) ${hr_indicator} - -1 Day: [${percent_change_24h}](${statsurl}) ${day_indicator} - -`; - const embed = { - description: description, - color: 7976557, - footer: { - text: 'Last Updated: ' + timestamp - }, - author: { - name: 'Coin Market Cap Stats (LBC)', - url: statsurl, - icon_url: 'https://spee.ch/2/pinkylbryheart.png' - } - }; - msg.channel.send({ embed }); - } - }); - } - }); - } - }); - - function parse_obj(obj) { - let array = []; - let prop; - for (prop in obj) { - if (obj.hasOwnProperty(prop)) { - let key = parseInt(prop, 10); - let value = obj[prop]; - if (typeof value == 'object') { - value = parse_obj(value); - } - array[key] = value; - } - } - return array; + function numberWithCommas(x) { + return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); + } + } + }) } - - function numberWithCommas(x) { - return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); - } - } -}; +}