-
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.
- Loading branch information
1 parent
3d41889
commit b8c8c31
Showing
12 changed files
with
634 additions
and
27 deletions.
There are no files selected for viewing
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,8 @@ | ||
/** | ||
* This interface is used to define the send process options. | ||
* i.e. if the process have to send an email or a webhook. | ||
*/ | ||
export interface SendOptionsInterface { | ||
email: boolean; | ||
webhook: boolean; | ||
} |
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,47 @@ | ||
export interface Author { | ||
name: string; | ||
icon_url: string; | ||
url: string; | ||
} | ||
|
||
export interface Thumbnail { | ||
url: string; | ||
} | ||
|
||
export interface Field { | ||
name: string; | ||
value: string; | ||
inline?: boolean; | ||
} | ||
|
||
export interface Image { | ||
url: string; | ||
} | ||
|
||
export interface Footer { | ||
text: string; | ||
icon_url: string; | ||
} | ||
|
||
export interface EmbedObject { | ||
color?: number; | ||
title: string; | ||
url?: string; | ||
author?: Author; | ||
description?: string; | ||
thumbnail?: Thumbnail; | ||
fields?: Field[]; | ||
image?: Image; | ||
timestamp?: Date; | ||
footer?: Footer; | ||
} | ||
|
||
/** | ||
* Discord channel interface. | ||
*/ | ||
export interface ChannelInterface { | ||
server: string; | ||
name: string; | ||
id: string; | ||
token: string; | ||
} |
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,87 @@ | ||
import axios from "axios"; | ||
import packageJson from "../../../package.json"; | ||
import { logger } from "../../config/logger.config"; | ||
import { ChannelInterface, EmbedObject } from "../../interfaces/webhook.interface"; | ||
|
||
/** | ||
* Discord Webhook Output class. | ||
* | ||
* This class is used to send messages to a Discord channel using a webhook. | ||
*/ | ||
export class WebhookOutput { | ||
/** | ||
* Webhook Discord API configuration. | ||
*/ | ||
private config = { | ||
url: "https://discord.com/api/webhooks", | ||
}; | ||
|
||
/** | ||
* Send content to one of multiple Webhook Discord channel(s). | ||
* @param channels List of Discord channel(s). | ||
* @param content Content to send. | ||
* @param embeds Embed array to send. | ||
* @param username Username to erase the default one. | ||
* @returns If the process was successful or not. | ||
* @throws Error if the process failed. | ||
*/ | ||
public async send( | ||
channels: ChannelInterface[], | ||
content?: string | null, | ||
embeds?: EmbedObject[], | ||
username?: string | ||
): Promise<boolean> { | ||
let responses: Promise<boolean>[] = []; | ||
|
||
const avatarUrl = | ||
"https://raw.githubusercontent.com/size-up/freegamescatcher-core/main/src/assets/freegamescatcher_logo.png"; | ||
|
||
if (channels && channels.length > 0) { | ||
responses = channels.map(async (channel) => { | ||
const channelInformations = `server: [${channel.server}], channel: [${channel.name}]`; | ||
|
||
logger.info(`Sending message to Discord webhook ${channelInformations} ...`); | ||
|
||
try { | ||
const response = await axios.post(`${this.config.url}/${channel.id}/${channel.token}`, { | ||
username: username || packageJson.displayName, | ||
content: content || null, | ||
embeds: embeds || null, | ||
avatar_url: avatarUrl, | ||
}); | ||
|
||
/** | ||
* Send contact message to Discord channel randomly | ||
* with a 25% chance to send it. | ||
*/ | ||
if (Math.random() < 0.25) { | ||
await axios.post(`${this.config.url}/${channel.id}/${channel.token}`, { | ||
username: username || packageJson.displayName, | ||
content: | ||
"Ces messages sont envoyés automatiquement.\nSi vous détectez quelque chose qui vous semble incorrect (un lien mort 💀 ou un bug 🐛) vous pouvez contacter RAIIIIIN#2304 ou Bediver#5058 sur Discord.", | ||
avatar_url: avatarUrl, | ||
}); | ||
} | ||
|
||
if (response.status === 204) { | ||
logger.info(`Message sent to Discord ${channelInformations}`); | ||
return true; | ||
} else { | ||
logger.error( | ||
`Error while sending message to Discord ${channelInformations} with status code: ${response.status}` | ||
); | ||
return false; | ||
} | ||
} catch (error) { | ||
logger.error("Error while sending message to Discord channels", error); | ||
return false; | ||
} | ||
}); | ||
} else { | ||
logger.error("No Discord channel found, no message sent"); | ||
return false; | ||
} | ||
|
||
return !(await Promise.all(responses)).includes(false); | ||
} | ||
} |
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.