Initial commit
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,189 @@
|
||||
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);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
const { Sequelize, DataTypes } = require('sequelize');
|
||||
|
||||
// Initialize SQLite database
|
||||
const sequelize = new Sequelize({
|
||||
dialect: 'sqlite',
|
||||
storage: './birthdays.sqlite',
|
||||
});
|
||||
|
||||
// Define the Birthday model
|
||||
const Birthday = sequelize.define('Birthday', {
|
||||
userId: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
unique: true,
|
||||
},
|
||||
date: {
|
||||
type: DataTypes.STRING, // Store as MM-DD
|
||||
allowNull: false,
|
||||
},
|
||||
});
|
||||
|
||||
// Sync the database
|
||||
(async () => {
|
||||
await sequelize.sync({ alter: true });
|
||||
console.log('Database synced!');
|
||||
})();
|
||||
|
||||
module.exports = { sequelize, Birthday };
|
||||
Generated
+2088
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"discord.js": "^14.17.3",
|
||||
"dotenv": "^16.4.7",
|
||||
"node-schedule": "^2.1.1",
|
||||
"node-schedule-tz": "^1.2.1-4",
|
||||
"sequelize": "^6.37.5",
|
||||
"sqlite3": "^5.1.7"
|
||||
},
|
||||
"name": "desasterdiscordbot",
|
||||
"version": "1.0.0",
|
||||
"main": "bot.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"description": ""
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
require('dotenv').config();
|
||||
const { REST, Routes } = require('discord.js');
|
||||
|
||||
const commands = [
|
||||
{
|
||||
name: 'addbirthday',
|
||||
description: 'Add your birthday',
|
||||
options: [
|
||||
{
|
||||
name: 'month',
|
||||
type: 4, // Integer
|
||||
description: 'The month of your birthday (1-12)',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: 'day',
|
||||
type: 4, // Integer
|
||||
description: 'The day of your birthday (1-31)',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'viewbirthdays',
|
||||
description: 'View all saved birthdays',
|
||||
},
|
||||
{
|
||||
name: 'todaybirthdays',
|
||||
description: 'Check who has a birthday today',
|
||||
},
|
||||
];
|
||||
|
||||
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN);
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
console.log('Started refreshing application (/) commands.');
|
||||
|
||||
await rest.put(
|
||||
Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.GUILD_ID),
|
||||
{ body: commands }
|
||||
);
|
||||
|
||||
console.log('Successfully reloaded application (/) commands.');
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
})();
|
||||
Reference in New Issue
Block a user