24.02.2023, 21:49
Code:
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
]
});
const TOKEN = 'BOT_TOKEN';
const CHANNEL_NAME = '📸bilder';
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
// Start the timer to check for messages without images every 60 seconds
setInterval(checkForNonImageMessages, 60 * 1000);
});
client.on('messageCreate', async (message) => {
console.log("Nachricht gesendet");
// Check if the message is in the designated channel
if (message.channel.name !== CHANNEL_NAME) return;
console.log("Channel erfolgreich erkannt");
// Check if the message has any attachments or mentions
const hasAttachment = message.attachments.size > 0;
// If the message has no attachment, mention, or reaction, delete it
if (!hasAttachment && !message.thread) {
console.log(`Nachricht wurde gelöscht`);
try {
await message.delete();
} catch (error) {
console.error(`Error deleting message: ${error}`);
}
}
});
async function checkForNonImageMessages() {
const channel = client.channels.cache.find(c => c.name === CHANNEL_NAME);
const messages = await channel.messages.fetch();
messages.forEach(async (message) => {
// Check if the message has any attachments or mentions
const hasAttachment = message.attachments.size > 0;
// If the message has no attachment, mention, or reaction, delete it
if (!hasAttachment && !message.thread) {
console.log(`Nachricht ohne Bild gefunden. Nachricht wird gelöscht.`);
try {
await message.delete();
} catch (error) {
console.error(`Error deleting message: ${error}`);
}
}
});
}
client.login(TOKEN);