|
| 1 | +import filterAxiosError from '@tf2autobot/filter-axios-error'; |
| 2 | +import axios, { AxiosError } from 'axios'; |
| 3 | +import Bot from '../../classes/Bot'; |
| 4 | +import { TradeSummery } from './tradeSummery'; |
| 5 | +import * as tradeSummery from './tradeSummery'; |
| 6 | + |
| 7 | +// eslint-disable-next-line @typescript-eslint/no-empty-interface |
| 8 | +interface WebhookHandler extends TradeSummery {} |
| 9 | + |
| 10 | +export enum WebhookType { |
| 11 | + priceUpdate = 'priceUpdate', |
| 12 | + tradeSummary = 'tradeSummary', |
| 13 | + declinedTrade = 'declinedTrade', |
| 14 | + sendStats = 'sendStats', |
| 15 | + tf2SystemMessage = 'tf2SystemMessage', |
| 16 | + tf2ItemBroadcast = 'tf2ItemBroadcast', |
| 17 | + tf2DisplayNotification = 'tf2DisplayNotification', |
| 18 | + sendAlert = 'sendAlert', |
| 19 | + messages = 'messages' |
| 20 | +} |
| 21 | + |
| 22 | +class WebhookHandler { |
| 23 | + // Lets make a list of what is enabled |
| 24 | + // and what is not |
| 25 | + |
| 26 | + // WebhookHandler is a class that will handle |
| 27 | + // all the webhooks that are enabled in the config |
| 28 | + // and will send them to the correct place |
| 29 | + private enablePriceListUpdate: boolean; |
| 30 | + |
| 31 | + private enableTradeSummary: boolean; |
| 32 | + |
| 33 | + private enableTradeDeclined: boolean; |
| 34 | + |
| 35 | + private enableStats: boolean; |
| 36 | + |
| 37 | + private enableTf2SystemMessage: boolean; |
| 38 | + |
| 39 | + private enableTf2ItemBroadcast: boolean; |
| 40 | + |
| 41 | + private enableTf2DisplayNotification: boolean; |
| 42 | + |
| 43 | + private enableAlert: boolean; |
| 44 | + |
| 45 | + private enableMessages: boolean; |
| 46 | + |
| 47 | + private webhookSecret: string; |
| 48 | + |
| 49 | + constructor(private readonly bot: Bot) { |
| 50 | + this.init(); |
| 51 | + } |
| 52 | + |
| 53 | + private init() { |
| 54 | + // Lets get webhook settings from bot |
| 55 | + const { webhooks } = this.bot.options; |
| 56 | + this.enablePriceListUpdate = webhooks.priceUpdate.enable; |
| 57 | + this.enableTradeSummary = webhooks.tradeSummary.enable; |
| 58 | + this.enableTradeDeclined = webhooks.declinedTrade.enable; |
| 59 | + this.enableStats = webhooks.sendStats.enable; |
| 60 | + this.enableTf2SystemMessage = webhooks.sendTf2Events.systemMessage.enable; |
| 61 | + this.enableTf2ItemBroadcast = webhooks.sendTf2Events.itemBroadcast.enable; |
| 62 | + this.enableTf2DisplayNotification = webhooks.sendTf2Events.displayNotification.enable; |
| 63 | + this.enableAlert = webhooks.sendAlert.enable; |
| 64 | + this.enableMessages = webhooks.messages.enable; |
| 65 | + this.webhookSecret = webhooks.webhookSecret; |
| 66 | + } |
| 67 | + |
| 68 | + private getUrl(type: WebhookType) { |
| 69 | + switch (type) { |
| 70 | + case WebhookType.priceUpdate: |
| 71 | + return this.bot.options.webhooks.priceUpdate.url; |
| 72 | + case WebhookType.tradeSummary: |
| 73 | + return this.bot.options.webhooks.tradeSummary.url; |
| 74 | + case WebhookType.declinedTrade: |
| 75 | + return this.bot.options.webhooks.declinedTrade.url; |
| 76 | + case WebhookType.sendStats: |
| 77 | + return this.bot.options.webhooks.sendStats.url; |
| 78 | + case WebhookType.tf2DisplayNotification: |
| 79 | + return this.bot.options.webhooks.sendTf2Events.displayNotification.url; |
| 80 | + case WebhookType.tf2ItemBroadcast: |
| 81 | + return this.bot.options.webhooks.sendTf2Events.itemBroadcast.url; |
| 82 | + case WebhookType.tf2SystemMessage: |
| 83 | + return this.bot.options.webhooks.sendTf2Events.systemMessage.url; |
| 84 | + case WebhookType.sendAlert: |
| 85 | + return this.bot.options.webhooks.sendAlert.url.main; |
| 86 | + case WebhookType.messages: |
| 87 | + return this.bot.options.webhooks.messages.url; |
| 88 | + default: |
| 89 | + return null; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private isEnabled(type: WebhookType) { |
| 94 | + switch (type) { |
| 95 | + case WebhookType.priceUpdate: |
| 96 | + return this.enablePriceListUpdate; |
| 97 | + case WebhookType.tradeSummary: |
| 98 | + return this.enableTradeSummary; |
| 99 | + case WebhookType.declinedTrade: |
| 100 | + return this.enableTradeDeclined; |
| 101 | + case WebhookType.sendStats: |
| 102 | + return this.enableStats; |
| 103 | + case WebhookType.tf2DisplayNotification: |
| 104 | + return this.enableTf2DisplayNotification; |
| 105 | + case WebhookType.tf2ItemBroadcast: |
| 106 | + return this.enableTf2ItemBroadcast; |
| 107 | + case WebhookType.tf2SystemMessage: |
| 108 | + return this.enableTf2SystemMessage; |
| 109 | + case WebhookType.sendAlert: |
| 110 | + return this.enableAlert; |
| 111 | + case WebhookType.messages: |
| 112 | + return this.enableMessages; |
| 113 | + default: |
| 114 | + return false; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + async sendWebhook(type: WebhookType, data: Record<any, any>) { |
| 119 | + return new Promise((resolve, reject) => { |
| 120 | + const url = this.getUrl(type); |
| 121 | + |
| 122 | + const urls = Array.isArray(url) ? url : [url]; |
| 123 | + // Lets check if the webhook is enabled |
| 124 | + if (this.isEnabled(type)) { |
| 125 | + // Lets send the webhook |
| 126 | + for (const url of urls) { |
| 127 | + void axios({ |
| 128 | + method: 'POST', |
| 129 | + url, |
| 130 | + headers: { |
| 131 | + 'Content-Type': 'application/json', |
| 132 | + 'X-Secret': this.webhookSecret ?? '' |
| 133 | + }, |
| 134 | + data: JSON.stringify(data) |
| 135 | + }) |
| 136 | + .then(() => { |
| 137 | + resolve(); |
| 138 | + }) |
| 139 | + .catch((err: AxiosError) => { |
| 140 | + reject({ err: filterAxiosError(err) }); |
| 141 | + }); |
| 142 | + } |
| 143 | + } else { |
| 144 | + // Webhook is disabled |
| 145 | + resolve(); |
| 146 | + } |
| 147 | + }); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +// Assign tradeSummery to WebhookHandler |
| 152 | +Object.assign(WebhookHandler.prototype, tradeSummery); |
| 153 | + |
| 154 | +export default WebhookHandler; |
0 commit comments