Last active
February 4, 2025 07:53
-
-
Save TheRealOwenRees/3da4b3d685a6f8ac97b599779dc19cb2 to your computer and use it in GitHub Desktop.
Basic Discord Logger
This file contains hidden or 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
| type DiscordLogType = 'error' | 'info' | |
| interface IDiscordLog { | |
| type: DiscordLogType | |
| message: string | |
| } | |
| const embedColours: Record<DiscordLogType, number> = { | |
| error: 0xff0000, | |
| info: 0x00ff00 | |
| } | |
| const logger = { | |
| info: (...params: string[]) => { | |
| console.info(...params) | |
| }, | |
| error: (...params: string[] | Error[]) => { | |
| console.error(...params) | |
| }, | |
| discord: async ({ type, message }: IDiscordLog) => { | |
| const date = new Date() | |
| const timestamp = date.toISOString() | |
| const formattedTimestamp = date.toString() | |
| const messageTypeTitle = type.charAt(0).toUpperCase() + type.slice(1) | |
| await fetch(process.env.DISCORD_LOG_WEBHOOK as string, { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json' | |
| }, | |
| body: JSON.stringify({ | |
| embeds: [ | |
| { | |
| title: messageTypeTitle, | |
| color: embedColours[type], | |
| fields: [ | |
| { name: 'Timestamp', value: `${timestamp}\n${formattedTimestamp}` }, | |
| { name: messageTypeTitle, value: message } | |
| ] | |
| } | |
| ] | |
| }) | |
| }) | |
| return true | |
| } | |
| } | |
| export default logger |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment