Skip to content

Commit

Permalink
Catch errors in shame and report to admins (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonas Bäck <63725059+backjonas@users.noreply.github.com>
  • Loading branch information
christiansegercrantz and backjonas authored Dec 11, 2024
1 parent ee13b1a commit 02060d1
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/admin/saldo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,8 @@ bot.hears(/^\/shame(?:_(\d+))?$/, async (ctx) => {
const balances = (await getAllBalances()).filter(
(obj) => obj.balance < -saldoCutOff
)

for await (const { userId, balance } of balances) {
const problem_cases = []
for await (const { userId, balance, userName } of balances) {
const message =
`Ert saldo är nu <b>${balance.toFixed(
2
Expand All @@ -245,16 +245,27 @@ bot.hears(/^\/shame(?:_(\d+))?$/, async (ctx) => {
2
)}€, till följande konto: ` +
formattedAccountString
await ctx.telegram.sendMessage(userId, message, {
parse_mode: 'HTML',
})
try {
await ctx.telegram.sendMessage(userId, message, {
parse_mode: 'HTML',
})
} catch (error) {
console.log(`User "${userName}" has probably not started a chat with the bot`, error)
problem_cases.push(userName)
}

}

if (balances.length > 0) {
const adminMessage =
var adminMessage =
`Följande användare pingades med en cut-off av -${saldoCutOff}€:<pre>` +
balances.map((b) => `${b.userName}: ${b.balance}`).join('\n') +
'</pre>'
if (problem_cases.length > 0){
adminMessage += `\nFöljande användare kunde inte pingas:<pre>` +
problem_cases.join('\n') +
'</pre>'
}
ctx.telegram.sendMessage(config.adminChatId, adminMessage, {
parse_mode: 'HTML',
})
Expand Down

0 comments on commit 02060d1

Please sign in to comment.