-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #4 add error notify to discord webhook
- Loading branch information
Showing
10 changed files
with
740 additions
and
94 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import dayjs from 'dayjs'; | ||
import {DEFAULT_TZ} from "../lib/Common.js"; | ||
|
||
class AuditLogsSingleton { | ||
constructor() { | ||
this.auditLogs = [];// in memory audit logs | ||
} | ||
|
||
static get() { | ||
if (!AuditLogsSingleton.instance) { | ||
AuditLogsSingleton.instance = new AuditLogsSingleton(); | ||
} | ||
return AuditLogsSingleton.instance; | ||
} | ||
} | ||
|
||
export default class AuditLogsService { | ||
|
||
constructor(discordService) { | ||
this.discordService = discordService; | ||
} | ||
|
||
createAuditLog(log) { | ||
const instant = dayjs(new Date()).tz(DEFAULT_TZ).format("DD/MM/YYYY à HH:mm"); | ||
AuditLogsSingleton.get().auditLogs.push({instant, log}) | ||
} | ||
|
||
retrieveAuditLogs(cleanup = false) { | ||
const logs = AuditLogsSingleton.get().auditLogs; | ||
const clone = [].concat(logs); | ||
if (true === cleanup) { | ||
AuditLogsSingleton.get().auditLogs = []; | ||
} | ||
return clone; | ||
} | ||
|
||
async notifyLogs() { | ||
const logs = this.retrieveAuditLogs(true); | ||
console.log(`*${logs?.length}`);// debug | ||
if (logs === undefined || logs.length < 1) { | ||
return; | ||
} | ||
let markdown = this.formatMarkdownMessageFromLogs(logs); | ||
return await this.discordService.sendMessage(markdown); | ||
} | ||
|
||
formatMarkdownMessageFromLogs(logs) { | ||
const count = logs.length; | ||
const nbMsg = count > 1 ? `${count} messages` : `${count} message`; | ||
let markdown = `${nbMsg} :\n` + "```\n"; | ||
markdown += logs.map(entry => `${entry.instant}|${entry.log}`).join(" \n"); | ||
markdown += "\n```"; | ||
|
||
//~ prevent discord limit | ||
const MAX_DISCORD_MSG_LENGTH = 2000; | ||
if (markdown.length > MAX_DISCORD_MSG_LENGTH) { | ||
markdown = markdown.substring(0, MAX_DISCORD_MSG_LENGTH - 12); | ||
markdown += "\n(...)\n```"; | ||
} | ||
return markdown; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.