forked from abrkn/shilly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (117 loc) · 3.68 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const assert = require('assert');
const superagent = require('superagent');
const Discord = require('discord.js');
const delay = require('delay');
const numeral = require('numeral');
const Xray = require('x-ray');
const { shuffle, isNumber } = require('lodash');
const {
formatNumber: n,
printError,
randomIntFromInterval,
} = require('./utils');
const bluebird = require('bluebird');
const redis = require('redis');
const { RichEmbed } = Discord;
const monitorYours = require('./monitorYours');
const monitorTether = require('./monitorTether');
const createTipping = require('./tipping');
const createRaffle = require('./raffle');
const router = require('./router');
const createWelcome = require('./welcome');
const createBitcoinRpc = require('./bitcoinRpc');
bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);
const {
DISCORD_TOKEN,
PRICE_INTERVAL = 10 * 60e3,
DISCORD_CHANNEL_ID,
REDIS_URL = 'redis://localhost',
RAFFLE_INTERVAL = 24 * 60 * 60e3,
DISCORD_YOURS_ORG_CHANNEL_ID,
BITCOIND_URL,
COMMAND_PREFIX = '!',
DISCORD_STAFF_ROLE_ID,
} = process.env;
assert(DISCORD_TOKEN, 'DISCORD_TOKEN');
assert(DISCORD_CHANNEL_ID, 'DISCORD_CHANNEL_ID');
assert(DISCORD_YOURS_ORG_CHANNEL_ID, 'DISCORD_YOURS_ORG_CHANNEL_ID');
assert(REDIS_URL, 'REDIS_URL');
assert(DISCORD_STAFF_ROLE_ID, 'DISCORD_STAFF_ROLE_ID');
const redisClient = redis.createClient(REDIS_URL);
(async () => {
const client = new Discord.Client();
client.login(DISCORD_TOKEN);
console.log('Waiting for Discord to connect...');
// Wait for connect
await new Promise(resolve => client.on('ready', resolve));
console.log('Discord connected');
const { fetchRpc, lockBitcoind } = createBitcoinRpc({
redisClient,
bitcoindUrl: BITCOIND_URL,
});
const tipping = createTipping({
say: _ => channel.send(_),
fetchRpc,
lockBitcoind,
});
const welcome = createWelcome({ client });
const channel = client.channels.get(DISCORD_CHANNEL_ID);
assert(channel, `Channel ${DISCORD_CHANNEL_ID} not found`);
const raffle = createRaffle({
interval: +RAFFLE_INTERVAL,
redisClient,
fetchBitcoinRpc: fetchRpc,
say: _ => channel.send(_),
client,
tipping,
channel,
});
const yoursChannel = client.channels.get(DISCORD_YOURS_ORG_CHANNEL_ID);
assert(yoursChannel, `Channel ${DISCORD_YOURS_ORG_CHANNEL_ID} not found`);
client.on('message', message =>
(async () => {
// Cheap fix to not react to own messages
if (message.author.bot) {
return;
}
console.log(message.content);
const words = message.content.split(/ /g).filter(_ => _);
const [firstWord, ...otherWords] = words;
if (!firstWord) {
return;
}
const commandMatch = firstWord
.toLowerCase()
.match(`^${COMMAND_PREFIX}([a-z0-9]+)$`);
if (!commandMatch) {
return;
}
const [, command] = commandMatch;
await router.handle({
command,
params: otherWords,
message,
reply: message.reply.bind(message),
say: message.channel.send.bind(message.channel),
tipping,
isDm: message.channel.type === 'dm',
recipient: message.channel.recipient,
client,
guild: channel.guild,
authorIsStaff: !!channel.guild.members
.get(message.author.id)
.roles.get(DISCORD_STAFF_ROLE_ID),
fetchBitcoinRpc: fetchRpc,
lockBitcoind,
});
})().catch(printError)
);
await Promise.all([
raffle(),
monitorYours({ redisClient, say: _ => yoursChannel.send(_) }),
monitorTether({ redisClient, say: _ => channel.send(_) }),
tipping(),
welcome(),
]);
})().then(_ => _);