Skip to content

Commit

Permalink
fix(discord): properly wait for webhooks (#2352)
Browse files Browse the repository at this point in the history
crwgregory authored Apr 14, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent aed9cfe commit bc59da1
Showing 1 changed file with 24 additions and 13 deletions.
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);
}

0 comments on commit bc59da1

Please sign in to comment.