wunsch-bot.js aktualisiert
This commit is contained in:
		
							
								
								
									
										130
									
								
								wunsch-bot.js
									
									
									
									
									
								
							
							
						
						
									
										130
									
								
								wunsch-bot.js
									
									
									
									
									
								
							@@ -6,6 +6,7 @@ const archiver = require('archiver');
 | 
			
		||||
const schedule = require('node-schedule');
 | 
			
		||||
const express = require('express');
 | 
			
		||||
const bodyParser = require('body-parser');
 | 
			
		||||
const axios = require('axios');
 | 
			
		||||
 | 
			
		||||
const app = express();
 | 
			
		||||
app.use(bodyParser.json());
 | 
			
		||||
@@ -233,8 +234,6 @@ bot.command('info', async (ctx) => {
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// Callback-Query-Handler
 | 
			
		||||
// Callback-Query-Handler
 | 
			
		||||
bot.on('callback_query', async (ctx) => {
 | 
			
		||||
    const data = ctx.callbackQuery.data;
 | 
			
		||||
@@ -266,28 +265,37 @@ bot.on('callback_query', async (ctx) => {
 | 
			
		||||
            // Überprüfe, ob der Benutzer ein Admin ist
 | 
			
		||||
            const admins = await bot.telegram.getChatAdministrators(chatId);
 | 
			
		||||
            const isAdmin = admins.some(admin => admin.user.id === userId);
 | 
			
		||||
 | 
			
		||||
        
 | 
			
		||||
            if (isAdmin) {
 | 
			
		||||
                const wishTitle = userStates[chatId]?.wishTitle;
 | 
			
		||||
                const category = userStates[chatId]?.category;
 | 
			
		||||
 | 
			
		||||
        
 | 
			
		||||
                // Füge den Wunsch in die "Nicht gefunden"-Liste ein
 | 
			
		||||
                if (wishTitle && category) {
 | 
			
		||||
                    saveNotFoundWish(wishTitle, category);
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
        
 | 
			
		||||
                // Bestätige die Speicherung und entferne die spezifische Nachricht
 | 
			
		||||
                await bot.telegram.sendMessage(allowedChatId, `📽️ *Sorry*,\n\nZum ${category} *"${wishTitle}"* wurde leider nichts gefunden. Keine Sorge, der Wunsch wurde auf unsere Liste der nicht gefundenen Titel gesetzt.`, {
 | 
			
		||||
                    message_thread_id: allowedThreadId,
 | 
			
		||||
                });
 | 
			
		||||
 | 
			
		||||
        
 | 
			
		||||
                await ctx.deleteMessage(messageId); 
 | 
			
		||||
 | 
			
		||||
        
 | 
			
		||||
                // Beantworte die Callback-Abfrage
 | 
			
		||||
                await ctx.answerCbQuery();
 | 
			
		||||
            } else {
 | 
			
		||||
                await ctx.answerCbQuery('Nur Admins können diese Funktion nutzen.');
 | 
			
		||||
            }
 | 
			
		||||
        } else if (data.startsWith('delete_not_found_')) {
 | 
			
		||||
            const index = parseInt(data.split('_')[3], 10);
 | 
			
		||||
            if (deleteNotFoundWish(index)) {
 | 
			
		||||
                await ctx.reply('Der Eintrag wurde erfolgreich gelöscht.');
 | 
			
		||||
            } else {
 | 
			
		||||
                await ctx.reply('Ungültige Nummer. Bitte versuche es erneut.');
 | 
			
		||||
            }
 | 
			
		||||
            // Beantworte die Callback-Abfrage
 | 
			
		||||
            await ctx.answerCbQuery();
 | 
			
		||||
        } else if (data === 'delete_not_found') {
 | 
			
		||||
            await ctx.reply('Bitte gib die Nummer des Eintrags ein, den du löschen möchtest.', {
 | 
			
		||||
                reply_markup: JSON.stringify({
 | 
			
		||||
@@ -334,7 +342,7 @@ bot.on('callback_query', async (ctx) => {
 | 
			
		||||
bot.on('text', async (ctx) => {
 | 
			
		||||
    const chatId = ctx.chat.id.toString();
 | 
			
		||||
    const userId = ctx.message.from.id;
 | 
			
		||||
    const text = ctx.message.text;
 | 
			
		||||
    const text = ctx.message.text.trim();
 | 
			
		||||
    const threadId = ctx.message?.message_thread_id; // Thema-ID (falls vorhanden)
 | 
			
		||||
 | 
			
		||||
    console.log(`Received message in chat ID ${chatId} and thread ID ${threadId}: ${text}`); // Logging zur Diagnose
 | 
			
		||||
@@ -342,81 +350,91 @@ bot.on('text', async (ctx) => {
 | 
			
		||||
    if (chatId !== allowedChatId || threadId !== allowedThreadId) {
 | 
			
		||||
        console.log(`Ignoring message in chat ID ${chatId} and thread ID ${threadId} as it's not allowed.`);
 | 
			
		||||
        if (text.startsWith('/wunsch')) {
 | 
			
		||||
            // Nachricht zurückweisen, wenn der Befehl in einem nicht erlaubten Kanal verwendet wird
 | 
			
		||||
            await ctx.reply('❌ Dieser Befehl ist in diesem Kanal nicht erlaubt. Bitte benutze den Befehl in den Serien- und Filmwünsche Kanal.', { disable_notification: true });
 | 
			
		||||
        }
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Überprüfe, ob der Benutzer in der Eingabestimmung ist
 | 
			
		||||
    // Prüfe, ob der Benutzer den Vorgang abbrechen möchte
 | 
			
		||||
    if (text === '/cancel') {
 | 
			
		||||
        if (userStates[chatId]) {
 | 
			
		||||
            const categoryMessageId = userStates[chatId].categoryMessageId;
 | 
			
		||||
            const wishCommandMessageId = userStates[chatId].wishCommandMessageId;
 | 
			
		||||
            const commandMessageId = userStates[chatId].commandMessageId;
 | 
			
		||||
 | 
			
		||||
            // Lösche alle relevanten Nachrichten
 | 
			
		||||
            if (categoryMessageId) await ctx.deleteMessage(categoryMessageId);
 | 
			
		||||
            if (wishCommandMessageId) await ctx.deleteMessage(wishCommandMessageId);
 | 
			
		||||
            if (commandMessageId) await ctx.deleteMessage(commandMessageId);
 | 
			
		||||
 | 
			
		||||
            // Setze den Benutzerstatus zurück
 | 
			
		||||
            delete userStates[chatId];
 | 
			
		||||
            await ctx.reply('🔴 Der Wunschvorgang wurde abgebrochen.', { disable_notification: true });
 | 
			
		||||
        } else {
 | 
			
		||||
            await ctx.reply('🔴 Es gibt keinen laufenden Vorgang zum Abbrechen.', { disable_notification: true });
 | 
			
		||||
        }
 | 
			
		||||
        return; // Beende die Verarbeitung
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Der Rest des Codes bleibt gleich
 | 
			
		||||
    if (userStates[chatId]) {
 | 
			
		||||
        if (userStates[chatId].waitingForLink) {
 | 
			
		||||
            // Verarbeite den Link des Wunsches
 | 
			
		||||
            const link = text.trim(); // Link erhalten
 | 
			
		||||
            userStates[chatId].wishLink = link; // Speichern des Links
 | 
			
		||||
            
 | 
			
		||||
            // Frage nach dem Titel des Wunsches
 | 
			
		||||
            const link = text;
 | 
			
		||||
            userStates[chatId].wishLink = link;
 | 
			
		||||
            await ctx.reply(`Bitte gib den Titel des ${userStates[chatId].category} ein.`, { disable_notification: true });
 | 
			
		||||
            
 | 
			
		||||
            userStates[chatId].waitingForLink = false; // Status zurücksetzen
 | 
			
		||||
            userStates[chatId].waitingForWish = true; // Nun auf den Wunsch warten
 | 
			
		||||
            return; // Beende die Verarbeitung, da der Benutzer jetzt nach dem Titel gefragt wird
 | 
			
		||||
            userStates[chatId].waitingForLink = false;
 | 
			
		||||
            userStates[chatId].waitingForWish = true;
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (userStates[chatId].waitingForWish) {
 | 
			
		||||
            // Verarbeite den Titel des Wunsches
 | 
			
		||||
            const wish = text.trim(); // Titel erhalten
 | 
			
		||||
            const wish = text;
 | 
			
		||||
            if (wish) {
 | 
			
		||||
                const category = userStates[chatId].category;
 | 
			
		||||
                const link = userStates[chatId].wishLink;
 | 
			
		||||
                const categoryMessageId = userStates[chatId].categoryMessageId;
 | 
			
		||||
                const titleMessageId = ctx.message.message_id; // ID der Titel-Nachricht erhalten
 | 
			
		||||
                const commandMessageId = userStates[chatId].commandMessageId; // ID der Befehl-Nachricht erhalten
 | 
			
		||||
                const wishCommandMessageId = userStates[chatId].wishCommandMessageId; // ID der /wunsch-Nachricht erhalten
 | 
			
		||||
                
 | 
			
		||||
                const titleMessageId = ctx.message.message_id;
 | 
			
		||||
                const commandMessageId = userStates[chatId].commandMessageId;
 | 
			
		||||
                const wishCommandMessageId = userStates[chatId].wishCommandMessageId;
 | 
			
		||||
 | 
			
		||||
                try {
 | 
			
		||||
                    userStates[chatId].wishTitle = wish; // Speichern des Wunsch-Titels für spätere Verwendung
 | 
			
		||||
                    userStates[chatId].wishTitle = wish;
 | 
			
		||||
                    await sendWish(wish, category, chatId, userId, link);
 | 
			
		||||
                    
 | 
			
		||||
                    // Lösche die Nachrichten, die beim Verarbeiten des Wunsches gesendet wurden
 | 
			
		||||
                    if (categoryMessageId) await ctx.deleteMessage(categoryMessageId).catch(e => logError(new Error(`Failed to delete category message: ${e.message}`)));
 | 
			
		||||
                    if (titleMessageId) await ctx.deleteMessage(titleMessageId).catch(e => logError(new Error(`Failed to delete title message: ${e.message}`)));
 | 
			
		||||
                    if (commandMessageId) await ctx.deleteMessage(commandMessageId).catch(e => logError(new Error(`Failed to delete command message: ${e.message}`)));
 | 
			
		||||
                    if (wishCommandMessageId) await ctx.deleteMessage(wishCommandMessageId).catch(e => logError(new Error(`Failed to delete /wunsch message: ${e.message}`)));
 | 
			
		||||
                    
 | 
			
		||||
                    userStates[chatId].waitingForWish = false; // Benutzerstatus zurücksetzen
 | 
			
		||||
                    if (categoryMessageId) await ctx.deleteMessage(categoryMessageId);
 | 
			
		||||
                    if (titleMessageId) await ctx.deleteMessage(titleMessageId);
 | 
			
		||||
                    if (commandMessageId) await ctx.deleteMessage(commandMessageId);
 | 
			
		||||
                    if (wishCommandMessageId) await ctx.deleteMessage(wishCommandMessageId);
 | 
			
		||||
 | 
			
		||||
                    userStates[chatId].waitingForWish = false;
 | 
			
		||||
                } catch (error) {
 | 
			
		||||
                    logError(new Error(`Error processing wish: ${error.message}`));
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                await ctx.reply(`Bitte gib den Titel des ${userStates[chatId].category} ein.`, { disable_notification: true });
 | 
			
		||||
            }
 | 
			
		||||
            return; // Beende die Verarbeitung, wenn der Benutzer in der Eingabestimmung ist
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (userStates[chatId].waitingForDeleteIndex) {
 | 
			
		||||
            // Verarbeite die Löschanfrage
 | 
			
		||||
            const index = parseInt(text.trim(), 10) - 1; // Index aus der Nachricht extrahieren
 | 
			
		||||
            const index = parseInt(text, 10) - 1;
 | 
			
		||||
            if (deleteNotFoundWish(index)) {
 | 
			
		||||
                await ctx.reply('Der Eintrag wurde erfolgreich gelöscht.');
 | 
			
		||||
            } else {
 | 
			
		||||
                await ctx.reply('Ungültige Nummer. Bitte versuche es erneut.');
 | 
			
		||||
            }
 | 
			
		||||
            userStates[chatId].waitingForDeleteIndex = false; // Benutzerstatus zurücksetzen
 | 
			
		||||
            userStates[chatId].waitingForDeleteIndex = false;
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (text.startsWith('/wunsch')) {
 | 
			
		||||
        // Benutzer zur Auswahl der Kategorie auffordern
 | 
			
		||||
        const commandMessage = await ctx.reply('Möchtest du etwas wünschen? Wähle bitte eine Kategorie:', { ...getCategoryKeyboard(), disable_notification: true });
 | 
			
		||||
        const commandMessage = await ctx.reply('Möchtest du etwas wünschen? Wähle bitte eine Kategorie. Du kannst den Vorgang jederzeit mit dem Befehl /cancel abbrechen.', { ...getCategoryKeyboard(), disable_notification: true });
 | 
			
		||||
        userStates[chatId] = { 
 | 
			
		||||
            waitingForCategory: true, 
 | 
			
		||||
            commandMessageId: commandMessage.message_id, // Speichern der ID der Befehl-Nachricht
 | 
			
		||||
            wishCommandMessageId: ctx.message.message_id // Speichern der ID der /wunsch-Nachricht
 | 
			
		||||
        }; // Setze den Status auf "wartend auf Kategorie"
 | 
			
		||||
            commandMessageId: commandMessage.message_id,
 | 
			
		||||
            wishCommandMessageId: ctx.message.message_id
 | 
			
		||||
        };
 | 
			
		||||
    } else if (text.startsWith('/notfound')) {
 | 
			
		||||
        // Überprüfe, ob der Benutzer ein Admin ist
 | 
			
		||||
        const admins = await bot.telegram.getChatAdministrators(chatId);
 | 
			
		||||
        const isAdmin = admins.some(admin => admin.user.id === userId);
 | 
			
		||||
    
 | 
			
		||||
@@ -429,21 +447,23 @@ bot.on('text', async (ctx) => {
 | 
			
		||||
                    replyMessage += 'Keine nicht gefundenen Wünsche.';
 | 
			
		||||
                } else {
 | 
			
		||||
                    notFoundWishes.forEach((entry, index) => {
 | 
			
		||||
                        replyMessage += `${index + 1}. Kategorie: ${entry.category}\n   Wunsch: ${entry.wish}\n\n\n`;
 | 
			
		||||
                        replyMessage += `${index + 1}. Kategorie: ${entry.category} - Wunsch: ${entry.wish}\n`;
 | 
			
		||||
                    });
 | 
			
		||||
                    replyMessage += '\n🗑️ [Löschen]';
 | 
			
		||||
                }
 | 
			
		||||
                
 | 
			
		||||
                // Füge die Löschen-Buttons für jeden Eintrag hinzu
 | 
			
		||||
                const inlineKeyboard = {
 | 
			
		||||
                    reply_markup: JSON.stringify({
 | 
			
		||||
                        inline_keyboard: notFoundWishes.map((_, index) => [
 | 
			
		||||
                            { text: `🗑️ Löschen ${index + 1}`, callback_data: `delete_not_found_${index}` }
 | 
			
		||||
                        ])
 | 
			
		||||
                    })
 | 
			
		||||
                };
 | 
			
		||||
    
 | 
			
		||||
                // Sende die Liste direkt an den Admin
 | 
			
		||||
                await ctx.reply(replyMessage, { disable_notification: true, ...getDeleteNotFoundWishKeyboard() });
 | 
			
		||||
    
 | 
			
		||||
                // Lösche den /notfound Befehl
 | 
			
		||||
                await ctx.deleteMessage(ctx.message.message_id).catch(e => logError(new Error(`Failed to delete /notfound command message: ${e.message}`)));
 | 
			
		||||
                await ctx.reply(replyMessage, inlineKeyboard);
 | 
			
		||||
            } else {
 | 
			
		||||
                await ctx.reply('Noch keine nicht gefundenen Wünsche aufgezeichnet.', { disable_notification: true });
 | 
			
		||||
                await ctx.reply('Es gibt keine nicht gefundenen Wünsche.');
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            await ctx.reply('❌ Du bist nicht berechtigt, diese Funktion zu nutzen. ❌', { disable_notification: true });
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
});
 | 
			
		||||
@@ -500,4 +520,4 @@ app.listen(PORT, () => {
 | 
			
		||||
 | 
			
		||||
bot.launch();
 | 
			
		||||
 | 
			
		||||
console.log('Bot is running...');
 | 
			
		||||
console.log('Bot is running...');
 | 
			
		||||
		Reference in New Issue
	
	Block a user