Automatically deleting and recreating channels every 5 seconds

I'm having trouble getting this to work for some reason:
const { Client, Intents, GatewayIntentBits } = require('discord.js');
const { EmbedBuilder } = require('discord.js');
require('dotenv').config();

const prefix = '!';

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});

client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
setInterval(deleteAndRecreateChannels, 5000); // run every 5 seconds
});

async function deleteAndRecreateChannels() {
const categoryId = '1203824690003443753';
const channelToExclude = '1203825676973510687';

const category = await client.channels.cache.get(categoryId);

if (!category) {
console.error(`Category ${categoryId} not found.`);
return;
}

for (const channel of category.channels.cache.values()) {
if (channel.id !== channelToExclude) {
try {
const oldChannelData = await channel.clone();
await channel.delete();
const newChannel = await channel.guild.channels.create(oldChannelData);
console.log(`Channel deleted and recreated: ${newChannel.name}`);
} catch (error) {
console.error(`Error deleting and recreating channel ${channel.id}:`, error);
}
}
}
}

client.on('messageCreate', async (message) => {
try {
if (message.channel.id === '1192951800412770324') {
const ignoredRoleIds = [
'1191396401272332368',
'1191396404724256829',
'1191396410545934536',
'1191396412055892101',
'1191396408994037851',
];

if (message.member.roles.cache.some((role) => ignoredRoleIds.includes(role.id))) {
return;
}

if (message.attachments.size === 0) {
message.delete();
} else {
return;
}
}
} catch (error) {
console.log(error);
}
});

const botToken = process.env['BOT_TOKEN'];
client.login(botToken);
const { Client, Intents, GatewayIntentBits } = require('discord.js');
const { EmbedBuilder } = require('discord.js');
require('dotenv').config();

const prefix = '!';

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});

client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
setInterval(deleteAndRecreateChannels, 5000); // run every 5 seconds
});

async function deleteAndRecreateChannels() {
const categoryId = '1203824690003443753';
const channelToExclude = '1203825676973510687';

const category = await client.channels.cache.get(categoryId);

if (!category) {
console.error(`Category ${categoryId} not found.`);
return;
}

for (const channel of category.channels.cache.values()) {
if (channel.id !== channelToExclude) {
try {
const oldChannelData = await channel.clone();
await channel.delete();
const newChannel = await channel.guild.channels.create(oldChannelData);
console.log(`Channel deleted and recreated: ${newChannel.name}`);
} catch (error) {
console.error(`Error deleting and recreating channel ${channel.id}:`, error);
}
}
}
}

client.on('messageCreate', async (message) => {
try {
if (message.channel.id === '1192951800412770324') {
const ignoredRoleIds = [
'1191396401272332368',
'1191396404724256829',
'1191396410545934536',
'1191396412055892101',
'1191396408994037851',
];

if (message.member.roles.cache.some((role) => ignoredRoleIds.includes(role.id))) {
return;
}

if (message.attachments.size === 0) {
message.delete();
} else {
return;
}
}
} catch (error) {
console.log(error);
}
});

const botToken = process.env['BOT_TOKEN'];
client.login(botToken);
6 Replies
d.js toolkit
d.js toolkit5mo 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! - Marked as resolved by OP
media_ref
media_ref5mo ago
Here's the error:
/home/container/index.js:31
for (const channel of category.channels.cache.values()) {
^
TypeError: Cannot read properties of undefined (reading 'cache')
at Timeout.deleteAndRecreateChannels [as _onTimeout] (/home/container/index.js:31:43)
/home/container/index.js:31
for (const channel of category.channels.cache.values()) {
^
TypeError: Cannot read properties of undefined (reading 'cache')
at Timeout.deleteAndRecreateChannels [as _onTimeout] (/home/container/index.js:31:43)
It's supposed to delete and then remake the channels with the same permissions
monbrey
monbrey5mo ago
Doing this every 5 seconds is ridiculous API spam and would end up with other issues
media_ref
media_ref5mo ago
No it's just for debugging It's supposed to be 1 hour
monbrey
monbrey5mo ago
In this case the problem seems to be that the channel you got is not a category channel Actually no sorry - its category.children, not channels
media_ref
media_ref5mo ago
Yep that was it, thank you!!