Skip to content

Commit

Permalink
Added Support for Fetching Live CryptoCurrency Prices
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberboysumanjay committed Jun 25, 2021
1 parent a15ff04 commit 2a90ea5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
- **tr** - Translate Text
- **ud** - Urban Dictionary
- **sticker** - Make sticker from Image
- **crypto** - Fetch Latest Price of a CryptoCurrency

### Deploy :

Expand Down
18 changes: 17 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const ud = require('./modules/ud');
const gitinfo = require('./modules/git');
const cron = require('node-cron');
const cricket = require('./modules/cricket');
const crypto = require('./modules/crypto');

const client = new Client({ puppeteer: { headless: true, args: ['--no-sandbox'] }, session: config.session });

Expand Down Expand Up @@ -336,7 +337,8 @@ client.on('message_create', async (msg) => {
let critask = allricketschedules[msg.to];
critask.stop();
client.sendMessage(msg.to, `All running cricket updates of this chat has been stopped !`)
} else if (msg.body.startsWith("!spam ")) { // Spamming Op in the chat
}
else if (msg.body.startsWith("!spam ")) { // Spamming Op in the chat
msg.delete(true)
var i, count
if (msg.hasQuotedMsg) {
Expand Down Expand Up @@ -387,6 +389,20 @@ client.on('message_create', async (msg) => {
client.sendMessage(msg.to, text)
}
}
else if (msg.body.startsWith("!crypto ")) {
msg.delete(true)
var data = await crypto.getPrice(msg.body.replace("!crypto ", ""));
if (data == "error") {
client.sendMessage(msg.to, `🙇‍♂️ *Error*\n\n` + "```Something unexpected happened while fetching Cryptocurrency Price```")
}
if (data == "unsupported") {
client.sendMessage(msg.to, `🙇‍♂️ *Error*\n\n` + "```Support for this CryptoCurrency is not yet added```")
}
else {
var date = new Date().toLocaleString('en-US', {timeZone: 'Asia/Kolkata'})
client.sendMessage(msg.to, `Price of *${data.name}* as of ${date} is *₹ ${data.price}*`);
}
}
}
});

Expand Down
33 changes: 33 additions & 0 deletions modules/crypto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Coded by Sumanjay (https://github.com/cyberboysumanjay)
const axios = require('axios');

async function getPrice(cryptoCode) {
cryptoCode = cryptoCode.toUpperCase()
var mainconfig = {
method: 'get',
url: 'https://public.coindcx.com/market_data/current_prices'
}
return axios(mainconfig)
.then(async function (response) {
var data = response.data
var cryptoCodeINR = cryptoCode + "INR"
if (data.cryptoCode != undefined || data.cryptoCodeINR != undefined) {
cryptoCode = data.cryptoCode == undefined ? cryptoCodeINR : cryptoCode
var out = ({
name: cryptoCode,
price: data.cryptoCode
})
return out
} else {
return "unsupported"
}
})
.catch(function (error) {
return "error"
})
}


module.exports = {
getPrice
}

0 comments on commit 2a90ea5

Please sign in to comment.