Skip to content

Commit

Permalink
improve reports
Browse files Browse the repository at this point in the history
  • Loading branch information
backmeupplz committed Nov 23, 2019
1 parent 8137adf commit ea066d9
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/commands/captchaMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function setupCaptchaMessage(bot: Telegraf<ContextMessageUpdate>) {
Extra.inReplyTo(ctx.message.message_id) as ExtraReplyMessage
)
} catch (err) {
report(bot, err, ctx)
report(err)
} finally {
next()
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/greeting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export function setupGreeting(bot: Telegraf<ContextMessageUpdate>) {
Extra.inReplyTo(ctx.message.message_id) as ExtraReplyMessage
)
} catch (err) {
report(bot, err, ctx)
report(err)
} finally {
next()
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/trust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function setupTrust(bot: Telegraf<ContextMessageUpdate>) {
can_add_web_page_previews: true,
})
} catch (err) {
await report(bot, err, ctx)
await report(err)
}
// Unrestrict in shieldy
ctx.dbchat.restrictedUsers = ctx.dbchat.restrictedUsers.filter(
Expand All @@ -36,7 +36,7 @@ export function setupTrust(bot: Telegraf<ContextMessageUpdate>) {
try {
await ctx.telegram.deleteMessage(ctx.dbchat.id, candidate.messageId)
} catch (err) {
await report(bot, err, ctx)
await report(err)
}
// Remove from candidates
ctx.dbchat.candidates = ctx.dbchat.candidates.filter(
Expand Down
34 changes: 17 additions & 17 deletions src/helpers/newcomers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function setupNewcomers(bot: Telegraf<ContextMessageUpdate>) {
disable_web_page_preview: true,
})
} catch (err) {
await report(bot, err, ctx)
await report(err)
}
continue
}
Expand Down Expand Up @@ -75,7 +75,7 @@ export function setupNewcomers(bot: Telegraf<ContextMessageUpdate>) {
try {
message = await notifyCandidate(ctx, member, equation)
} catch (err) {
await report(bot, err, ctx)
await report(err)
}
candidatesToAdd.push({
id: member.id,
Expand Down Expand Up @@ -120,7 +120,7 @@ export function setupNewcomers(bot: Telegraf<ContextMessageUpdate>) {
)
}
} catch (err) {
await report(bot, err, ctx)
await report(err)
}
}
}
Expand Down Expand Up @@ -148,7 +148,7 @@ export function setupNewcomers(bot: Telegraf<ContextMessageUpdate>) {
ctx.message!.message_id
)
} catch (err) {
await report(bot, err, ctx)
await report(err)
}
}
} finally {
Expand All @@ -164,7 +164,7 @@ export function setupNewcomers(bot: Telegraf<ContextMessageUpdate>) {
try {
await ctx.telegram.deleteMessage(ctx.chat!.id, ctx.message!.message_id)
} catch (err) {
await report(bot, err, ctx)
await report(err)
}
}
})
Expand Down Expand Up @@ -196,7 +196,7 @@ export function setupNewcomers(bot: Telegraf<ContextMessageUpdate>) {
try {
await ctx.deleteMessage()
} catch (err) {
await report(bot, err, ctx)
await report(err)
}
return
}
Expand All @@ -205,15 +205,15 @@ export function setupNewcomers(bot: Telegraf<ContextMessageUpdate>) {
try {
await ctx.deleteMessage()
} catch (err) {
await report(bot, err, ctx)
await report(err)
}
}
console.log(`🔥 Removing ${userId} from candidates of ${ctx.chat.id}`)
chat.candidates = chat.candidates.filter(c => c.id !== userId)
try {
ctx.dbchat = await chat.save()
} catch (err) {
await report(bot, err, ctx)
await report(err)
}
console.log(
`✅ Resulting candidates of ${ctx.chat.id}: ${chat.candidates.map(v =>
Expand All @@ -223,7 +223,7 @@ export function setupNewcomers(bot: Telegraf<ContextMessageUpdate>) {
try {
await ctx.telegram.deleteMessage(ctx.chat!.id, candidate.messageId)
} catch (err) {
await report(bot, err, ctx)
await report(err)
}
try {
if (chat.greetsUsers && chat.greetingMessage) {
Expand Down Expand Up @@ -258,7 +258,7 @@ export function setupNewcomers(bot: Telegraf<ContextMessageUpdate>) {
}
}
} catch (err) {
await report(bot, err, ctx)
await report(err)
}
next()
})
Expand All @@ -274,7 +274,7 @@ export function setupNewcomers(bot: Telegraf<ContextMessageUpdate>) {
try {
await ctx.answerCbQuery(strings(chat, 'only_candidate_can_reply'))
} catch (err) {
await report(bot, err, ctx)
await report(err)
}
return
}
Expand All @@ -287,7 +287,7 @@ export function setupNewcomers(bot: Telegraf<ContextMessageUpdate>) {
try {
await chat.save()
} catch (err) {
await report(bot, err, ctx)
await report(err)
}
console.log(
`✅ Resulting candidates of ${ctx.chat.id}: ${chat.candidates.map(v =>
Expand All @@ -297,7 +297,7 @@ export function setupNewcomers(bot: Telegraf<ContextMessageUpdate>) {
try {
await ctx.telegram.deleteMessage(ctx.chat!.id, candidate.messageId)
} catch (err) {
await report(bot, err, ctx)
await report(err)
}
try {
if (chat.greetsUsers && chat.greetingMessage) {
Expand Down Expand Up @@ -334,7 +334,7 @@ export function setupNewcomers(bot: Telegraf<ContextMessageUpdate>) {
}
}
} catch (err) {
await report(bot, err, ctx)
await report(err)
}
})
}
Expand Down Expand Up @@ -446,13 +446,13 @@ async function check() {
if (checkIfErrorDismissable(err)) {
candidatesToDelete.push(candidate)
} else {
await report(bot, err, undefined, 'kickChatMember')
await report(err, 'kickChatMember')
}
}
try {
await bot.telegram.deleteMessage(chat.id, candidate.messageId)
} catch (err) {
await report(bot, err, undefined, 'deleteMessage')
await report(err, 'deleteMessage')
}
}
const idsToDelete = candidatesToDelete.map(c => c.id)
Expand Down Expand Up @@ -482,7 +482,7 @@ async function check() {
}
}
} catch (err) {
report(bot, err, undefined, 'checking candidates')
report(err, 'checking candidates')
} finally {
checking = false
}
Expand Down
61 changes: 29 additions & 32 deletions src/helpers/report.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
// Dependencies
import Telegraf, { ContextMessageUpdate } from 'telegraf'
import { checkIfErrorDismissable } from './error'
import { bot } from './bot'

export async function report(
bot: Telegraf<ContextMessageUpdate>,
error: Error,
ctx?: ContextMessageUpdate,
reason?: string
) {
if (checkIfErrorDismissable(error)) {
let errorsToReport = []

async function bulkReport() {
const tempErrorsToReport = errorsToReport
errorsToReport = []
const adminChatId = process.env.ADMIN
if (!adminChatId) {
return
}
const adminChatId = process.env.ADMIN
if (!adminChatId) return
try {
await bot.telegram.sendMessage(
adminChatId,
`${reason ? `${reason}\n` : ''}<code>${error.message}</code>\n\n<code>${
error.stack
}</code>${
ctx
? `${JSON.stringify(
ctx.message ||
ctx.channelPost ||
ctx.inlineQuery ||
ctx.callbackQuery,
undefined,
2
)}`
: ''
}`,
{
parse_mode: 'HTML',
} as any
if (tempErrorsToReport.length > 15) {
const reportText = tempErrorsToReport.reduce(
(prev, cur) => `${prev}${cur}\n`,
''
)
} catch (err) {
console.error(err)
const chunks = reportText.match(/[\s\S]{1,4000}/g)
for (const chunk of chunks) {
try {
await bot.telegram.sendMessage(adminChatId, chunk)
} catch (err) {
console.error(err)
}
}
}
}

setInterval(bulkReport, 60 * 1000)

export async function report(error: Error, reason?: string) {
if (checkIfErrorDismissable(error)) {
return
}
errorsToReport.push(`${reason ? `${reason}\n` : ''}${error.message}`)
}
2 changes: 1 addition & 1 deletion src/middlewares/attachUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function attachUser(ctx: ContextMessageUpdate, next) {
const chat = await findChat(ctx.chat.id)
ctx.dbchat = chat
} catch (err) {
await report(bot, err, ctx)
await report(err)
}
next()
}
6 changes: 3 additions & 3 deletions src/middlewares/checkRestrict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export async function checkRestrict(
message &&
((message.entities && message.entities.length) ||
(message.caption_entities && message.caption_entities.length) ||
(message.forward_from ||
message.forward_from ||
message.forward_date ||
message.forward_from_chat) ||
message.forward_from_chat ||
message.document ||
message.sticker ||
message.photo ||
Expand All @@ -40,7 +40,7 @@ export async function checkRestrict(
try {
await ctx.telegram.deleteMessage(ctx.chat.id, message.message_id)
} catch (err) {
await report(bot, err, ctx)
await report(err)
}
} else {
next()
Expand Down

0 comments on commit ea066d9

Please sign in to comment.