-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
151 lines (132 loc) · 4.88 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
141
142
143
144
145
146
147
148
149
150
151
require('dotenv').config()
const Discord = require('discord.js')
const client = new Discord.Client()
const GitHubApi = require('github')
const github = new GitHubApi()
const express = require('express')
const app = express()
/* WEB SERVER */
// set the port of our application
// process.env.PORT lets the port be set by Heroku
const port = process.env.PORT || 5000
app.get('/', function (req, res) {
res.redirect('https://github.com/lohs-software-club/locs-discord')
})
app.listen(port, () => {
// will echo 'Our app is running on http://localhost:5000 when run locally'
console.log('App is running on port: ' + port)
})
/**
* Checks any incoming message to see if it starts
* with the chosen prefix
* @param {string} message The incoming message
* @returns null if message doesn't start with prefix, message without prefix otherwise.
*/
const checkMessage = (message) => {
return (message.startsWith('.'))
? message.slice(1).split(' ')
: null
}
const replyToMessage = (reply, userMessage) => {
userMessage.reply(reply)
.then((message) => message.delete(7500))
userMessage.delete()
}
client.on('ready', () => {
console.log('Up and running!')
})
client.on('message', message => {
let checkedMessage = checkMessage(message.content)
if (checkedMessage) {
switch (checkedMessage[0]) {
case 'ping': {
replyToMessage('pong!', message)
break
} case 'subscribe': {
if (message.channel.name === 'bot-spam') {
let roles = message.guild.roles
checkedMessage.shift() // Removing first element from array (the command)
let requestedRoles = checkedMessage
let user = message.member
let addedRoles = []
roles.forEach((role) => {
for (var i = 0, len = requestedRoles.length; i < len; i++) {
// Checking against all roles and making sure it isn't a member/admin role.
if (role.name.slice(0, -1).toLowerCase() === requestedRoles[i].toLowerCase() && role.name.substr(-1) === '*') {
addedRoles.push(role)
}
}
})
if (addedRoles.length > 0) {
user.addRoles(addedRoles, 'Requested via CS Bot.')
replyToMessage('Added roles: \n' + addedRoles.map(role => role.name).join('\n'), message)
} else {
replyToMessage('No acceptable roles found. Please double-check spelling!', message)
}
} else {
replyToMessage('Please use #bot-spam', message)
}
break
} case 'unsubscribe': {
if (message.channel.name === 'bot-spam') {
let roles = message.guild.roles
checkedMessage.shift() // Removing first element from array (the command)
let requestedRoles = checkedMessage
let user = message.member
let removedRoles = []
roles.forEach((role) => {
for (var i = 0, len = requestedRoles.length; i < len; i++) {
// Checking against all roles and making sure it isn't a member/admin role.
if (role.name.slice(0, -1).toLowerCase() === requestedRoles[i].toLowerCase() && role.name.substr(-1) === '*') {
removedRoles.push(role)
}
}
})
if (removedRoles.length > 0) {
user.addRoles(removedRoles, 'Requested via CS Bot.')
replyToMessage('Removed roles: \n' + removedRoles.map(role => role.name.slice(0, -1)).join('\n'), message)
} else {
replyToMessage('No acceptable roles found. Please double-check spelling!', message)
}
} else {
replyToMessage('Please use #bot-spam', message)
}
break
} case 'list': {
if (message.channel.name === 'bot-spam') {
let roles = message.guild.roles
let acceptableRoles = []
roles.forEach((role) => {
if (role.name.substr(-1) === '*') { acceptableRoles.push(role.name.slice(0, -1)) }
})
replyToMessage('Acceptable roles: \n' + acceptableRoles.join('\n'), message)
} else {
replyToMessage('Please use #bot-spam', message)
}
break
} case 'github': {
if (message.channel.name === 'bot-spam') {
github.authenticate({
type: 'token',
token: process.env.GITHUB_TOKEN
})
let user = checkedMessage[1]
github.orgs.addOrgMembership({
org: 'lohs-software-club',
username: user,
role: 'member'
})
.then(() => { replyToMessage('GitHub user ' + user + ' added to the org!', message) })
.catch((err) => {
console.log(err)
replyToMessage('Something went wrong: ' + err, message)
})
} else {
replyToMessage('Please use #bot-spam', message)
}
break
}
}
}
})
client.login(process.env.DISCORD_TOKEN)