142 lines
5.3 KiB
JavaScript
142 lines
5.3 KiB
JavaScript
// Mappa globale per salvare gli interval id anche dopo un "hot-reload"
|
|
if (!global.__meb_live_trackers) {
|
|
global.__meb_live_trackers = new Map();
|
|
}
|
|
|
|
module.exports = [
|
|
{
|
|
id: 'live-refresh',
|
|
execute: async ({ bot, chatId, msg }) => {
|
|
const liveCmd = require('../commands/live.js');
|
|
const newText = liveCmd.formatLiveData();
|
|
|
|
try {
|
|
await bot.editMessageText(newText, {
|
|
chat_id: chatId,
|
|
message_id: msg.message_id,
|
|
parse_mode: 'Markdown',
|
|
reply_markup: {
|
|
inline_keyboard: [
|
|
[{ text: 'Avvia Live (2s)', callback_data: 'live-start' }],
|
|
[{ text: 'Aggiorna', callback_data: 'live-refresh' }]
|
|
]
|
|
}
|
|
});
|
|
} catch (e) {
|
|
if (!e.message.includes('message is not modified')) {
|
|
console.error('[Telegram Live] Errore refresh:', e.message);
|
|
}
|
|
}
|
|
}
|
|
},
|
|
{
|
|
id: 'live-start',
|
|
execute: async ({ bot, chatId, msg }) => {
|
|
const liveCmd = require('../commands/live.js');
|
|
|
|
const messageId = msg.message_id;
|
|
const liveKey = `${chatId}_${messageId}`;
|
|
|
|
// Se gia' attivo per questo messaggio, ignora
|
|
if (global.__meb_live_trackers.has(liveKey)) return;
|
|
|
|
const stopMarkup = {
|
|
inline_keyboard: [
|
|
[{ text: 'Ferma Live', callback_data: 'live-stop' }]
|
|
]
|
|
};
|
|
|
|
await bot.editMessageReplyMarkup(stopMarkup, {
|
|
chat_id: chatId,
|
|
message_id: messageId
|
|
});
|
|
|
|
// 30 tick da 2 secondi = 60 secondi, poi auto-stop
|
|
let count = 30;
|
|
const intervalTimer = setInterval(async () => {
|
|
count--;
|
|
const baseText = liveCmd.formatLiveData();
|
|
|
|
// Auto-stop quando il tempo scade
|
|
if (count <= 0) {
|
|
if (global.__meb_live_trackers.has(liveKey)) {
|
|
clearInterval(global.__meb_live_trackers.get(liveKey));
|
|
global.__meb_live_trackers.delete(liveKey);
|
|
}
|
|
try {
|
|
await bot.editMessageText(
|
|
baseText + `\n_Live terminato automaticamente (60s)._`,
|
|
{
|
|
chat_id: chatId,
|
|
message_id: messageId,
|
|
parse_mode: 'Markdown',
|
|
reply_markup: {
|
|
inline_keyboard: [
|
|
[{ text: 'Avvia Live (2s)', callback_data: 'live-start' }],
|
|
[{ text: 'Aggiorna', callback_data: 'live-refresh' }]
|
|
]
|
|
}
|
|
}
|
|
);
|
|
} catch (e) { /* ignore */ }
|
|
return;
|
|
}
|
|
|
|
// Aggiornamento live con countdown
|
|
const newText = baseText + `\n_Live attivo: arresto tra *${count * 2}s*_`;
|
|
|
|
try {
|
|
await bot.editMessageText(newText, {
|
|
chat_id: chatId,
|
|
message_id: messageId,
|
|
parse_mode: 'Markdown',
|
|
reply_markup: stopMarkup
|
|
});
|
|
} catch (e) {
|
|
if (e.response && e.response.statusCode === 429) {
|
|
console.warn('[Telegram Live] Rate limit raggiunto');
|
|
} else if (e.message && e.message.includes('message to edit not found')) {
|
|
// Messaggio cancellato dall'utente
|
|
clearInterval(intervalTimer);
|
|
global.__meb_live_trackers.delete(liveKey);
|
|
}
|
|
// Ignora "message is not modified"
|
|
}
|
|
}, 2000);
|
|
|
|
global.__meb_live_trackers.set(liveKey, intervalTimer);
|
|
}
|
|
},
|
|
{
|
|
id: 'live-stop',
|
|
execute: async ({ bot, chatId, msg }) => {
|
|
const liveCmd = require('../commands/live.js');
|
|
|
|
const messageId = msg.message_id;
|
|
const liveKey = `${chatId}_${messageId}`;
|
|
|
|
// Pulisci l'interval se esiste
|
|
if (global.__meb_live_trackers.has(liveKey)) {
|
|
clearInterval(global.__meb_live_trackers.get(liveKey));
|
|
global.__meb_live_trackers.delete(liveKey);
|
|
}
|
|
|
|
const newText = liveCmd.formatLiveData();
|
|
|
|
try {
|
|
await bot.editMessageText(newText + '\n_Live fermato._', {
|
|
chat_id: chatId,
|
|
message_id: messageId,
|
|
parse_mode: 'Markdown',
|
|
reply_markup: {
|
|
inline_keyboard: [
|
|
[{ text: 'Avvia Live (2s)', callback_data: 'live-start' }],
|
|
[{ text: 'Aggiorna', callback_data: 'live-refresh' }]
|
|
]
|
|
}
|
|
});
|
|
} catch (e) { /* ignore */ }
|
|
}
|
|
}
|
|
];
|