require('dotenv').config(); const { Client, GatewayIntentBits, REST, Routes } = require('discord.js'); const { Birthday } = require('./database'); const schedule = require('node-schedule-tz'); // Create a new Discord client const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers] }); // Role name for birthdays const BIRTHDAY_ROLE_NAME = 'Geburtstags Kind'; // Handle slash commands client.on('interactionCreate', async (interaction) => { if (!interaction.isCommand()) return; if (interaction.channelId !== process.env.BIRTHDAY_CHANNEL_ID) return; const { commandName, options } = interaction; if (commandName === 'addbirthday') { const month = options.getInteger('month'); const day = options.getInteger('day'); if (!month || !day || month < 1 || month > 12 || day < 1 || day > 31) { return interaction.reply({ content: 'Bitte geben Sie ein gültiges Datum an (Monat: 1-12, Tag: 1-31).', ephemeral: true }); } const formattedDate = `${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`; try { await Birthday.upsert({ userId: interaction.user.id, date: formattedDate }); await interaction.reply({ content: 'Dein Geburtstag wurde hinzugefügt!', ephemeral: true }); } catch (error) { console.error(error); await interaction.reply({ content: 'Beim Speichern Ihres Geburtstages ist ein Fehler aufgetreten.', ephemeral: true }); } } else if (commandName === 'viewbirthdays') { try { const birthdays = await Birthday.findAll({ order: [['date', 'ASC']], }); if (birthdays.length === 0) { return interaction.reply({ content: 'Keine Geburtstage gefunden!', ephemeral: true }); } const birthdayList = birthdays.map((b) => `<@${b.userId}>: ${b.date}`).join('\n'); await interaction.reply({ content: `Hier sind die gespeicherten Geburtstage: (MM-DD)\n${birthdayList}`, ephemeral: true }); } catch (error) { console.error(error); await interaction.reply({ content: 'Beim Abrufen der Geburtstage ist ein Fehler aufgetreten.', ephemeral: true }); } } else if (commandName === 'todaybirthdays') { const today = new Date(); const formattedToday = `${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`; try { const birthdays = await Birthday.findAll({ where: { date: formattedToday } }); if (birthdays.length === 0) { return interaction.reply({ content: 'Heute sind keine Geburtstage!', ephemeral: true }); } const birthdayList = birthdays.map((b) => `<@${b.userId}>`).join(', '); await interaction.reply({ content: `Geburtstag hat heute: ${birthdayList}`, ephemeral: true }); } catch (error) { console.error(error); await interaction.reply({ content: 'Beim Abrufen der heutigen Geburtstage ist ein Fehler aufgetreten.', ephemeral: true }); } } }); // Schedule a job to assign the birthday role at midnight UTC schedule.scheduleJob('0 0 * * *', async () => { try { const today = new Date(); const formattedToday = `${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`; const birthdays = await Birthday.findAll({ where: { date: formattedToday } }); if (birthdays.length > 1) { const channel = client.channels.cache.get(process.env.BIRTHDAY_CHANNEL_ID); // Replace with your channel ID const birthdayList = birthdays.map((b) => `<@${b.userId}>`).join(', '); channel.send(`<:Stimmung:1263591234455474206> ${birthdayList} haben heute Geburtstag <:Love:1236986512235823124> Das gesamte Team wünscht euch einen Tollen Tag und lasst euch schön Feiern <:Stimmung:1263591234455474206>\n\n@everyone`); channel.send({ stickers: ['1263591446146060402'] }) .then(() => console.log('Sticker sent successfully!')) .catch(console.error); } else if (birthdays.length === 1) { const channel = client.channels.cache.get(process.env.BIRTHDAY_CHANNEL_ID); // Replace with your channel ID const birthdayList = birthdays.map((b) => `<@${b.userId}>`).join(', '); channel.send(`<:Stimmung:1263591234455474206> ${birthdayList} hat heute Geburtstag <:Love:1236986512235823124> Das gesamte Team wünscht dir einen Tollen Tag und lass dich schön Feiern <:Stimmung:1263591234455474206>\n\n@everyone`); channel.send({ stickers: ['1263591446146060402'] }) .then(() => console.log('Sticker sent successfully!')) .catch(console.error); } for (const guild of client.guilds.cache.values()) { const birthdayRole = guild.roles.cache.find((role) => role.name === BIRTHDAY_ROLE_NAME); if (!birthdayRole) { console.error(`Birthday role not found in guild: ${guild.name}`); continue; } for (const birthday of birthdays) { const member = await guild.members.fetch(birthday.userId).catch(() => null); if (member) { await member.roles.add(birthdayRole).catch(console.error); console.log(`Assigned birthday role to ${member.user.tag} in guild: ${guild.name}`); } } } } catch (error) { console.error('Error assigning birthday roles:', error); } }); // Schedule a job to remove the birthday role at the end of the day schedule.scheduleJob('59 23 * * *', async () => { try { // Fetch the last 2 messages (to include the command message itself) const channel = client.channels.cache.get(process.env.BIRTHDAY_CHANNEL_ID); const userId = process.env.CLIENT_ID; // Fetch the last 100 messages in the channel const messages = await channel.messages.fetch({ limit: 100 }); // Filter messages by the specific user ID const userMessages = messages.filter(msg => msg.author.id === userId); // Loop through and delete each message for (const msg of userMessages.values()) { await msg.delete(); } console.log(`Deleted ${userMessages.size} messages from user ${userId}.`); for (const guild of client.guilds.cache.values()) { const birthdayRole = guild.roles.cache.find((role) => role.name === BIRTHDAY_ROLE_NAME); if (!birthdayRole) { console.error(`Birthday role not found in guild: ${guild.name}`); continue; } const membersWithRole = guild.members.cache.filter((member) => member.roles.cache.has(birthdayRole.id)); for (const member of membersWithRole.values()) { await member.roles.remove(birthdayRole).catch(err => { console.error(`Failed to remove role from ${member.user.tag}: ${err}`); }); console.log(`Removed birthday role from ${member.user.tag} in guild: ${guild.name}`); } } } catch (error) { console.error('Error removing birthday roles:', error); } }); async function asdad() { for (const guild of client.guilds.cache.values()) { const birthdayRole = guild.roles.cache.find((role) => role.id === 1282577941183598623); if (!birthdayRole) { console.error(`Birthday role not found in guild: ${guild.name}`); continue; } const membersWithRole = guild.members.cache.filter((member) => member.roles.cache.has(birthdayRole.id)); for (const member of membersWithRole.values()) { await member.roles.remove(birthdayRole).catch(err => { console.error(`Failed to remove role from ${member.user.tag}: ${err}`); }); console.log(`Removed birthday role from ${member.user.tag} in guild: ${guild.name}`); } } } asdad(); // Log in to Discord client.login(process.env.DISCORD_TOKEN);