Organize este código: function processOpenCommand(openTime, groupId) {…
- User
Organize este código:
function processOpenCommand(openTime, groupId) { scheduleOpenOperation(groupId, openTime); }
function checkScheduledOperations() { const schedule = loadData(); const now = moment();
for (const groupId in schedule) { const { fechar, abrir } = schedule[groupId]; if (fechar && now.isSameOrAfter(moment(fechar.nextDate, 'DD/MM/YYYY').set({ hour: fechar.time.split(':')[0], minute: fechar.time.split(':')[1] }))) { client.groupSettingUpdate(groupId, 'announcement'); delay(500); // client.sendMessage(groupId, { text: `*Grupo fechado com sucesso 🔒*` }); const nextTime = calculateNextTime(fechar.time); fechar.nextDate = nextTime.nextDate; fechar.timeRemaining = nextTime.timeRemaining; saveData(schedule); } if (abrir && now.isSameOrAfter(moment(abrir.nextDate, 'DD/MM/YYYY').set({ hour: abrir.time.split(':')[0], minute: abrir.time.split(':')[1] }))) { client.groupSettingUpdate(groupId, 'not_announcement'); delay(500); // client.sendMessage(groupId, { text: `*Grupo aberto com sucesso 🔓*` }); const nextTime = calculateNextTime(abrir.time); abrir.nextDate = nextTime.nextDate; abrir.timeRemaining = nextTime.timeRemaining; saveData(schedule); } }}
function deleteGroupOperation(groupId) { const data = loadData(); if (data[groupId]) { delete data[groupId]; saveData(data); client.sendMessage(from, { text:
*Registro deste grupo foi apagado com sucesso!*}); } else { console.log(Não há registro para esse grupo!); } }// Função para verificar as operações agendadas de forma periódica setInterval(checkScheduledOperations, 60 * 1000);
// [**** SORTEIO POR REAÇÕES *****]
// [***** FUNÇÃO PARA ENVIA UMA MSG X PARA TODOS OS GRUPOS****]
const schedule = require('node-schedule');
- Assistant
Claro! Aqui está o código organizado, com uma estrutura mais clara e separando as funções e seus propósitos de forma mais lógica:
const schedule = require('node-schedule'); const moment = require('moment'); // Função principal para processar o comando de abrir function processOpenCommand(openTime, groupId) { scheduleOpenOperation(groupId, openTime); } // Função para verificar operações agendadas function checkScheduledOperations() { const scheduleData = loadData(); const currentTime = moment(); for (const groupId in scheduleData) { const { fechar, abrir } = scheduleData[groupId]; checkCloseOperation(groupId, fechar, currentTime); checkOpenOperation(groupId, abrir, currentTime); } } // Função para verificar a operação de fechamento function checkCloseOperation(groupId, fechar, currentTime) { if (fechar && currentTime.isSameOrAfter(moment(fechar.nextDate, 'DD/MM/YYYY').set({ hour: fechar.time.split(':')[0], minute: fechar.time.split(':')[1] }))) { client.groupSettingUpdate(groupId, 'announcement'); delay(500); // client.sendMessage(groupId, { text: `*Grupo fechado com sucesso 🔒*` }); const nextTime = calculateNextTime(fechar.time); fechar.nextDate = nextTime.nextDate; fechar.timeRemaining = nextTime.timeRemaining; saveData(scheduleData); } } // Função para verificar a operação de abertura function checkOpenOperation(groupId, abrir, currentTime) { if (abrir && currentTime.isSameOrAfter(moment(abrir.nextDate, 'DD/MM/YYYY').set({ hour: abrir.time.split(':')[0], minute: abrir.time.split(':')[1] }))) { client.groupSettingUpdate(groupId, 'not_announcement'); delay(500); // client.sendMessage(groupId, { text: `*Grupo aberto com sucesso 🔓*` }); const nextTime = calculateNextTime(abrir.time); abrir.nextDate = nextTime.nextDate; abrir.timeRemaining = nextTime.timeRemaining; saveData(scheduleData); } } // Função para deletar operação do grupo function deleteGroupOperation(groupId) { const data = loadData(); if (data[groupId]) { delete data[groupId]; saveData(data); client.sendMessage(from, { text: `*Registro deste grupo foi apagado com sucesso!*` }); } else { console.log(`Não há registro para esse grupo!`); } } // Verificando operações agendadas de forma periódica setInterval(checkScheduledOperations, 60 * 1000); // [**** SORTEIO POR REAÇÕES *****] // [***** FUNÇÃO PARA ENVIA UMA MSG X PARA TODOS OS GRUPOS****]Melhorias e Organização
- Funções Separadas: As operações de fechar e abrir o grupo foram extraídas em suas próprias funções (
checkCloseOperationecheckOpenOperation) para melhor legibilidade e manutenção do código. - Nomes Claros: As variáveis e funções foram nomeadas de forma a representar melhor suas funcionalidades.
- Estruturação: O código foi organizado de forma que as funções seguem uma ordem lógica, o que facilita a navegação.
Este formato torna o código mais fácil de entender e modificar no futuro.
- Funções Separadas: As operações de fechar e abrir o grupo foram extraídas em suas próprias funções (
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.