Better error handling + more commands + code optimization

This commit is contained in:
lucmsilva651 2024-08-30 21:12:38 -03:00
parent e4c8327117
commit 96f915838d
No known key found for this signature in database
GPG key ID: D9B075FC6DC93985
7 changed files with 240 additions and 280 deletions

View file

@ -12,120 +12,76 @@ function formatUptime(uptime) {
}
function getSystemInfo() {
const platform = os.platform();
const release = os.release();
const arch = os.arch();
const cpuModel = os.cpus()[0].model;
const cpuCores = os.cpus().length;
const totalMemory = (os.totalmem() / (1024 ** 3)).toFixed(2) + ' GB';
const freeMemory = (os.freemem() / (1024 ** 3)).toFixed(2) + ' GB';
const loadAverage = os.loadavg().map(avg => avg.toFixed(2)).join(', ');
const uptime = formatUptime(os.uptime());
const nodeVersion = process.version;
const { platform, release, arch, cpus, totalmem, freemem, loadavg, uptime } = os;
const [cpu] = cpus();
return `*Server Stats*\n\n` +
`*OS:* \`${platform} ${release}\`\n` +
`*Arch:* \`${arch}\`\n` +
`*Node.js Version:* \`${nodeVersion}\`\n` +
`*CPU:* \`${cpuModel}\`\n` +
`*CPU Cores:* \`${cpuCores} cores\`\n` +
`*RAM:* \`${freeMemory} / ${totalMemory}\`\n` +
`*Load Average:* \`${loadAverage}\`\n` +
`*Uptime:* \`${uptime}\`\n\n`;
`*OS:* \`${platform()} ${release()}\`\n` +
`*Arch:* \`${arch()}\`\n` +
`*Node.js Version:* \`${process.version}\`\n` +
`*CPU:* \`${cpu.model}\`\n` +
`*CPU Cores:* \`${cpus().length} cores\`\n` +
`*RAM:* \`${(freemem() / (1024 ** 3)).toFixed(2)} GB / ${(totalmem() / (1024 ** 3)).toFixed(2)} GB\`\n` +
`*Load Average:* \`${loadavg().map(avg => avg.toFixed(2)).join(', ')}\`\n` +
`*Uptime:* \`${formatUptime(uptime())}\`\n\n`;
}
async function handleAdminCommand(ctx, action, successMessage, errorMessage) {
const Strings = getStrings(ctx.from.language_code);
const userId = ctx.from.id;
if (Config.admins.includes(userId)) {
try {
await action();
ctx.reply(successMessage, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
});
} catch (error) {
ctx.reply(errorMessage.replace('{error}', error.message), {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
});
}
} else {
ctx.reply(Strings.botAdminOnly, {
reply_to_message_id: ctx.message.message_id
});
}
}
module.exports = (bot) => {
bot.command('getbotstats', spamwatchMiddleware, async (ctx) => {
const Strings = getStrings(ctx.from.language_code);
const userId = ctx.from.id || Strings.unKnown;
if (Config.admins.includes(userId)) {
const machineStats = getSystemInfo();
ctx.reply(
machineStats, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
}
).catch(error => ctx.reply(
"Error when getting server status:\n" + error, {
reply_to_message_id: ctx.message.message_id
}
));
} else {
ctx.reply(Strings.botAdminOnly, {
handleAdminCommand(ctx, async () => {
const stats = getSystemInfo();
await ctx.reply(stats, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
});
}
}, '', Strings.errorRetrievingStats); // No success message
});
bot.command('setbotname', spamwatchMiddleware, async (ctx) => {
const Strings = getStrings(ctx.from.language_code);
const userId = ctx.from.id || Strings.unKnown;
if (Config.admins.includes(userId)) {
const botName = ctx.message.text.split(' ').slice(1).join(' ');
const botNameReport = Strings.botNameChanged ? Strings.botNameChanged.replace('{botName}', botName) : `Bot name changed to ${botName}`;
try {
await ctx.telegram.setMyName(botName);
ctx.reply(
botNameReport, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
}
);
} catch (error) {
const botNameErr = Strings.botNameErr ? Strings.botNameErr.replace('{tgErr}', error.message) : `Error setting bot name: ${error.message}`;
ctx.reply(
botNameErr, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
}
);
}
} else {
ctx.reply(Strings.botAdminOnly, {
reply_to_message_id: ctx.message.message_id
});
}
const botName = ctx.message.text.split(' ').slice(1).join(' ');
handleAdminCommand(ctx, async () => {
await ctx.telegram.setMyName(botName);
}, Strings.botNameChanged.replace('{botName}', botName), Strings.botNameErr.replace('{tgErr}', '{error}'));
});
bot.command('setbotdesc', spamwatchMiddleware, async (ctx) => {
const Strings = getStrings(ctx.from.language_code);
const userId = ctx.from.id || Strings.unKnown;
if (Config.admins.includes(userId)) {
const botDesc = ctx.message.text.split(' ').slice(1).join(' ');
const botDescReport = Strings.botDescChanged ? Strings.botDescChanged.replace('{botDesc}', botDesc) : `Bot description changed to ${botDesc}`;
try {
await ctx.telegram.setMyDescription(botDesc);
ctx.reply(
botDescReport, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
}
);
} catch (error) {
const botDescErr = Strings.botDescErr ? Strings.botDescErr.replace('{tgErr}', error.message) : `Error setting bot description: ${error.message}`;
ctx.reply(
botDescErr, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
}
);
}
} else {
ctx.reply(
Strings.botAdminOnly, {
reply_to_message_id: ctx.message.message_id
});
}
const botDesc = ctx.message.text.split(' ').slice(1).join(' ');
handleAdminCommand(ctx, async () => {
await ctx.telegram.setMyDescription(botDesc);
}, Strings.botDescChanged.replace('{botDesc}', botDesc), Strings.botDescErr.replace('{tgErr}', '{error}'));
});
bot.command('botkickme', spamwatchMiddleware, async (ctx) => {
const Strings = getStrings(ctx.from.language_code);
const chatId = ctx.chat.id || Strings.unKnown;
ctx.reply(
Strings.kickingMyself, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
ctx.reply(Strings.kickingMyself, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
});
ctx.telegram.leaveChat(chatId);
await ctx.telegram.leaveChat(ctx.chat.id);
});
};