This snippet will delete a channel under a specified category. A channel will be deleted if it is inactive after a certain number of days; that is, if the timestamp of the last message in the channel is over the specified number of days.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const INACTIVE_DAYS = 7; // Set the number of inactive days
const CATEGORY_IDS = [`CATEGORYID1`, `CATEGORYID2`]; //IDs of category where channels will be monitored for inactivity
const GUILD_ID = `GUILD_ID_HERE`; // Guild or server id
// W A R N I N G : Don't touch the code below unless you understand how it works π
let duration = INACTIVE_DAYS * 24 * 60 * 60 * 1000;
let all_channels = await lib.discord.guilds['@0.0.2'].channels.list({
guild_id: GUILD_ID,
});
let channels = all_channels.filter((channel) => {
return CATEGORY_IDS.includes(channel.parent_id);
});
for (let channel of channels) {
let messages = await lib.discord.channels['@0.3.2'].messages.list({
channel_id: channel.id,
limit: 1,
});
if (messages && messages.length > 0) {
let message = messages[0];
let timestamp = new Date(message.timestamp).getTime();
let isExpired = new Date().getTime() - timestamp >= duration;
if (isExpired) {
await lib.discord.channels['@0.3.2'].destroy({
channel_id: `${channel.id}`,
});
}
}
}