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

@ -15,88 +15,50 @@ async function collectInfo(ctx) {
return { Strings, chatId, userId, isAdmin, onCrew };
}
async function handleMember(ctx, action, successMessage, errorMessage) {
const { Strings, chatId, userId, isAdmin, onCrew } = await collectInfo(ctx);
if (onCrew || isAdmin) {
if (isNaN(userId)) {
return ctx.reply(Strings.invalidId, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
});
}
try {
await action(chatId, userId);
const report = successMessage.replace('{userId}', userId);
ctx.reply(report, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
});
} catch (err) {
const error = errorMessage.replace('{tgErr}', err.message);
ctx.reply(error, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
});
}
} else {
ctx.reply(Strings.noPermission, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
});
}
}
module.exports = (bot) => {
bot.command('ban', spamwatchMiddleware, async (ctx) => {
const info = await collectInfo(ctx);
const { Strings, chatId, userId, isAdmin, onCrew } = info;
if (onCrew || isAdmin) {
if (userId === NaN) {
return ctx.reply(
Strings.invalidId, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
}
);
} else {
try {
await ctx.telegram.kickChatMember(chatId, userId);
const banReport = Strings.banSuccess.replace('{userId}', userId);
ctx.reply(
banReport, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
}
);
} catch (err) {
const banErr = Strings.banErr.replace('{tgErr}', err);
ctx.reply(
banErr, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
}
);
};
};
} else {
ctx.reply(
Strings.noPermission, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
}
);
};
bot.command('ban', spamwatchMiddleware, (ctx) => {
handleMember(ctx, (chatId, userId) => ctx.telegram.kickChatMember(chatId, userId),
getStrings(ctx.from.language_code).banSuccess,
getStrings(ctx.from.language_code).banErr
);
});
bot.command('unban', spamwatchMiddleware, async (ctx) => {
const info = await collectInfo(ctx);
const { Strings, chatId, userId, isAdmin, onCrew } = info;
if (onCrew || isAdmin) {
if (userId === NaN) {
return ctx.reply(
Strings.invalidId, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
}
);
} else {
try {
await ctx.telegram.unbanChatMember(chatId, userId);
const unBanReport = Strings.unBanSuccess.replace('{userId}', userId);
ctx.reply(
unBanReport, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
}
);
} catch (err) {
const unBanErr = Strings.unBanErr.replace('{tgErr}', err);
ctx.reply(
unBanErr, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
}
);
};
};
} else {
ctx.reply(
Strings.noPermission, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
}
);
};
bot.command('unban', spamwatchMiddleware, (ctx) => {
handleMember(ctx, (chatId, userId) => ctx.telegram.unbanChatMember(chatId, userId),
getStrings(ctx.from.language_code).unBanSuccess,
getStrings(ctx.from.language_code).unBanErr
);
});
};
};

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);
});
};

View file

@ -3,69 +3,73 @@ const { getStrings } = require('../plugins/checklang.js');
const { isOnSpamWatch } = require('../plugins/lib-spamwatch/spamwatch.js');
const spamwatchMiddleware = require('../plugins/lib-spamwatch/Middleware.js')(isOnSpamWatch);
function furryFunction(ctx) {
function sendRandomReply(ctx, gifUrl, textKey, notTextKey) {
const Strings = getStrings(ctx.from.language_code);
if (Math.random() < 0.5 ? "yes" : "no" === "yes") {
ctx.replyWithAnimation(
resources.furryGif, {
caption: Strings.isFurry,
const shouldSendGif = Math.random() < 0.5;
if (shouldSendGif) {
ctx.replyWithAnimation(gifUrl, {
caption: Strings[textKey],
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
}).catch(err => {
gifErr = gifErr.replace('{err}', err);
ctx.reply(Strings.gifErr, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
}
);
});
});
} else {
ctx.reply(
Strings.isNtFurry, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
}
);
ctx.reply(Strings[notTextKey], {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
});
}
}
function gayFunction(ctx) {
async function handleDiceCommand(ctx, emoji, delay) {
const Strings = getStrings(ctx.from.language_code);
if (Math.random() < 0.5 ? "yes" : "no" === "yes") {
ctx.replyWithAnimation(
resources.gayFlag, {
caption: Strings.isGay,
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
}
);
} else {
ctx.reply(
Strings.isNtGay, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
}
);
}
const result = await ctx.sendDice({ emoji, reply_to_message_id: ctx.message.message_id });
const botResponse = Strings.funEmojiResult
.replace('{emoji}', result.dice.emoji)
.replace('{value}', result.dice.value);
setTimeout(() => {
ctx.reply(botResponse, {
parse_mode: 'Markdown',
reply_to_message_id: ctx.message.message_id
});
}, delay);
}
module.exports = (bot) => {
bot.command('dice', spamwatchMiddleware, async (ctx) => {
ctx.telegram.sendDice(
ctx.chat.id, {
reply_to_message_id: ctx.message.message_id
}
);
await handleDiceCommand(ctx, undefined, 4000);
});
bot.command('slot', spamwatchMiddleware, async (ctx) => {
ctx.telegram.sendDice(
ctx.chat.id, {
emoji: '🎰',
reply_to_message_id: ctx.message.message_id
}
);
await handleDiceCommand(ctx, '🎰', 3000);
});
bot.command('ball', spamwatchMiddleware, async (ctx) => {
await handleDiceCommand(ctx, '⚽', 3000);
});
bot.command('idice', spamwatchMiddleware, async (ctx) => {
const stickerId = "CAACAgQAAxkBAAJxjWbSSP-8ZNEhEpAJjQsHsGf-UuEPAAJCAAPI-uwTAAEBVWWh4ucINQQ";
ctx.replyWithSticker(stickerId, {
reply_to_message_id: ctx.message.message_id
});
});
bot.command('furry', spamwatchMiddleware, async (ctx) => {
furryFunction(ctx);
sendRandomReply(ctx, resources.furryGif, 'isFurry', 'isNtFurry');
});
bot.command('gay', spamwatchMiddleware, async (ctx) => {
gayFunction(ctx);
sendRandomReply(ctx, resources.gayFlag, 'isGay', 'isNtGay');
});
};
};

View file

@ -4,48 +4,29 @@ const spamwatchMiddleware = require('../plugins/lib-spamwatch/Middleware.js')(is
async function getUserInfo(ctx) {
const Strings = getStrings(ctx.from.language_code);
let userInfoTemplate = Strings.userInfo;
const userName = ctx.from.first_name || Strings.unKnown;
const userId = ctx.from.id || Strings.unKnown;
const userHandle = ctx.from.username ? `@${ctx.from.username}` : Strings.varNone;
const isBot = ctx.from.is_bot ? Strings.varYes : Strings.varNo;
const userPremium = ctx.from.is_premium ? Strings.varYes : Strings.varNo;
const userLang = ctx.from.language_code || Strings.unKnown;
userInfo = Strings.userInfo
.replace('{userName}', ctx.from.first_name || Strings.unKnown)
.replace('{userId}', ctx.from.id || Strings.unKnown)
.replace('{userHandle}', ctx.from.username ? `@${ctx.from.username}` : Strings.varNone)
.replace('{userPremium}', ctx.from.is_premium ? Strings.varYes : Strings.varNo)
.replace('{userLang}', ctx.from.language_code || Strings.unKnown);
userInfoTemplate = userInfoTemplate
.replace('{userName}', userName)
.replace('{userId}', userId)
.replace('{userHandle}', userHandle)
.replace('{isBot}', isBot)
.replace('{userPremium}', userPremium)
.replace('{userLang}', userLang);
return userInfoTemplate;
return userInfo;
}
async function getChatInfo(ctx) {
const Strings = getStrings(ctx.from.language_code);
if (ctx.chat.type === 'group' || ctx.chat.type === 'supergroup') {
let chatInfoTemplate = Strings.chatInfo;
const chatId = ctx.chat.id || Strings.unKnown;
const chatName = ctx.chat.title;
const chatHandle = ctx.chat.username ? `@${ctx.chat.username}` : Strings.varNone;
const chatType = ctx.chat.type || Strings.unKnown;
chatInfo = Strings.chatInfo
.replace('{chatId}', ctx.chat.id || Strings.unKnown)
.replace('{chatName}', ctx.chat.title || Strings.unKnown)
.replace('{chatHandle}', ctx.chat.username ? `@${ctx.chat.username}` : Strings.varNone)
.replace('{chatMembersCount}', await ctx.getChatMembersCount(ctx.chat.id || Strings.unKnown))
.replace('{chatType}', ctx.chat.type || Strings.unKnown)
.replace('{isForum}', ctx.chat.is_forum ? Strings.varYes : Strings.varNo);
const chatMembersCount = await ctx.telegram.getChatMembersCount(chatId);
const isForum = ctx.chat.is_forum ? Strings.varYes : Strings.varNo;
chatInfoTemplate = chatInfoTemplate
.replace('{chatId}', chatId)
.replace('{chatName}', chatName)
.replace('{chatHandle}', chatHandle)
.replace('{chatMembersCount}', chatMembersCount)
.replace('{chatType}', chatType)
.replace('{isForum}', isForum);
return chatInfoTemplate;
return chatInfo;
} else {
return ctx.reply(
Strings.groupOnly, {