-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (39 loc) · 1.26 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
50
const dotenv = require('dotenv')
dotenv.config()
const { TOKEN } = process.env
const client = require('./src/discord-config')
const cron = require('node-cron')
// Imported functions
const getAPIData = require('./src/functions/getAPIData')
const createEmbed = require('./src/functions/createEmbed')
const getCountry = require('./src/functions/getCountry')
let result = ''
getAPIData().then((data) => {
result = data
})
// New call to API and update to result at the top of every hour
cron.schedule('* 0 * * * *', () => {
getAPIData().then((data) => {
result.length = 0
result = data
})
})
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`)
})
client.on('message', async (msg) => {
if (!msg.content.startsWith('!covid')) {
return
}
const countryIndex = await getCountry(result, msg)
const resultMessage = await createEmbed(result[countryIndex])
if (resultMessage === 'embedError') {
return msg.channel.send('Country not found. See <https://github.com/NathanDennis/Discord-Coronavirus-Stats> for specific search terms')
}
msg.channel.send(resultMessage)
})
client.login(TOKEN).then(() => {
console.log('Coronavirus bot logged in')
}).catch((error) => {
console.log(error)
})