Bot Not Updating Voice Permissions

I'm trying to have an event voice channel that is locked(everyone cannot connect or speak), but when an event is live have the perms update so everyone can connect and speak to that channel. There are no console errors I am seeing, and I know it's detecting an active event since the channels status gets updated to reflect the event name as it's supposed to. Can someone give this a look over and see what I'm doing wrong in the permissions area?
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);
}
2 Replies
d.js toolkit
d.js toolkit3mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
duck
duck3mo ago
I know it's detecting an active event since the channels status gets updated to reflect the event name as it's supposed to
that's strange given that <GuildScheduledEvent>.status isn't a string it should be a number for which you could use the GuildScheduledEventStatus enum
Want results from more Discord servers?
Add your server