forked from jacobhilty926/mtgbot2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
96 lines (84 loc) · 2.33 KB
/
helpers.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
const fetch = require('node-fetch')
const stringify = require('json-stringify')
const slackBaseUrl = process.env.NODE_ENV === 'production'
? 'https://slack.com/api/'
: 'http://2d11cc07.ngrok.io/'
const makeChannelSender = channel => message => {
const headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + process.env.OAUTH_TOKEN
}
fetch(slackBaseUrl + 'chat.postMessage', {
method: 'POST',
headers: headers,
body: stringify({
channel: channel,
text: message
})
}).then(response => response.json())
.then(json => { console.log(json) })
.catch(ex => {
console.log('Err: ' + ex)
})
}
const getUserList = function () {
const headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + process.env.OAUTH_TOKEN
}
return fetch(slackBaseUrl + 'users.list', {
method: 'GET',
headers: headers
}).then(response => response.json())
.then(json => {
return json
})
}
const makeDMSender = (values, player) => {
const data = values[1]['members']
const message = values[0]
console.log('Sending message to ' + player)
const userId = getIdByName(data, player)
console.log('User: ' + player + ' Id: ' + userId)
const headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + process.env.OAUTH_TOKEN
}
fetch(slackBaseUrl + 'chat.postMessage', {
method: 'POST',
headers: headers,
body: stringify({
channel: userId,
text: message,
as_user: false
})
}).then(response => response.json())
.then(json => { console.log(json) })
.catch(ex => {
console.log('Err: ' + ex)
})
}
const getIdByName = (memberList, name) => {
const result = memberList.filter(member => {
if (member.name === 'SeriousMTGBot') {
console.log(member)
}
return member['name'] === name
})
if (result) {
console.log('Found user')
} else {
console.log('User not found')
}
return result ? result[0].id : null
}
const helpers = {
makeChannelSender: makeChannelSender,
makeDMSender: makeDMSender,
getUserList: getUserList,
// TODO: Handle !define <trample etc.>, likely before the card pattern
lookupRule: function (text, channel) {
makeChannelSender(channel)('Rules lookup not implemented yet, sorry friends')
}
}
module.exports = helpers