Skip to content

Commit

Permalink
#patch added catch to update message delivery
Browse files Browse the repository at this point in the history
  • Loading branch information
jgaribsin committed Mar 15, 2024
1 parent 9aebbd8 commit 3a60328
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
43 changes: 37 additions & 6 deletions src/handlers/cron/deliverUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {config, helldiversConfig} from '../../config';
import {planetNameTransform} from '../custom';
import {validateChannelArr} from '../discord';
import {majorOrderEmbed} from '../embed';
import {logger} from '../logging';

const {SUBSCRIBE_FOOTER} = config;
const {factionSprites, altSprites} = helldiversConfig;
Expand Down Expand Up @@ -58,7 +59,13 @@ export async function newCampaignUpdate(
];
// send new updates to subscribed channels
const promises: Promise<any>[] = [];
for (const channel of channels) promises.push(channel.send({embeds}));
for (const channel of channels) {
try {
await channel.send({embeds});
} catch (err) {
logger.error(err);
}
}
await Promise.all(promises);
return;
}
Expand Down Expand Up @@ -123,7 +130,14 @@ export async function wonPlanetUpdate(
];
// send new updates to subscribed channels
const promises: Promise<any>[] = [];
for (const channel of channels) promises.push(channel.send({embeds}));
for (const channel of channels) {
try {
await channel.send({embeds});
} catch (err) {
logger.error(err);
}
}

await Promise.all(promises);
return;
}
Expand Down Expand Up @@ -188,7 +202,13 @@ export async function lostPlanetUpdate(
];
// send new updates to subscribed channels
const promises: Promise<any>[] = [];
for (const channel of channels) promises.push(channel.send({embeds}));
for (const channel of channels) {
try {
await channel.send({embeds});
} catch (err) {
logger.error(err);
}
}
await Promise.all(promises);
return;
}
Expand All @@ -209,8 +229,13 @@ export async function newEventUpdate(event: GlobalEvent, channelIds: string[]) {

// send new updates to subscribed channels
const promises: Promise<any>[] = [];
for (const channel of channels)
promises.push(channel.send({embeds: [eventEmbed]}));
for (const channel of channels) {
try {
await channel.send({embeds: [eventEmbed]});
} catch (err) {
logger.error(err);
}
}
await Promise.all(promises);
return;
}
Expand All @@ -226,7 +251,13 @@ export async function newMajorOrderUpdater(

// send new updates to subscribed channels
const promises: Promise<any>[] = [];
for (const channel of channels) promises.push(channel.send({embeds: embeds}));
for (const channel of channels) {
try {
await channel.send({embeds});
} catch (err) {
logger.error(err);
}
}
await Promise.all(promises);
return;
}
Expand Down
30 changes: 21 additions & 9 deletions src/handlers/discord/channels.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import {TextChannel, PublicThreadChannel, ChannelType} from 'discord.js';
import {
TextChannel,
PublicThreadChannel,
ChannelType,
DiscordAPIError,
} from 'discord.js';
import {client} from '../client';
import {logger} from '../logging';

export async function validateChannel(
id: string
): Promise<TextChannel | PublicThreadChannel | void> {
const channel = await client.channels.fetch(id);
if (!channel) return;
if (
channel.type === ChannelType.GuildText ||
channel.type === ChannelType.PublicThread
)
return channel as TextChannel | PublicThreadChannel;
return;
try {
const channel = await client.channels.fetch(id);
if (!channel) return;
if (
channel.type === ChannelType.GuildText ||
channel.type === ChannelType.PublicThread
)
return channel as TextChannel | PublicThreadChannel;
return;
} catch (err) {
const discordErr = err as DiscordAPIError;
logger.error(discordErr.message, {type: 'error'});
return;
}
}

export async function validateChannelArr(
Expand Down

0 comments on commit 3a60328

Please sign in to comment.