-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
167 lines (144 loc) · 5.44 KB
/
main.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Init Database Local
const NodeCache = require("node-cache");
const dbcache = new NodeCache();
// Init Discord Client
const Discord = require("discord.js");
const client = new Discord.Client();
const config = require("./config.json");
client.login(config.token);
// Init Number Randomizer
var MersenneTwister = require("mersenne-twister");
var generator = new MersenneTwister();
/* -------------------------------
Start Init functions
------------------------------- */
// Add match to cache
function dbCacheAddMatch(memberID, matchID){
dbcache.set(`match_${memberID}-${matchID}`, {matchs: true }, config.secondsMatchTimeout);
}
// Verify if match exist in cache
function dbCacheVerifyMatch(memberID, matchID){
var cache = dbcache.get(`match_${memberID}-${matchID}`);
if(cache){
return true;
} else {
cache = dbcache.get(`match_${matchID}-${memberID}`);
if(cache) {
return true;
} else {
return false;
}
}
}
// Create channel in Discord
function createChannel(guild, channel){
guild.channels.create("Vocal #" + Math.floor(generator.random()*10000), {
type: "voice",
userLimit: 2,
parent: config.categoryID,
permissionOverwrites: [
{
id: channel.guild.id,
deny: ["VIEW_CHANNEL"],
},
]
}).then( (vc) => {
for (const [, member] of channel.members) {
member.voice.setChannel(vc).catch( console.error );
}
}).catch( console.error);
}
// Check channels to have better match
function channelCheck(categoryChannels, waitChannel, waitChannelMember){
// If no Channel exist in Category, we will create it.
if(categoryChannels.size === 0){
createChannel(waitChannel.guild, waitChannel);
} else {
var n = 0; // Increment count
move:
for (const [, channel] of categoryChannels) {
n++;
if(channel.members.size === 1){
for (const [memberIDMove, memberMove] of waitChannelMember) {
for( const [memberID] of channel.members){
// Verify if the member will be moved and member already in channel have already matched
if(dbCacheVerifyMatch(memberID, memberIDMove)){
// Next Channel Check
continue;
} else {
dbCacheAddMatch(memberID, memberIDMove);
memberMove.voice.setChannel(channel).catch(console.error);
break move;
}
}
}
}
// If loop increment equal channels size in category, create channel
if(n === categoryChannels.size){
createChannel(waitChannel.guild, waitChannel);
}
}
}
}
// Verify if empty channel exist every 30s -> delete if true
function verifyVC(){
const channels = client.channels.cache.filter( (c) => c.parentID === config.categoryID && c.type === "voice");
for (const [, channel] of channels) {
if(channel.members.size === 0){
channel.delete("delete for new channels").catch( console.error);
}
}
setTimeout( () => {
verifyVC();
}, 30000);
}
/* -------------------------------
End Init functions
------------------------------- */
// On Client Ready, check empty and waiting channels
client.on("ready", async() => {
console.log(`√ Logged into Discord as ${client.user.username}!`);
client.user.setStatus("dnd");
client.user.setPresence({
status: "online",
activity: {
name: "his creator DamsDev.me",
type: "WATCHING"
}
});
// Check waiting channel
console.info("... Check waiting channel");
var waitChannel = client.channels.cache.get(config.waitChannelID);
const channels = client.channels.cache.filter( (c) => c.parentID === config.categoryID && c.type === "voice");
if(waitChannel.type === "voice" && waitChannel.members.size === 1 ){
channelCheck(channels, waitChannel, waitChannel.members);
} else {
for (const [memberID, member] of waitChannel.members) {
member.voice.setChannel(null).catch(console.error);
}
if(waitChannel.userLimit !== 1){
waitChannel.setUserLimit(1, "It's a requirement for the bot");
}
}
console.log("√ Waiting channels checked !");
// Check empty channels -> if empty, channel was deleted
console.info("... Check empty channels");
verifyVC();
console.log("√ Empty channels checked !");
});
// When user connect to channel, verify multiples things and move user
client.on("voiceStateUpdate", (oldState, newState) => {
if (newState.channel){
if ( (newState.channel.id === config.waitChannelID) && (newState.channel.members.size === 1)) {
const channels = client.channels.cache.filter( (c) => c.parentID === config.categoryID && c.type === "voice");
channelCheck(channels, newState.channel, newState.channel.members);
}
}
if(oldState.channel){
if(oldState.channel.name.startsWith("Vocal") && oldState.channel.parentID === config.categoryID){
if(oldState.channel.members.size === 0){
oldState.channel.delete("nobody in this channel").catch(console.error);
}
}
}
});