Skip to content

Commit 666edad

Browse files
committed
Fix news posts
1 parent e0ac933 commit 666edad

2 files changed

Lines changed: 46 additions & 42 deletions

File tree

src/utils/FollowManager.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,20 @@ export default class FollowManager {
121121
channels = channels.filter((val, ind) => channels.findIndex(v => v.channelID == val.channelID) === ind)
122122

123123
Logger.info(`Sending ${category} to ${channels.length} channels: ${content}`)
124-
const messages = (await sendToChannels(channels, content, embed)).filter((x): x is PromiseFulfilledResult<Message | Message[]> => x.status == "fulfilled").map(x => x.value).flat()
125-
126-
for (const message of messages)
127-
if (message instanceof Message && message.channel.type === ChannelType.GuildAnnouncement)
128-
try {
129-
await message.crosspost()
130-
} catch (error) {
131-
Logger.error(`Unable to publish to ${message.channel.id} from ${message.guild?.id}: `, error)
132-
}
133-
134-
return messages
124+
try {
125+
const messages = (await sendToChannels(channels, content, embed)).filter((x): x is Message => x !== undefined)
126+
127+
for (const message of messages)
128+
if (message instanceof Message && message.channel.type === ChannelType.GuildAnnouncement)
129+
try {
130+
await message.crosspost()
131+
} catch (error) {
132+
Logger.error(`Unable to publish to ${message.channel.id} from ${message.guild?.id}: `, error)
133+
}
134+
return messages
135+
} catch (error) {
136+
Logger.error(`Unable to send to ${channels.length} channels: `, error)
137+
return []
138+
}
135139
}
136140
}

src/utils/Utils.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,37 @@ const Logger = log4js.getLogger("Utils")
1313
* @param embed Possible embed/attachment to send
1414
* @returns All the messages send
1515
*/
16-
export async function sendToChannels(channels: {channelID: Snowflake, pingRole?: string}[] | undefined, content?: string, embed?: EmbedBuilder): Promise<PromiseSettledResult<Message | Message[]>[]> {
17-
const messages = []
18-
if (!channels) return Promise.all([])
19-
20-
for (const channel of channels) {
21-
try {
22-
const chanObj = await client.channels.fetch(channel.channelID)
23-
if (!(chanObj && chanObj.isTextBased())) {
24-
Logger.error(`Invalid channel ${channel.channelID}`)
25-
continue
26-
}
16+
export async function sendToChannels(channels: {channelID: Snowflake, pingRole?: string}[] | undefined, content?: string, embed?: EmbedBuilder): Promise<(Message | undefined)[]> {
17+
if (!channels) return []
2718

28-
if (chanObj instanceof GuildChannel)
29-
if (!(chanObj.permissionsFor(client.user!)?.has("SendMessages") && chanObj.permissionsFor(client.user!)?.has("ViewChannel"))) {
30-
Logger.error(`Missing permissions in ${chanObj.id} (${chanObj.name})`)
31-
continue
32-
}
19+
return await Promise.all(channels.map(async (c) => sendToChannel(c, content, embed)))
20+
}
3321

34-
if (embed && ((content && content.length > 0) || (channel.pingRole && channel.pingRole.length > 0)))
35-
messages.push(chanObj.send({
36-
content: `${channel.pingRole && channel.pingRole != "" ? `<@&${channel.pingRole}> ` : ""}${content ?? ""}`.trim(),
37-
embeds: [embed],
38-
allowedMentions: { roles: channel.pingRole ? [channel.pingRole] : [] }
39-
}))
40-
else if (embed)
41-
messages.push(chanObj.send({ embeds: [embed] }))
42-
else if (content)
43-
messages.push(chanObj.send(content))
44-
} catch (error) {
45-
Logger.error(`Failed to fetch ${channel}`)
22+
export async function sendToChannel(channel: {channelID: Snowflake, pingRole?: string}, content?: string, embed?: EmbedBuilder): Promise<Message | undefined> {
23+
try {
24+
const chanObj = await client.channels.fetch(channel.channelID)
25+
if (!(chanObj && chanObj.isTextBased())) {
26+
Logger.error(`Invalid channel ${channel.channelID}`)
27+
return undefined
4628
}
47-
}
4829

49-
return Promise.allSettled(messages)
30+
if (embed && ((content && content.length > 0) || (channel.pingRole && channel.pingRole.length > 0)))
31+
return await chanObj.send({
32+
content: `${channel.pingRole && channel.pingRole != "" ? `<@&${channel.pingRole}> ` : ""}${content ?? ""}`.trim(),
33+
embeds: [embed],
34+
allowedMentions: { roles: channel.pingRole ? [channel.pingRole] : [] }
35+
})
36+
else if (embed)
37+
return await chanObj.send({ embeds: [embed] })
38+
else if (content)
39+
return await chanObj.send(content)
40+
} catch (error) {
41+
Logger.error(`Failed to fetch ${channel.channelID}`)
42+
return undefined
43+
}
5044
}
5145

46+
5247
/**
5348
* Send something to the error log channels
5449
* @param content Content to send
@@ -57,7 +52,12 @@ export async function sendToChannels(channels: {channelID: Snowflake, pingRole?:
5752
*/
5853
export async function sendError(content: string, embed?: EmbedBuilder): Promise<Message[]> {
5954
Logger.error(content)
60-
return (await sendToChannels((config.errorLog as Snowflake[]).map(x => ({ channelID: x })), content, embed)).filter((x): x is PromiseFulfilledResult<Message | Message[]> => x.status == "fulfilled").map(x => x.value).flat()
55+
try {
56+
return (await sendToChannels((config.errorLog as Snowflake[]).map(x => ({ channelID: x })), content, embed)).filter((x): x is Message => x !== undefined)
57+
} catch (error) {
58+
Logger.error("Error occurred while sending error", error)
59+
return []
60+
}
6161
}
6262

6363
export const PAD_START = 0

0 commit comments

Comments
 (0)