-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
97 lines (80 loc) · 3.43 KB
/
bot.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
const Discord = require('discord.js');
require('dotenv').config();
const { parse } = require('./commands/parser.js');
const { executer } = require('./commands/executer.js');
const getContext = require('./utils/getContext.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', async msg => {
// Ignore messages from bots. Stops recursive loops and miscalls.
if (msg.author.bot) return;
let command;
try {
command = parse(msg.content);
} catch (err) {
return err;
}
let response;
try {
command.args._ctx = getContext(msg);
response = await executer(command);
msg.channel.send(response);
} catch(err) {
if (err.name !== 'InvalidCommandError') {
msg.channel.send(`We couldn't quite execute your command :(`);
}
}
});
/**
* Handles Queue Mechanism. Watches Channels in 'Queue' Category and dynamically previsions 'Interview' Channels as the queue grows.
* TODO: Add Queue Pause / Play functionality using roles. If the bots role is set to queue-pause, the bot should pause. If the bots role is set to queue-play, the bot should play.
*/
client.on('voiceStateUpdate', async (oldVoiceState, newVoiceState) => {
const CATEGORIES = {
INTERVIEW_CATEGORY: 'Interviews',
QUEUE_CATEGORY: 'Queue'
}
const interviewChannelLimit = 5;
const requiredPlayersToPop = 2;
const server = newVoiceState.channel ? newVoiceState.channel.guild : oldVoiceState.channel.guild;
const interviewCategory = server.channels.cache.find(channel => channel.type === 'category' && channel.name === CATEGORIES.INTERVIEW_CATEGORY);
// Check if a player is joining a channel.
if (newVoiceState.channel) {
const category = newVoiceState.channel.parent;
// Check if they joined a queue and if there are enough players to intiate the pop.
if (category.name === CATEGORIES.QUEUE_CATEGORY && newVoiceState.channel.members.size >= requiredPlayersToPop) {
// Pop two players off the queue.
const poppedPlayers = newVoiceState.channel.members.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp).first(2);
// Get all the free interview channels from this server.
const freeInterviewChannels = server.channels.cache.filter(channel => channel.type === 'voice'
&& channel.parent.name === CATEGORIES.INTERVIEW_CATEGORY
&& channel.members.size === 0 ? true
: false
);
// Find or create a free work room for them to hang out in.
let targetRoom;
if (freeInterviewChannels.size > 0) {
targetRoom = freeInterviewChannels.array()[0];
} else if (interviewCategory.members.size < interviewChannelLimit) {
targetRoom = await server.channels.create(`Work Room ${interviewCategory.children.size+1}`, {type: 'voice', parent: interviewCategory, userLimit: 2});
}
// Migrate players to that work room.
poppedPlayers.forEach(player => {
if (!player.user.bot) player.voice.setChannel(targetRoom).catch(err => console.log(err));
});
}
}
// Check if a player is leaving a channel.
else if (oldVoiceState.channel) {
const category = oldVoiceState.channel.parent;
// If all the people leave the interview channel, remove it
if (category.name === CATEGORIES.INTERVIEW_CATEGORY && oldVoiceState.channel.members.size === 0) {
oldVoiceState.channel.delete();
}
}
});
module.exports = {
bot: client,
}