const { Events, PermissionFlagsBits } = require('discord.js');
const voiceChannelId = '1284713593056661618'; // Your target voice channel ID
client.on(Events.GuildScheduledEventUpdate, async (oldEvent, newEvent) => {
const guild = newEvent.guild;
const voiceChannel = guild.channels.cache.get(voiceChannelId);
if (!voiceChannel) {
console.error(`Voice channel with ID ${voiceChannelId} not found.`);
return;
}
// Ensure we only react to the relevant event with the specified channel
if (newEvent.channelId !== voiceChannelId) return;
// Handle 10 minutes before event start
const now = new Date();
const tenMinutesBeforeStart = new Date(newEvent.scheduledStartTimestamp - 10 * 60 * 1000);
if (now >= tenMinutesBeforeStart && newEvent.status === 'SCHEDULED') {
// Unlock the channel 10 minutes before the event starts
await updateChannelPermissions(voiceChannel, true);
await voiceChannel.setName('Event Starting Soon - Channel Unlocked');
}
// Handle when event becomes active
if (newEvent.status === 'ACTIVE') {
// Set channel name to event name and keep it unlocked
await voiceChannel.setName(newEvent.name);
await updateChannelPermissions(voiceChannel, true);
}
// Handle when the event is canceled or completed
if (newEvent.status === 'COMPLETED' || newEvent.status === 'CANCELED') {
const events = await guild.scheduledEvents.fetch();
const activeEvents = events.filter(event => event.channelId === voiceChannelId && event.status === 'ACTIVE');
if (activeEvents.size === 0) {
// Lock the channel 10 minutes after the event ends if no other active events
setTimeout(async () => {
const currentEvents = await guild.scheduledEvents.fetch();
const currentActiveEvents = currentEvents.filter(event => event.channelId === voiceChannelId && event.status === 'ACTIVE');
if (currentActiveEvents.size === 0) {
await voiceChannel.setName('Event Ended - Channel Locking Soon');
await updateChannelPermissions(voiceChannel, false);
}
}, 10 * 60 * 1000); // 10 minutes
}
}
});
// Periodically check for upcoming events and handle permissions
setInterval(async () => {
const guilds = client.guilds.cache;
for (const guild of guilds.values()) {
const events = await guild.scheduledEvents.fetch();
const upcomingEvents = events.filter(event => event.channelId === voiceChannelId && event.scheduledStartTimestamp - Date.now() < 10 * 60 * 1000 && event.status === 'SCHEDULED');
// Handle unlocking channels for any events starting in less than 10 minutes
for (const event of upcomingEvents.values()) {
const voiceChannel = guild.channels.cache.get(event.channelId);
if (voiceChannel) {
await updateChannelPermissions(voiceChannel, true);
await voiceChannel.setName('Event Starting Soon - Channel Unlocked');
}
}
}
}, 5 * 60 * 1000); // Check every 5 minutes
// Helper function to update permissions
async function updateChannelPermissions(channel, allowAccess) {
const permissions = {
Connect: allowAccess,
Speak: allowAccess,
};
await channel.permissionOverwrites.edit(channel.guild.roles.everyone, permissions);
}
const { Events, PermissionFlagsBits } = require('discord.js');
const voiceChannelId = '1284713593056661618'; // Your target voice channel ID
client.on(Events.GuildScheduledEventUpdate, async (oldEvent, newEvent) => {
const guild = newEvent.guild;
const voiceChannel = guild.channels.cache.get(voiceChannelId);
if (!voiceChannel) {
console.error(`Voice channel with ID ${voiceChannelId} not found.`);
return;
}
// Ensure we only react to the relevant event with the specified channel
if (newEvent.channelId !== voiceChannelId) return;
// Handle 10 minutes before event start
const now = new Date();
const tenMinutesBeforeStart = new Date(newEvent.scheduledStartTimestamp - 10 * 60 * 1000);
if (now >= tenMinutesBeforeStart && newEvent.status === 'SCHEDULED') {
// Unlock the channel 10 minutes before the event starts
await updateChannelPermissions(voiceChannel, true);
await voiceChannel.setName('Event Starting Soon - Channel Unlocked');
}
// Handle when event becomes active
if (newEvent.status === 'ACTIVE') {
// Set channel name to event name and keep it unlocked
await voiceChannel.setName(newEvent.name);
await updateChannelPermissions(voiceChannel, true);
}
// Handle when the event is canceled or completed
if (newEvent.status === 'COMPLETED' || newEvent.status === 'CANCELED') {
const events = await guild.scheduledEvents.fetch();
const activeEvents = events.filter(event => event.channelId === voiceChannelId && event.status === 'ACTIVE');
if (activeEvents.size === 0) {
// Lock the channel 10 minutes after the event ends if no other active events
setTimeout(async () => {
const currentEvents = await guild.scheduledEvents.fetch();
const currentActiveEvents = currentEvents.filter(event => event.channelId === voiceChannelId && event.status === 'ACTIVE');
if (currentActiveEvents.size === 0) {
await voiceChannel.setName('Event Ended - Channel Locking Soon');
await updateChannelPermissions(voiceChannel, false);
}
}, 10 * 60 * 1000); // 10 minutes
}
}
});
// Periodically check for upcoming events and handle permissions
setInterval(async () => {
const guilds = client.guilds.cache;
for (const guild of guilds.values()) {
const events = await guild.scheduledEvents.fetch();
const upcomingEvents = events.filter(event => event.channelId === voiceChannelId && event.scheduledStartTimestamp - Date.now() < 10 * 60 * 1000 && event.status === 'SCHEDULED');
// Handle unlocking channels for any events starting in less than 10 minutes
for (const event of upcomingEvents.values()) {
const voiceChannel = guild.channels.cache.get(event.channelId);
if (voiceChannel) {
await updateChannelPermissions(voiceChannel, true);
await voiceChannel.setName('Event Starting Soon - Channel Unlocked');
}
}
}
}, 5 * 60 * 1000); // Check every 5 minutes
// Helper function to update permissions
async function updateChannelPermissions(channel, allowAccess) {
const permissions = {
Connect: allowAccess,
Speak: allowAccess,
};
await channel.permissionOverwrites.edit(channel.guild.roles.everyone, permissions);
}