-
-
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.
* feat(env): Add notification config variables for new notification service * refactor(config): Remove discord configuration and add notify configuration * refactor(utils): Simplify message sending logic in Chad flex method
- Loading branch information
Showing
3 changed files
with
47 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,51 @@ | ||
import logger from './logger.js'; | ||
import { discord, env } from '../config/env.js'; | ||
import { env, notify } from '../config/env.js'; | ||
|
||
import axios from 'axios'; | ||
|
||
/* It sends a message to a Discord channel */ | ||
// https://gist.github.com/Birdie0/78ee79402a4301b1faf412ab5f1cdcf9 | ||
export default class Chad { | ||
static async flex(msg, object = null) { | ||
try { | ||
let params = null; | ||
|
||
// use different format to send if we have object passed in | ||
if (object == null) { | ||
params = { username: 'Chad', content: msg }; | ||
} else { | ||
params = { | ||
username: 'Chad', | ||
content: msg, | ||
embeds: [ | ||
{ | ||
title: msg, | ||
description: object, | ||
}, | ||
], | ||
}; | ||
} | ||
if (env !== 'production') { | ||
const res = await fetch(notify.url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-API-KEY': notify.xApiKey, | ||
}, | ||
body: JSON.stringify({ | ||
message: msg, | ||
details: object, | ||
}), | ||
}); | ||
|
||
let res = null; | ||
console.log(await res.json()); | ||
|
||
// only send chad message to discord in production environment | ||
// prettier-ignore | ||
if (env === 'production') { | ||
res = await axios({ method: 'POST', headers: { 'Content-Type': 'application/json', }, data: JSON.stringify(params), url: discord.url }); | ||
if (res?.status === 204) logger.info(`Chad sent ${msg}`); | ||
} else { | ||
logger.warn(`Skipping Chad message in dev environment!`) | ||
if (res.ok) { | ||
logger.info(`Chad sent ${msg}`); | ||
} else { | ||
logger.error(`Failed to send Chad message: ${res.statusText}`); | ||
} | ||
|
||
} else { | ||
logger.warn('Skipping Chad message in dev environment!'); | ||
} | ||
|
||
} catch (e) { | ||
logger.error(e); | ||
|
||
logger.error(e.message); | ||
|
||
await fetch(notify.url, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-API-KEY': notify.xApiKey, | ||
}, | ||
body: JSON.stringify({ | ||
message: e.message, | ||
details: e.stack, | ||
}), | ||
}); | ||
|
||
} | ||
} | ||
} |