Skip to content

Commit

Permalink
Merge pull request hmes98318#7 from Darkzx0/main
Browse files Browse the repository at this point in the history
feat: Add autoLeaveCooldown
  • Loading branch information
hmes98318 authored Nov 27, 2022
2 parents 22e94ec + 3f136e9 commit 9fc6b78
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ TOKEN = "your_token"
"defaultVolume": 50,
"maxVolume": 100,
"autoLeave": true,
"autoLeaveCooldown": 1000,
"displayVoiceState": true,
"port": 33333
}
Expand Down
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"defaultVolume": 50,
"maxVolume": 100,
"autoLeave": true,
"autoLeaveCooldown": 1000,
"displayVoiceState": true,
"port": 33333
}
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ client.config.playing = process.env.PLAYING || config.playing;
client.config.defaultVolume = Number(process.env.DEFAULTVOLUME || config.defaultVolume);
client.config.maxVolume = Number(process.env.MAXVOLUME || config.maxVolume);
client.config.autoLeave = process.env.AUTO_LEAVE === 'true' ? true : false || config.autoLeave;
client.config.autoLeaveCooldown = Number(process.env.AUTO_LEAVE_COOLDOWN || config.autoLeaveCooldown);
client.config.displayVoiceState = process.env.DISPLAY_VOICE_STATE === 'true' ? true : false || config.displayVoiceState;
client.config.port = process.env.PORT || config.port;

Expand Down
2 changes: 2 additions & 0 deletions src/commands/play.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ module.exports = {
const queue = await client.player.createQueue(message.guild, {
metadata: message.channel,
leaveOnEnd: client.config.autoLeave,
leaveOnEndCooldown: client.config.autoLeaveCooldown,
leaveOnStop: client.config.autoLeave,
leaveOnEmpty: client.config.autoLeave,
leaveOnEmptyCooldown: client.config.autoLeaveCooldown,
initialVolume: client.config.defaultVolume,
ytdlOptions: client.config.ytdlOptions
});
Expand Down
2 changes: 2 additions & 0 deletions src/commands/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ module.exports = {
const queue = await client.player.createQueue(message.guild, {
metadata: message.channel,
leaveOnEnd: client.config.autoLeave,
leaveOnEndCooldown: client.config.autoLeaveCooldown,
leaveOnStop: client.config.autoLeave,
leaveOnEmpty: client.config.autoLeave,
leaveOnEmptyCooldown: client.config.autoLeaveCooldown,
initialVolume: client.config.defaultVolume,
ytdlOptions: client.config.ytdlOptions
});
Expand Down
39 changes: 35 additions & 4 deletions src/events/voiceStateUpdate.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
let pool = [];

module.exports = async (client, oldState, newState) => {
const display = client.config.displayVoiceState;
const color = { white: '\x1B[0m', grey: '\x1B[2m' };
Expand All @@ -12,14 +14,43 @@ module.exports = async (client, oldState, newState) => {
if (display) console.log(`${color.grey}-- ${newState.member.user.username} moved channel ${oldState.channel.name} to ${newState.channel.name}${color.white}`);


if (!oldState.member.user.bot) { // If the member who left the channel is not bot, check if the channel still has members
// If the member who left the channel is not bot, check if the channel still has members
if (!oldState.member.user.bot) {
const queue = await client.player.getQueue(oldState.guild.id);
const botChannelId = queue?.connection?.channel?.id;
const oldStateId = oldState.channel.id;
const oldChannelId = oldState.channel.id;
const newChannelId = newState.channel.id;

if (botChannelId === oldChannelId) {

if (botChannelId === oldStateId) {
// If the channel only has bot, then start counting timeout until leave
if (oldState.channel.members.size <= 1) {
client.player.deleteQueue(oldState.guild.id);

let timeoutID = setTimeout(() => {
client.player.deleteQueue(oldState.guild.id);
}, client.config.autoLeaveCooldown);

pool.push({
guildId: oldState.guild.id,
timeoutId: timeoutID
});
//console.log(pool);
}
}
else if (botChannelId === newChannelId) {

// When bot is in the target channel and only one member joined
// If there are two members or more (not include bot) in the channel, it will not trigger
if (newState.channel.members.size > 1 && newState.channel.members.size <= 2) {

// If member join bot channel, then find current channel's timeoutID to clear
for (var i = 0; i < pool.length; i++) {
//console.log(pool[i]);
if (pool[i].guildId === newState.guild.id) {
clearTimeout(pool[i].timeoutId);
pool.splice(i, 1);
}
}
}
}
}
Expand Down

0 comments on commit 9fc6b78

Please sign in to comment.