This repository was archived by the owner on Dec 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (58 loc) · 1.91 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const fs = require('fs')
const Discord = require('discord.js')
const CoinGecko = require('coingecko-api')
const Redis = require('ioredis')
require('dotenv').config()
const { BOT_TOKEN, REDIS_URL } = process.env
const REDIS_COINS_HASH = 'coins'
const { prefix } = require('./config.json')
const discordClient = new Discord.Client()
const redis = new Redis(REDIS_URL)
const coinGeckoClient = new CoinGecko()
discordClient.commands = new Discord.Collection()
const commandFiles = fs
.readdirSync('./commands')
.filter(file => file.endsWith('.js'))
.filter(file => !file.includes('test'))
for (const file of commandFiles) {
const command = require(`./commands/${file}`)
discordClient.commands.set(command.name, command)
}
discordClient.once('ready', () => {
initialiseCoinsList()
console.log('ready')
})
const initialiseCoinsList = async () => {
const { data } = await coinGeckoClient.coins.list()
for (const coin of data) {
await redis.hset(REDIS_COINS_HASH, coin.symbol, coin.id)
}
}
discordClient.on('message', async message => {
if (!message.content.startsWith(prefix) || message.author.bot) return
const args = message.content.slice(prefix.length).split(/ +/)
const commandName = args.shift().toLowerCase()
const command =
discordClient.commands.get(commandName) ||
discordClient.commands.find(
cmd => cmd.aliases && cmd.aliases.includes(commandName)
)
if (!command) return
if (command.args && !args.length) {
let reply = 'Invalid number of arguments.'
if (command.usage) {
reply += `\nUsage: \`${prefix}${command.name} ${command.usage}\``
}
return message.channel.send(reply)
}
try {
command.execute(message, args)
} catch (err) {
console.error(err)
message.reply('There was an error executing the command.')
}
})
process.on('unhandledRejection', error =>
console.error('Uncaught Promise Rejection', error)
)
discordClient.login(BOT_TOKEN)