-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
49 lines (39 loc) · 1.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require('dotenv').config() // Load .env file
const { enable_heroku } = require('./heroku.js');
const axios = require('axios')
const Discord = require('discord.js')
const client = new Discord.Client()
if ("HEROKU_DYNO_URL" in process.env) {
enable_heroku();
}
function getPrices() {
// API for price data.
axios.get(`https://api.coingecko.com/api/v3/coins/markets?vs_currency=${process.env.PREFERRED_CURRENCY}&ids=${process.env.COIN_ID}`).then(res => {
// If we got a valid response
if(res.data && res.data[0].current_price && res.data[0].price_change_percentage_24h) {
let currentPrice = res.data[0].current_price || 0 // Default to zero
let priceChange = res.data[0].price_change_percentage_24h || 0 // Default to zero
let symbol = res.data[0].symbol || '?'
client.user.setPresence({
game: {
// Example: "Watching -5,52% | BTC"
name: `${priceChange.toFixed(2)}% | ${symbol.toUpperCase()}`,
type: 3 // Use activity type 3 which is "Watching"
}
})
client.guilds.find(guild => guild.id === process.env.SERVER_ID).me.setNickname(`${process.env.CURRENCY_SYMBOL}${(currentPrice).toLocaleString().replace(/,/g,process.env.THOUSAND_SEPARATOR)}`)
console.log('Updated price to', currentPrice)
}
else
console.log('Could not load player count data for', process.env.COIN_ID)
}).catch(err => console.log('Error at api.coingecko.com data:', err))
}
// Runs when client connects to Discord.
client.on('ready', () => {
console.log('Logged in as', client.user.tag)
getPrices() // Ping server once on startup
// Ping the server and set the new status message every x minutes. (Minimum of 1 minute)
setInterval(getPrices, Math.max(1, process.env.MC_PING_FREQUENCY || 1) * 60 * 1000)
})
// Login to Discord
client.login(process.env.DISCORD_TOKEN)