Skip to content

Instantly share code, notes, and snippets.

@TheRealOwenRees
Last active February 4, 2025 07:53
Show Gist options
  • Select an option

  • Save TheRealOwenRees/3da4b3d685a6f8ac97b599779dc19cb2 to your computer and use it in GitHub Desktop.

Select an option

Save TheRealOwenRees/3da4b3d685a6f8ac97b599779dc19cb2 to your computer and use it in GitHub Desktop.
Basic Discord Logger
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