-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
68 lines (61 loc) · 2 KB
/
server.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
const express = require('express')
const app = express()
const Discord = require('discord.js')
const client = new Discord.Client()
const firebase = require('firebase')
var firebaseConfig = {
apiKey: 'AIzaSyBm7onpz9n5IhpQnMldcY0PbKzJFOGoH-o',
authDomain: 'firet0y.firebaseapp.com',
databaseURL: 'https://firet0y.firebaseio.com',
projectId: 'firet0y',
storageBucket: 'firet0y.appspot.com',
messagingSenderId: '1047007358320',
appId: '1:1047007358320:web:8c697b8a6f26ee9852f3b5',
}
firebase.initializeApp(firebaseConfig)
const audienceApp = firebase.initializeApp(
{
apiKey: 'AIzaSyCRRP6g0vMhR194iGM9pzDQ-C17cSGPOjo',
authDomain: 'the-stupid-hackathon-thailand.firebaseapp.com',
databaseURL: 'https://the-stupid-hackathon-thailand.firebaseio.com',
projectId: 'the-stupid-hackathon-thailand',
storageBucket: 'the-stupid-hackathon-thailand.appspot.com',
messagingSenderId: '973484319476',
appId: '1:973484319476:web:a4e0a8bc80cfe5f86d74a1',
},
'audienceApp',
)
audienceApp.auth().signInWithEmailAndPassword(...process.env.AUDIENCE_APP_CREDENTIALS.split(':')).then(() => {
console.log('Authence app authenticated as', audienceApp.auth().currentUser.uid)
})
const storeRef = firebase
.database()
.ref(`data/${process.env.DATASTORE_TENANT_ID}`)
storeRef.on('value', store => {
console.log('Data store updated', new Date())
})
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`)
})
client.on('message', async msg => {
try {
const id = require.resolve('./handlers/message')
delete require.cache[id]
await require(id)(msg, { client, firebase, storeRef, audienceApp })
} catch (error) {
console.error(
'Cannot process message',
msg.content,
'from',
msg.author.tag,
'in',
msg.channel,
error,
)
}
})
app.use(express.static('public'))
const listener = app.listen(process.env.PORT, () => {
console.log('Your app is listening on port ' + listener.address().port)
})
client.login(process.env.DISCORD_TOKEN)