Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(notification): add discord groups #1211

Merged
merged 13 commits into from
Dec 9, 2020
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
13 changes: 13 additions & 0 deletions docs/reference/notification.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,21 @@ 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 | Same as `DISCORD_NOTIFY_GROUP` |
| `DISCORD_NOTIFY_GROUP_3070` | Discord group to notify on 3070 stock | Same as `DISCORD_NOTIFY_GROUP` |
| `DISCORD_NOTIFY_GROUP_3080` | Discord group to notify on 3080 stock | Same as `DISCORD_NOTIFY_GROUP` |
| `DISCORD_NOTIFY_GROUP_3090` | Discord group to notify on 3090 stock | Same as `DISCORD_NOTIFY_GROUP` |
| `DISCORD_NOTIFY_GROUP_RYZEN5600` | Discord group to notify on 5600X stock | Same as `DISCORD_NOTIFY_GROUP` |
| `DISCORD_NOTIFY_GROUP_RYZEN5800` | Discord group to notify on 5800X stock | Same as `DISCORD_NOTIFY_GROUP` |
| `DISCORD_NOTIFY_GROUP_RYZEN5900` | Discord group to notify on 5900X stock | Same as `DISCORD_NOTIFY_GROUP` |
| `DISCORD_NOTIFY_GROUP_RYZEN5950` | Discord group to notify on 5950X stock | Same as `DISCORD_NOTIFY_GROUP` |
| `DISCORD_NOTIFY_GROUP_CORSAIR_SF` | Discord group to notify on Corsair SF stock | Same as `DISCORD_NOTIFY_GROUP` |
| `DISCORD_NOTIFY_GROUP_SONYPS5C` | Discord group to notify on Sony PS5 stock | Same as `DISCORD_NOTIFY_GROUP` |
| `DISCORD_NOTIFY_GROUP_SONYPS5DE` | Discord group to notify on Sony PS5 Digital stock | Same as `DISCORD_NOTIFY_GROUP` |
| `DISCORD_NOTIFY_GROUP_TEST` | Discord group to notify on test stock | Same as `DISCORD_NOTIFY_GROUP` |
styler2go marked this conversation as resolved.
Show resolved Hide resolved
| `DISCORD_WEB_HOOK` | Discord Web Hook URL. Can be comma separated. Use whole webhook URL |


???+ 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
18 changes: 16 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,27 @@ export function sendDiscordMessage(link: Link, store: Store) {
embed.addField('Model', link.model, true);
embed.addField('Series', link.series, true);

embed.setColor(0x76b900);
styler2go marked this conversation as resolved.
Show resolved Hide resolved
styler2go marked this conversation as resolved.
Show resolved Hide resolved
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