-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
198 lines (162 loc) · 8.25 KB
/
Copy pathindex.js
File metadata and controls
198 lines (162 loc) · 8.25 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const http = require('http')
const port = process.env.PORT || 8080
const Discord = require('discord.js')
const client = new Discord.Client()
const token = process.env.TOKEN
const prefix = '!'
const offLimits = [ "@EVERYONE", "ADMIN", "ADMINISTRATOR", "ADMINISTRAITOR", "MAXBOT", "INTERROGATION", "QUARANTINE" ]
let privateChannels = new Map()
let roleSetup = new Map()
client.once('ready', () => {
console.log('MaxBot connected and ready to go!')
})
client.on('message', async msg => {
const author = msg.author
const authorID = author.id
const content = msg.content
// Request sent in #commands
if (msg.channel.id === '767194969605537792' || msg.channel.id === '786836345263226910' || msg.channel.id == '873737969910030417') {
// A setup process is already registered
if (roleSetup.get(authorID) !== undefined) {
const timeDifference = Math.floor((Date.now() - roleSetup.get(authorID).startTime) / 1000)
// 5 minutes since the first request
if (timeDifference <= 300) {
if (content.toUpperCase() == 'stop'.toUpperCase() || content.toUpperCase() == 'exit'.toUpperCase() || content.toUpperCase() == 'cancel'.toUpperCase()) {
roleSetup.delete(authorID)
msg.channel.send(author.toString() + " Gotcha, cancelled your setup. Come back soon!")
} else if (roleSetup.get(authorID).stage == 1) {
if (offLimits.includes(content.toUpperCase())) {
msg.channel.send(author.toString() + " Hold your horses! That role is off limits. Try again with another name.")
return
}
roleSetup.get(authorID).name = content
roleSetup.get(authorID).stage = 2
msg.channel.send(author.toString() + " '" + content + '\', got it. What color would you like it to be?')
} else if (roleSetup.get(authorID).stage == 2) {
if (Discord.Util.resolveColor(content.toUpperCase()).toString() == "NaN") {
msg.channel.send(author.toString() + " Invalid color. Try again!")
return
}
roleSetup.get(authorID).color = content
msg.channel.guild.roles.create({
data: {
name: roleSetup.get(authorID).name,
color: roleSetup.get(authorID).color.toUpperCase(),
}
})
.then(console.log("Sucessfully created vanity role!")).catch(console.error)
.then(newRole => msg.member.roles.add(newRole)).catch(console.error)
msg.channel.send(author.toString() + ' Thanks! Creating role ' + roleSetup.get(authorID).name + ' with the color ' + roleSetup.get(authorID).color + ' now.')
roleSetup.delete(authorID)
}
} else {
roleSetup.delete(authorID)
}
}
// Someone ran ! prefix
if (msg.content.startsWith(prefix)) {
const args = msg.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
// Someone ran !vanity and isn't registered
if (command === 'vanity') {
let roleSize = 0
await msg.member.roles.cache.filter(role => !offLimits.includes(role.name.toUpperCase())).tap(roles => roleSize = roles.size)
if (roleSize < 1) {
if (roleSetup.get(authorID) === undefined) {
msg.channel.send(author.toString() + ' What do you want your vanity role to be named?')
roleSetup.set(authorID, {
startTime: Date.now(),
stage: 1
})
console.log("Initialized vanity setup for " + msg.member.nickname + ".")
}
} else {
if (msg.content.toLowerCase() === '!vanity remove') {
msg.channel.send(author.toString() + ' Removing your role.')
console.log("Removing vanity role for " + msg.member.nickname + ".")
msg.member.roles.cache.filter(role => !offLimits.includes(role.name.toUpperCase())).forEach(role =>
role.delete()
)
} else {
msg.channel.send(author.toString() + ' Type \'!vanity remove\' to remove your role.')
}
}
}
}
}
})
client.on('voiceStateUpdate', async (oldState, newState) => {
const oldChannel = oldState.channel
const newChannel = newState.channel
// User joins a channel
if (newChannel !== null) {
const player = newState.member
const guild = newState.guild
const channel = newChannel
// User joins the 'Join to Create' channel
if (channel.name == "Join to Create") {
// Generate a new private channel
let privateChannel = await guild.channels.create(player.displayName + "'s channel", {
type: 'voice',
parent: channel.parentID,
permissionOverwrites: [
{
id: player.id,
allow: ['MANAGE_CHANNELS']
},
]
}).then(console.log("New room created for: " + player.displayName + "(" + player.id + ")")).catch(console.error)
// Move the player to the private channel and add it to database
await player.voice.setChannel(privateChannel.id)
privateChannels.set(privateChannel.id, [ player.id ])
}
//User joins an existing room
if (privateChannels.has(channel.id)) {
// If player isn't already in the room add them
if (!privateChannels.get(channel.id).includes(player.id)) {
privateChannels.get(channel.id).push(player.id)
console.log("Added " + player.displayName + " to room " + channel.id)
}
}
}
// User leaves a channel - The old channel exists and either the new channel doesn't exist(they left) or the new channel is different(they switched channels)
if (oldChannel !== null && (newChannel === null || oldChannel !== newChannel)) {
const player = oldState.member
const channel = oldChannel
// User leaves a channel room with 0 members - delete it
if (privateChannels.has(channel.id) && channel.members.size === 0) {
console.log("Deleted room " + channel.id + " due to " + player.displayName + " leaving")
privateChannels.delete(channel.id)
await channel.delete()
}
// User leaves a channel room with more than 0 members - see if we can transfer ownership
if (privateChannels.has(channel.id)) {
const oldAdminID = privateChannels.get(channel.id)[0]
const index = privateChannels.get(channel.id).indexOf(player.id)
if (index > -1) {
privateChannels.get(channel.id).splice(index, 1);
}
console.log(player.displayName + " left a room")
const newAdminID = privateChannels.get(channel.id)[0]
if (newAdminID !== oldAdminID) {
const newAdmin = await oldState.guild.members.cache.get(newAdminID)
console.log("Because " + player.displayName + " left a room they owned, " + newAdmin.displayName + " will take over")
await channel.permissionOverwrites.get(oldAdminID).delete();
await channel.overwritePermissions([
{
id: newAdminID,
allow: ['MANAGE_CHANNELS']
}
]);
await channel.setName(newAdmin.displayName + "'s channel");
}
}
}
})
client.login(token)
http.createServer((req, res) => {
res.writeHead(404);
res.end('Beep boop.');
}).listen(port, '0.0.0.0', () => {
console.log('Health checks open on port ' + port + '.')
})