|
1 | | -import { APIEmbed, Client, Guild } from 'discord.js'; |
| 1 | +import { |
| 2 | + APIEmbed, |
| 3 | + Client, |
| 4 | + CommandInteraction, |
| 5 | + Guild, |
| 6 | + GuildChannel, |
| 7 | + WebhookClient, |
| 8 | +} from 'discord.js'; |
| 9 | +import { capitalize, isEmpty } from 'lodash'; |
| 10 | +import { ERROR_NOTIFICATION_WEBHOOK_URL } from '../config/environment'; |
| 11 | +import { v4 as uuid } from 'uuid'; |
| 12 | + |
| 13 | +export const codeMark = (text: string) => { |
| 14 | + return '`' + text + '`'; |
| 15 | +}; |
2 | 16 |
|
3 | 17 | export const serverNotificationEmbed = async ({ |
4 | 18 | app, |
@@ -44,3 +58,71 @@ export const serverNotificationEmbed = async ({ |
44 | 58 | }; |
45 | 59 | return embed; |
46 | 60 | }; |
| 61 | + |
| 62 | +export const sendErrorLog = async ({ |
| 63 | + error, |
| 64 | + interaction, |
| 65 | +}: { |
| 66 | + error: any; |
| 67 | + interaction?: CommandInteraction; |
| 68 | +}) => { |
| 69 | + console.error(error); |
| 70 | + const errorID = uuid(); |
| 71 | + if (interaction) { |
| 72 | + const errorEmbed = { |
| 73 | + description: `Oops something went wrong! D:\n\nError: ${ |
| 74 | + error.message ? codeMark(error.message) : codeMark('Unexpected Error') |
| 75 | + }\nError ID: ${codeMark(errorID)}`, |
| 76 | + color: 16711680, |
| 77 | + }; |
| 78 | + await interaction.editReply({ embeds: [errorEmbed] }); |
| 79 | + } |
| 80 | + if (ERROR_NOTIFICATION_WEBHOOK_URL && !isEmpty(ERROR_NOTIFICATION_WEBHOOK_URL)) { |
| 81 | + const interactionChannel = interaction?.channel as GuildChannel | undefined; |
| 82 | + const notificationEmbed: APIEmbed = { |
| 83 | + title: interaction ? `Error | ${capitalize(interaction.commandName)} Command` : 'Error', |
| 84 | + color: 16711680, |
| 85 | + description: `uuid: ${errorID}\nError: ${error.message ? error.message : 'Unexpected Error'}`, |
| 86 | + fields: interaction |
| 87 | + ? [ |
| 88 | + { |
| 89 | + name: 'User', |
| 90 | + value: interaction.user.username, |
| 91 | + inline: true, |
| 92 | + }, |
| 93 | + { |
| 94 | + name: 'User ID', |
| 95 | + value: interaction.user.id, |
| 96 | + inline: true, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: 'Channel', |
| 100 | + value: interactionChannel ? interactionChannel.name : '-', |
| 101 | + inline: true, |
| 102 | + }, |
| 103 | + { |
| 104 | + name: 'Channel ID', |
| 105 | + value: interaction.channelId, |
| 106 | + inline: true, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: 'Guild', |
| 110 | + value: interaction.guild ? interaction.guild.name : '-', |
| 111 | + inline: true, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: 'Guild ID', |
| 115 | + value: interaction.guildId ? interaction.guildId : '-', |
| 116 | + inline: true, |
| 117 | + }, |
| 118 | + ] |
| 119 | + : undefined, |
| 120 | + }; |
| 121 | + const notificationWebhook = new WebhookClient({ url: ERROR_NOTIFICATION_WEBHOOK_URL }); |
| 122 | + await notificationWebhook.send({ |
| 123 | + embeds: [notificationEmbed], |
| 124 | + username: 'My App Error Notification', |
| 125 | + avatarURL: '', |
| 126 | + }); |
| 127 | + } |
| 128 | +}; |
0 commit comments