media_ref
media_ref
DIAdiscord.js - Imagine an app
Created by media_ref on 2/5/2024 in #djs-questions
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);
10 replies