Skip to content

Commit

Permalink
feat(notification): add discord groups (#1211)
Browse files Browse the repository at this point in the history
  • Loading branch information
styler2go committed Dec 9, 2020
1 parent 9dc04b3 commit 2632386
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
15 changes: 15 additions & 0 deletions .env-example
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ BROWSER_TRUSTED=""
COUNTRY=""
DESKTOP_NOTIFICATIONS=""
DISCORD_NOTIFY_GROUP=""
DISCORD_NOTIFY_GROUP_3060TI=""
DISCORD_NOTIFY_GROUP_3070=""
DISCORD_NOTIFY_GROUP_3080=""
DISCORD_NOTIFY_GROUP_3090=""
DISCORD_NOTIFY_GROUP_CORSAIR_SF=""
DISCORD_NOTIFY_GROUP_RX6800=""
DISCORD_NOTIFY_GROUP_RX6800XT=""
DISCORD_NOTIFY_GROUP_RX6900XT=""
DISCORD_NOTIFY_GROUP_RYZEN5600=""
DISCORD_NOTIFY_GROUP_RYZEN5800=""
DISCORD_NOTIFY_GROUP_RYZEN5900=""
DISCORD_NOTIFY_GROUP_RYZEN5950=""
DISCORD_NOTIFY_GROUP_SONYPS5C=""
DISCORD_NOTIFY_GROUP_SONYPS5DE=""
DISCORD_NOTIFY_GROUP_TEST=""
DISCORD_WEB_HOOK=""
EMAIL_PASSWORD=""
EMAIL_TO=""
Expand Down
15 changes: 15 additions & 0 deletions docs/reference/notification.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,23 @@ You can test your notification configuration by running `npm run test:notificati
| Environment variable | Description |
|:---:|---|
| `DISCORD_NOTIFY_GROUP` | Discord group you would like to notify. Can be comma separated |
| `DISCORD_NOTIFY_GROUP_3060TI` | Discord group to notify on 3060 Ti stock |
| `DISCORD_NOTIFY_GROUP_3070` | Discord group to notify on 3070 stock |
| `DISCORD_NOTIFY_GROUP_3080` | Discord group to notify on 3080 stock |
| `DISCORD_NOTIFY_GROUP_3090` | Discord group to notify on 3090 stock |
| `DISCORD_NOTIFY_GROUP_RYZEN5600` | Discord group to notify on 5600X stock |
| `DISCORD_NOTIFY_GROUP_RYZEN5800` | Discord group to notify on 5800X stock |
| `DISCORD_NOTIFY_GROUP_RYZEN5900` | Discord group to notify on 5900X stock |
| `DISCORD_NOTIFY_GROUP_RYZEN5950` | Discord group to notify on 5950X stock |
| `DISCORD_NOTIFY_GROUP_CORSAIR_SF` | Discord group to notify on Corsair SF stock |
| `DISCORD_NOTIFY_GROUP_SONYPS5C` | Discord group to notify on Sony PS5 stock |
| `DISCORD_NOTIFY_GROUP_SONYPS5DE` | Discord group to notify on Sony PS5 Digital stock |
| `DISCORD_NOTIFY_GROUP_TEST` | Discord group to notify on test stock |
| `DISCORD_WEB_HOOK` | Discord Web Hook URL. Can be comma separated. Use whole webhook URL |

???+ note
If you don't assign a value to a series group it will always fallback to `DISCORD_NOTIFY_GROUP`.

???+ note
- If you're using a role, please use `<@&2834729847239842>`
- If you're using a user, please use `<@2834729847239842>`
Expand Down
19 changes: 19 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,25 @@ const notifications = {
desktop: process.env.DESKTOP_NOTIFICATIONS === 'true',
discord: {
notifyGroup: envOrArray(process.env.DISCORD_NOTIFY_GROUP),
notifyGroupSeries: {
'3060ti': envOrArray(process.env.DISCORD_NOTIFY_GROUP_3060TI),
3070: envOrArray(process.env.DISCORD_NOTIFY_GROUP_3070),
3080: envOrArray(process.env.DISCORD_NOTIFY_GROUP_3080),
3090: envOrArray(process.env.DISCORD_NOTIFY_GROUP_3090),
rx6800: envOrArray(process.env.DISCORD_NOTIFY_GROUP_RX6800),
rx6800xt: envOrArray(process.env.DISCORD_NOTIFY_GROUP_RX6800XT),
rx6900xt: envOrArray(process.env.DISCORD_NOTIFY_GROUP_RX6900XT),
ryzen5600: envOrArray(process.env.DISCORD_NOTIFY_GROUP_RYZEN5600),
ryzen5800: envOrArray(process.env.DISCORD_NOTIFY_GROUP_RYZEN5800),
ryzen5900: envOrArray(process.env.DISCORD_NOTIFY_GROUP_RYZEN5900),
ryzen5950: envOrArray(process.env.DISCORD_NOTIFY_GROUP_RYZEN5950),
sf: envOrArray(process.env.DISCORD_NOTIFY_GROUP_CORSAIR_SF),
sonyps5c: envOrArray(process.env.DISCORD_NOTIFY_GROUP_SONYPS5C),
sonyps5de: envOrArray(process.env.DISCORD_NOTIFY_GROUP_SONYPS5DE),
'test:series': envOrArray(process.env.DISCORD_NOTIFY_GROUP_TEST),
xboxss: envOrArray(process.env.DISCORD_NOTIFY_GROUP_XBOXSS),
xboxsx: envOrArray(process.env.DISCORD_NOTIFY_GROUP_XBOXSX)
},
webhooks: envOrArray(process.env.DISCORD_WEB_HOOK)
},
email: {
Expand Down
17 changes: 15 additions & 2 deletions src/notification/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {config} from '../config';
import {logger} from '../logger';

const discord = config.notifications.discord;
const {notifyGroup, webhooks} = discord;
const {notifyGroup, webhooks, notifyGroupSeries} = discord;

function getIdAndToken(webhook: string) {
const match = /.*\/webhooks\/(\d+)\/(.+)/.exec(webhook);
Expand Down Expand Up @@ -44,13 +44,26 @@ export function sendDiscordMessage(link: Link, store: Store) {
embed.addField('Model', link.model, true);
embed.addField('Series', link.series, true);

embed.setTimestamp();

let notifyText: string[] = [];

if (Object.keys(notifyGroupSeries).indexOf(link.series) !== 0) {
notifyText = notifyText.concat(
notifyGroupSeries[link.series]
);
} else if (notifyGroup) {
notifyText = notifyText.concat(notifyGroup); // If there is no group for the series we
}

const promises = [];
for (const webhook of webhooks) {
const {id, token} = getIdAndToken(webhook);
const client = new Discord.WebhookClient(id, token);

promises.push({
client,
message: client.send(notifyGroup.join(' '), {
message: client.send(notifyText.join(' '), {
embeds: [embed],
username: 'streetmerchant'
})
Expand Down

0 comments on commit 2632386

Please sign in to comment.