Skip to content

Commit

Permalink
feat: configurable leaveOnEmpty status + cooldown
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirasaki committed Jun 13, 2023
1 parent a8f3095 commit dbad3d0
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 7 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,15 @@ All configuration is done in `/config.js`. Multiple Discord servers are supporte
// Default: 2 minutes
defaultLeaveOnEndCooldown: 120,

// Should the bot leave the voice-channel if there's no other members
defaultLeaveOnEmpty: true,

// Time amount of seconds to stay in the voice channel
// when channel is empty/no other members aside from bot
// Only active when leaveOnEmpty is true
// Default: 2 minutes
defaultLeaveOnEmptyCooldown: 120,

// When true, will create a thread when the voice session is first initialized
// and continue to send music/queue events in that thread instead of flooding
// the channel
Expand Down
9 changes: 9 additions & 0 deletions config.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ const config = {
// Default: 2 minutes
defaultLeaveOnEndCooldown: 120,

// Should the bot leave the voice-channel if there's no other members
defaultLeaveOnEmpty: true,

// Time amount of seconds to stay in the voice channel
// when channel is empty/no other members aside from bot
// Only active when leaveOnEmpty is true
// Default: 2 minutes
defaultLeaveOnEmptyCooldown: 120,

// When true, will create a thread when the voice session is first initialized
// and continue to send music/queue events in that thread instead of flooding
// the channel
Expand Down
64 changes: 64 additions & 0 deletions src/commands/music-admin/leave-on-empty-cooldown.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const { ApplicationCommandOptionType } = require('discord.js');
const { ChatInputCommand } = require('../../classes/Commands');
const { requireSessionConditions } = require('../../modules/music');
const {
getGuildSettings, db, saveDb
} = require('../../modules/db');
const { clientConfig, msToHumanReadableTime } = require('../../util');
const { MS_IN_ONE_SECOND } = require('../../constants');

module.exports = new ChatInputCommand({
global: true,
permLevel: 'Administrator',
data: {
description: 'Change the amount of seconds to wait before leaving when channel is empty',
options: [
{
name: 'seconds',
description: 'The amount of seconds to wait',
type: ApplicationCommandOptionType.Integer,
min_value: 1,
max_value: Number.MAX_SAFE_INTEGER,
required: false
},
{
name: 'status',
description: 'Enable/disable leave-on-empty, uses a lot more bandwidth when disabled',
type: ApplicationCommandOptionType.Boolean,
required: false
}
]
},
run: async (client, interaction) => {
const { emojis } = client.container;
const { member, guild } = interaction;
const seconds = interaction.options.getInteger('seconds');
const status = interaction.options.getBoolean('status') ?? true;
const guilds = db.getCollection('guilds');

// Check conditions/state
if (!requireSessionConditions(interaction, false)) return;

// Resolve settings
const settings = getGuildSettings(guild.id);
if (!seconds) {
interaction.reply(`${ emojis.success } ${ member }, the amount of seconds to wait before leaving when channel is empty is currently set to **\`${ settings.leaveOnEmptyCooldown ?? clientConfig.defaultLeaveOnEmptyCooldown }\`**`);
return;
}

try {
// Perform and notify collection that the document has changed
settings.leaveOnEmpty = status;
settings.leaveOnEmptyCooldown = seconds;
guilds.update(settings);
saveDb();

// Feedback
if (status !== true) interaction.reply({ content: `${ emojis.success } ${ member }, leave-on-empty has been disabled` });
else interaction.reply({ content: `${ emojis.success } ${ member }, leave-on-empty cooldown set to \`${ msToHumanReadableTime(seconds * MS_IN_ONE_SECOND) }\`\nThis change will take effect the next time playback is initialized` });
}
catch (e) {
interaction.reply(`${ emojis.error } ${ member }, something went wrong:\n\n${ e.message }`);
}
}
});
5 changes: 4 additions & 1 deletion src/commands/music/play.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ module.exports = new ChatInputCommand({
// and adjust on every song end isn't perfect
volume = Math.min(100, volume);

// Resolve leave on end cooldown
// Resolve cooldown
const leaveOnEndCooldown = ((settings.leaveOnEndCooldown ?? 2) * MS_IN_ONE_SECOND);
const leaveOnEmptyCooldown = ((settings.leaveOnEmptyCooldown ?? 2) * MS_IN_ONE_SECOND);

// nodeOptions are the options for guild node (aka your queue in simple word)
// we can access this metadata object using queue.metadata later on
Expand All @@ -95,6 +96,8 @@ module.exports = new ChatInputCommand({
nodeOptions: {
leaveOnEnd: true,
leaveOnEndCooldown,
leaveOnEmpty: settings.leaveOnEmpty,
leaveOnEmptyCooldown,
volume,
metadata: {
channel: eventChannel,
Expand Down
3 changes: 3 additions & 0 deletions src/interactions/buttons/play-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module.exports = new ComponentCommand({ run: async (client, interaction) => {

// Resolve leave on end cooldown
const leaveOnEndCooldown = ((settings.leaveOnEndCooldown ?? 2) * MS_IN_ONE_SECOND);
const leaveOnEmptyCooldown = ((settings.leaveOnEmptyCooldown ?? 2) * MS_IN_ONE_SECOND);

// nodeOptions are the options for guild node (aka your queue in simple word)
// we can access this metadata object using queue.metadata later on
Expand All @@ -63,6 +64,8 @@ module.exports = new ComponentCommand({ run: async (client, interaction) => {
nodeOptions: {
leaveOnEnd: true,
leaveOnEndCooldown,
leaveOnEmpty: settings.leaveOnEmpty,
leaveOnEmptyCooldown,
volume,
metadata: {
channel: eventChannel,
Expand Down
14 changes: 14 additions & 0 deletions src/modules/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ const getGuildSettings = (guildId) => {
useThreadSessions: clientConfig.defaultUseThreadSessions,
threadSessionStrictCommandChannel: clientConfig.defaultThreadSessionStrictCommandChannel,
leaveOnEndCooldown: clientConfig.defaultLeaveOnEndCooldown,
leaveOnEmpty: clientConfig.defaultLeaveOnEmpty,
leaveOnEmptyCooldown: clientConfig.defaultLeaveOnEmptyCooldown,
djRoleIds: [],
equalizer: 'null'
});
Expand Down Expand Up @@ -111,6 +113,18 @@ const getGuildSettings = (guildId) => {
saveDb();
}

if (typeof settings.leaveOnEmpty === 'undefined') {
settings.leaveOnEmpty = clientConfig.defaultLeaveOnEmpty;
guilds.update(settings);
saveDb();
}

if (typeof settings.leaveOnEmptyCooldown === 'undefined') {
settings.leaveOnEmptyCooldown = clientConfig.defaultLeaveOnEmptyCooldown;
guilds.update(settings);
saveDb();
}

if (process.env.NODE_ENV !== 'production') console.dir(settings);
return settings;
};
Expand Down
18 changes: 12 additions & 6 deletions src/music-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,21 @@ module.exports = (player) => {
});

player.events.on('emptyChannel', (queue) => {
// Emitted when the voice channel has been empty for the set threshold
// Bot will automatically leave the voice channel with this event
queue.metadata.channel.send({ embeds: [
const settings = getGuildSettings(queue.guild.id);
if (!settings) return;
const ctx = { embeds: [
{
color: colorResolver(),
title: 'Channel Empty',
description: 'Leaving channel because it has been empty for the past 5 minutes'
title: 'Channel Empty'
}
] });
] };
if (!settings.leaveOnEmpty) ctx.embeds[0].description = 'Staying in channel as leaveOnEnd is disabled';
else ctx.embeds[0].description = `Leaving empty channel in ${ msToHumanReadableTime(
(settings.leaveOnEmptyCooldown ?? clientConfig.defaultLeaveOnEndCooldown) * MS_IN_ONE_SECOND
) }`;
// Emitted when the voice channel has been empty for the set threshold
// Bot will automatically leave the voice channel with this event
queue.metadata.channel.send(ctx);
});

player.events.on('emptyQueue', (queue) => {
Expand Down

0 comments on commit dbad3d0

Please sign in to comment.