I’ve hit a bit of a roadblock with an idea. There are no errors in the code, console wise, so I’m sure it’s just a matter of how I’m trying to do things.
The general idea is if an event is scheduled for a particular voice channel within the next 10 minutes then the channels status gets updated to reflect this. This portion doesn’t work, but isn’t a big deal.
What I do need to work and doesn’t is the “everyone” permissions for connect and speak. Currently I have them set to false so the event channel isn’t “open to the public”
In the event that an event is starting in the next 10 minutes or is currently active I’d like the connect and speak permissions for everyone to be true. This is the part that isn’t working either, but is the main purpose of this script. Again no errors in my console to go off of.
It is detecting an active event though because I have it set to set the channels status as the event name when active and this is working.
Am I doing something wrong permissions wise?
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);
}