2024-02-07 23:21:53 +00:00
|
|
|
import { ChannelType, Events, PermissionFlagsBits } from 'discord.js';
|
2024-02-07 18:38:51 +00:00
|
|
|
import { VoiceChannel } from '../../database.js';
|
2024-01-28 17:33:22 +00:00
|
|
|
|
2024-02-07 23:21:53 +00:00
|
|
|
const vcPermissionOverwrites = [
|
2024-02-08 19:30:50 +00:00
|
|
|
PermissionFlagsBits.ReadMessageHistory,
|
|
|
|
PermissionFlagsBits.PrioritySpeaker,
|
|
|
|
PermissionFlagsBits.ManageMessages,
|
2024-02-07 23:21:53 +00:00
|
|
|
PermissionFlagsBits.ManageChannels,
|
2024-02-08 19:30:50 +00:00
|
|
|
PermissionFlagsBits.DeafenMembers,
|
2024-02-07 23:21:53 +00:00
|
|
|
PermissionFlagsBits.SendMessages,
|
2024-02-08 19:30:50 +00:00
|
|
|
PermissionFlagsBits.ViewChannel,
|
2024-02-07 23:21:53 +00:00
|
|
|
PermissionFlagsBits.MuteMembers,
|
|
|
|
PermissionFlagsBits.MoveMembers,
|
2024-02-08 19:30:50 +00:00
|
|
|
PermissionFlagsBits.Connect,
|
|
|
|
PermissionFlagsBits.Stream,
|
|
|
|
PermissionFlagsBits.UseVAD,
|
|
|
|
PermissionFlagsBits.Speak
|
2024-02-07 23:21:53 +00:00
|
|
|
];
|
|
|
|
|
2024-02-09 21:59:11 +00:00
|
|
|
const getChannel = async (member, guildChs, channel) => {
|
2024-02-07 19:04:16 +00:00
|
|
|
// Check database for existing channel
|
|
|
|
const ownCh = await VoiceChannel.findOne({
|
|
|
|
where: {
|
|
|
|
owner: member.user.id
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (ownCh !== null) {
|
2024-02-09 21:59:11 +00:00
|
|
|
return await guildChs.fetch(ownCh.id);
|
2024-02-07 19:04:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create private channel with all permissions
|
|
|
|
const name = member.user.username;
|
2024-02-07 21:02:02 +00:00
|
|
|
const chName = `${name}${name.toLowerCase().endsWith('s') ? "'" : "'s"} channel`;
|
2024-02-09 21:59:11 +00:00
|
|
|
const privCh = await guildChs.create({
|
2024-02-07 19:04:16 +00:00
|
|
|
name: chName,
|
2024-02-09 21:59:11 +00:00
|
|
|
parent: channel.parent,
|
2024-02-07 19:04:16 +00:00
|
|
|
type: ChannelType.GuildVoice,
|
|
|
|
permissionOverwrites: [
|
|
|
|
{
|
|
|
|
id: member.id,
|
2024-02-07 23:21:53 +00:00
|
|
|
allow: vcPermissionOverwrites
|
2024-02-07 19:04:16 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
// Save newly created channel
|
|
|
|
await VoiceChannel.create({
|
|
|
|
id: privCh.id,
|
|
|
|
owner: member.user.id
|
2024-02-08 20:11:56 +00:00
|
|
|
});
|
2024-02-07 19:04:16 +00:00
|
|
|
|
|
|
|
return privCh;
|
|
|
|
};
|
|
|
|
|
2024-02-08 17:12:10 +00:00
|
|
|
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;
|
|
|
|
|
2024-02-08 19:13:12 +00:00
|
|
|
// Find channel by id, return if not registered as custom
|
|
|
|
const custom = await VoiceChannel.findOne({
|
|
|
|
where: {
|
|
|
|
id: channel.id,
|
|
|
|
create: false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (custom === null) return;
|
|
|
|
|
2024-02-08 17:12:10 +00:00
|
|
|
// 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.`);
|
|
|
|
};
|
|
|
|
|
2024-01-28 17:33:22 +00:00
|
|
|
export const name = Events.VoiceStateUpdate;
|
2024-02-08 17:12:10 +00:00
|
|
|
export async function execute(oldState, newState) {
|
2024-02-09 21:59:11 +00:00
|
|
|
const { channel } = newState
|
2024-02-09 18:42:27 +00:00
|
|
|
await leftVoiceChat(oldState);
|
2024-02-09 21:59:11 +00:00
|
|
|
if (!channel) return;
|
2024-01-28 17:33:22 +00:00
|
|
|
|
2024-02-07 18:38:51 +00:00
|
|
|
// Find channel by id, return if not registered for customs
|
2024-02-07 19:04:16 +00:00
|
|
|
const createCh = await VoiceChannel.findOne({
|
2024-02-07 18:38:51 +00:00
|
|
|
where: {
|
2024-02-09 21:59:11 +00:00
|
|
|
id: channel.id,
|
2024-02-08 20:11:56 +00:00
|
|
|
create: true
|
2024-02-07 18:38:51 +00:00
|
|
|
}
|
|
|
|
});
|
2024-02-07 19:04:16 +00:00
|
|
|
if (createCh === null) return;
|
2024-02-07 18:38:51 +00:00
|
|
|
|
2024-02-07 16:40:29 +00:00
|
|
|
// Extract user data
|
2024-02-08 17:12:10 +00:00
|
|
|
const member = newState.member;
|
2024-01-28 17:33:22 +00:00
|
|
|
|
2024-02-07 16:40:29 +00:00
|
|
|
// Extract channel data
|
2024-02-08 17:12:10 +00:00
|
|
|
const channels = newState.guild.channels;
|
2024-02-07 19:04:16 +00:00
|
|
|
let step = 'create';
|
2024-02-07 16:40:29 +00:00
|
|
|
try {
|
2024-02-09 21:59:11 +00:00
|
|
|
const privCh = await getChannel(member, channels, channel);
|
2024-01-28 17:33:22 +00:00
|
|
|
|
2024-02-07 19:04:16 +00:00
|
|
|
step = 'move to';
|
2024-02-07 16:40:29 +00:00
|
|
|
// Move user to private channel
|
2024-02-08 17:12:10 +00:00
|
|
|
await newState.setChannel(privCh);
|
2024-02-07 16:40:29 +00:00
|
|
|
console.info(`[INFO] User '${name}' created private channel with ID ${privCh.id}.`);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
2024-02-07 19:04:16 +00:00
|
|
|
await member.send(`Failed to ${step} channel! Please contact server staff.`);
|
2024-02-07 16:40:29 +00:00
|
|
|
}
|
2024-01-28 17:33:22 +00:00
|
|
|
}
|