delete empty custom vc
This commit is contained in:
parent
ea54565c6a
commit
76641b31c7
@ -53,31 +53,47 @@ const getChannel = async (member, channels) => {
|
|||||||
return privCh;
|
return privCh;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const leftVoiceChat = async (state) => {
|
||||||
|
const { channel } = state;
|
||||||
|
|
||||||
|
// Isn't this always false?
|
||||||
|
if (!channel) return;
|
||||||
|
|
||||||
|
// Get active members from channel
|
||||||
|
const members = Array.from(channel.members);
|
||||||
|
if (members.length > 0) return;
|
||||||
|
|
||||||
|
// Delete channel from guild
|
||||||
|
await channel.guild.channels.delete(channel.id);
|
||||||
|
console.info(`[INFO] Custom VC with ID '${channel.id}' was empty and got deleted.`);
|
||||||
|
};
|
||||||
|
|
||||||
export const name = Events.VoiceStateUpdate;
|
export const name = Events.VoiceStateUpdate;
|
||||||
export async function execute(_, state) {
|
export async function execute(oldState, newState) {
|
||||||
if (!state.channel) return;
|
if (!newState.channel)
|
||||||
|
return await leftVoiceChat(oldState);
|
||||||
|
|
||||||
// Find channel by id, return if not registered for customs
|
// Find channel by id, return if not registered for customs
|
||||||
const createCh = await VoiceChannel.findOne({
|
const createCh = await VoiceChannel.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: state.channel.id,
|
id: newState.channel.id,
|
||||||
create: true,
|
create: true,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (createCh === null) return;
|
if (createCh === null) return;
|
||||||
|
|
||||||
// Extract user data
|
// Extract user data
|
||||||
const member = state.member;
|
const member = newState.member;
|
||||||
|
|
||||||
// Extract channel data
|
// Extract channel data
|
||||||
const channels = state.guild.channels;
|
const channels = newState.guild.channels;
|
||||||
let step = 'create';
|
let step = 'create';
|
||||||
try {
|
try {
|
||||||
const privCh = await getChannel(member, channels);
|
const privCh = await getChannel(member, channels);
|
||||||
|
|
||||||
step = 'move to';
|
step = 'move to';
|
||||||
// Move user to private channel
|
// Move user to private channel
|
||||||
await state.setChannel(privCh);
|
await newState.setChannel(privCh);
|
||||||
console.info(`[INFO] User '${name}' created private channel with ID ${privCh.id}.`);
|
console.info(`[INFO] User '${name}' created private channel with ID ${privCh.id}.`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
Loading…
Reference in New Issue
Block a user