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

fix(discord): properly wait for webhooks #2352

Merged
merged 5 commits into from
Apr 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions src/notification/discord.ts
Original file line number Diff line number Diff line change
@@ -51,28 +51,39 @@ export function sendDiscordMessage(link: Link, store: Store) {
if (notifyGroup) {
notifyText = notifyText.concat(notifyGroup);
}

if (Object.keys(notifyGroupSeries).indexOf(link.series) !== -1) {
notifyText = notifyText.concat(notifyGroupSeries[link.series]);
const notifyKeys = Object.keys(notifyGroupSeries);
const notifyIndex = notifyKeys.indexOf(link.series);
if (notifyIndex !== -1) {
notifyText = notifyText.concat(
Object.values(notifyGroupSeries)[notifyIndex]
);
}

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(notifyText.join(' '), {
embeds: [embed],
username: 'streetmerchant',
}),
});
promises.push(
new Promise((resolve, reject) => {
client
.send(notifyText.join(' '), {
embeds: [embed],
username: 'streetmerchant',
})
.then(resp => {
logger.info('✔ discord message sent resp.id: ' + resp.id);
resolve(resp);
})
.catch(err => reject(err))
.finally(() => client.destroy());
})
);
}

(await Promise.all(promises)).forEach(({client}) => client.destroy());

logger.info('✔ discord message sent');
await Promise.all(promises).catch(err =>
logger.error("✖ couldn't send discord message", err)
);
} catch (error: unknown) {
logger.error("✖ couldn't send discord message", error);
}