2
0
mirror of https://github.com/thedevs-network/the-guard-bot synced 2025-08-31 14:15:25 +00:00

Added and used scheduleDeletion

...only where I was certain that whis was a good idea,
perhaps it could be useful in more places.
This commit is contained in:
GingerPlusPlus
2017-10-31 22:20:49 +01:00
parent 84e178f363
commit 337f6e2183
12 changed files with 45 additions and 37 deletions

View File

@@ -1,7 +1,7 @@
'use strict';
// Utils
const { link } = require('../../utils/tg');
const { link, scheduleDeletion } = require('../../utils/tg');
const { logError } = require('../../utils/log');
// Bot
@@ -24,7 +24,7 @@ const banHandler = async ({ chat, message, reply, telegram, me, state }) => {
if (!userToBan) {
return reply(' <b>Reply to a message or mention a user.</b>',
replyOptions);
replyOptions).then(scheduleDeletion);
}
if (message.chat.type === 'private' || userToBan.username === me) {
@@ -36,7 +36,8 @@ const banHandler = async ({ chat, message, reply, telegram, me, state }) => {
}
if (reason.length === 0) {
return reply(' <b>Need a reason to ban.</b>', replyOptions);
return reply(' <b>Need a reason to ban.</b>', replyOptions)
.then(scheduleDeletion);
}
if (message.reply_to_message) {

View File

@@ -1,7 +1,7 @@
'use strict';
// Utils
const { link } = require('../../utils/tg');
const { link, scheduleDeletion } = require('../../utils/tg');
// Bot
const { replyOptions } = require('../../bot/options');
@@ -20,7 +20,7 @@ const getWarnsHandler = async ({ message, reply, state }) => {
: null;
if (!theUser) {
return reply(' <b>Reply to a message or mention a user.</b>',
replyOptions);
replyOptions).then(scheduleDeletion);
}
let i = 0;
const warns = await getWarns(theUser);

View File

@@ -1,10 +1,7 @@
'use strict';
// Utils
const { escapeHtml } = require('../../utils/tg');
// Bot
const bot = require('../../bot');
const { escapeHtml, scheduleDeletion } = require('../../utils/tg');
// DB
const { listGroups } = require('../../stores/group');
@@ -19,7 +16,7 @@ const entry = group => group.username
? `- @${group.username}`
: `- <a href="${group.link}">${escapeHtml(group.title)}</a>`;
const groupsHandler = async ({ chat, replyWithHTML }) => {
const groupsHandler = async ({ replyWithHTML }) => {
if (config.groupsString) {
return replyWithHTML(config.groupsString);
}
@@ -28,15 +25,11 @@ const groupsHandler = async ({ chat, replyWithHTML }) => {
const entries = groups.map(entry).join('\n');
const { message_id } = await replyWithHTML(
return replyWithHTML(
`🛠 <b>Groups I manage</b>:\n\n${entries}`, {
disable_web_page_preview: true,
reply_markup,
});
return setTimeout(() =>
bot.telegram.deleteMessage(chat.id, message_id), 5 * 60 * 1000);
}).then(scheduleDeletion);
};
module.exports = groupsHandler;

View File

@@ -1,7 +1,7 @@
'use strict';
// Bot
const bot = require('../../bot');
// Utils
const { scheduleDeletion } = require('../../utils/tg');
// DB
const { managesGroup } = require('../../stores/group');
@@ -9,12 +9,10 @@ const { managesGroup } = require('../../stores/group');
const linkHandler = async ({ chat, replyWithHTML }) => {
const group = await managesGroup({ id: chat.id });
const { message_id } = await replyWithHTML(
return replyWithHTML(
' <b>Group\'s link:</b>\n\n' +
`<a href="${group.link}">${group.title}</a>`
);
return setTimeout(() =>
bot.telegram.deleteMessage(chat.id, message_id), 5 * 60 * 1000);
).then(scheduleDeletion);
};
module.exports = linkHandler;

View File

@@ -1,7 +1,7 @@
'use strict';
// Utils
const { link } = require('../../utils/tg');
const { link, scheduleDeletion } = require('../../utils/tg');
const { logError } = require('../../utils/log');
// Bot
@@ -22,7 +22,7 @@ const nowarnsHandler = async ({ message, reply, state }) => {
if (!userToUnwarn) {
return reply(' <b>Reply to a message or mention a user.</b>',
replyOptions);
replyOptions).then(scheduleDeletion);
}
const warns = await getWarns(userToUnwarn);

View File

@@ -1,7 +1,7 @@
'use strict';
// Utils
const { link } = require('../../utils/tg');
const { link, scheduleDeletion } = require('../../utils/tg');
// Bot
const { replyOptions } = require('../../bot/options');
@@ -13,7 +13,7 @@ const reportHandler = async ctx => {
const msg = ctx.message;
if (!msg.reply_to_message) {
return ctx.reply(' <b>Reply to message you\'d like to report</b>',
replyOptions);
replyOptions).then(scheduleDeletion);
}
const admins = await getAdmins();
const adminObjects = admins.map(user => ({

View File

@@ -1,7 +1,7 @@
'use strict';
// Utils
const { quietLink } = require('../../utils/tg');
const { quietLink, scheduleDeletion } = require('../../utils/tg');
// DB
const { getAdmins } = require('../../stores/user');
@@ -16,7 +16,7 @@ const staffHandler = async ctx => {
return ctx.replyWithHTML(`<b>Admins in the network:</b>\n\n${list}`, {
disable_notification: true,
disable_web_page_preview: true,
});
}).then(scheduleDeletion);
};
module.exports = staffHandler;

View File

@@ -1,7 +1,7 @@
'use strict';
// Utils
const { link } = require('../../utils/tg');
const { link, scheduleDeletion } = require('../../utils/tg');
const { logError } = require('../../utils/log');
// Bot
@@ -22,7 +22,7 @@ const unAdminHandler = async ({ message, reply, state }) => {
if (!userToUnadmin) {
return reply(' <b>Reply to a message or mention a user.</b>',
replyOptions);
replyOptions).then(scheduleDeletion);
}
if (!await isAdmin(userToUnadmin)) {

View File

@@ -1,7 +1,7 @@
'use strict';
// Utils
const { link } = require('../../utils/tg');
const { link, scheduleDeletion } = require('../../utils/tg');
const { logError } = require('../../utils/log');
// Bot
@@ -25,7 +25,7 @@ const unbanHandler = async ({ message, reply, telegram, state }) => {
if (!userToUnban) {
return reply(' <b>Reply to a message or mention a user.</b>',
replyOptions);
replyOptions).then(scheduleDeletion);
}

View File

@@ -1,7 +1,7 @@
'use strict';
// Utils
const { link } = require('../../utils/tg');
const { link, scheduleDeletion } = require('../../utils/tg');
// Bot
const { replyOptions } = require('../../bot/options');
@@ -22,7 +22,7 @@ const unwarnHandler = async ({ message, reply, state, telegram }) => {
if (!userToUnwarn) {
return reply(' <b>Reply to a message or mention a user.</b>',
replyOptions);
replyOptions).then(scheduleDeletion);
}
const allWarns = await getWarns(userToUnwarn);

View File

@@ -1,7 +1,7 @@
'use strict';
// Utils
const { link } = require('../../utils/tg');
const { link, scheduleDeletion } = require('../../utils/tg');
const { logError } = require('../../utils/log');
// Config
@@ -26,7 +26,7 @@ const warnHandler = async ({ message, chat, reply, me, state }) => {
if (!userToWarn) {
return reply(' <b>Reply to a message or mentoin a user.</b>',
replyOptions);
replyOptions).then(scheduleDeletion);
}
if (message.chat.type === 'private' || userToWarn.username === me) {
@@ -40,7 +40,8 @@ const warnHandler = async ({ message, chat, reply, me, state }) => {
}
if (reason.length === 0) {
return reply(' <b>Need a reason to warn.</b>', replyOptions);
return reply(' <b>Need a reason to warn.</b>', replyOptions)
.then(scheduleDeletion);
}
await warn(userToWarn, reason);

View File

@@ -1,5 +1,11 @@
'use strict';
const { telegram } = require('../bot');
const { promisify } = require('util');
const delay = promisify(setTimeout);
const escapeHtml = s => s
.replace(/</g, '&lt;');
@@ -21,9 +27,18 @@ const deleteAfter = ms => ctx =>
ctx.telegram.deleteMessage(ctx.chat.id, ctx.message.message_id),
ms);
const scheduleDeletion = async ({ chat, message_id }) => {
if (chat.type === 'private') {
return null;
}
await delay(5 * 60 * 1000);
return telegram.deleteMessage(chat.id, message_id);
};
module.exports = {
deleteAfter,
escapeHtml,
link,
quietLink,
scheduleDeletion,
};