Skip to content

Commit

Permalink
Fix news posts
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibowl committed Nov 4, 2023
1 parent e0ac933 commit 666edad
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 42 deletions.
26 changes: 15 additions & 11 deletions src/utils/FollowManager.ts
Expand Up @@ -121,16 +121,20 @@ export default class FollowManager {
channels = channels.filter((val, ind) => channels.findIndex(v => v.channelID == val.channelID) === ind)

Logger.info(`Sending ${category} to ${channels.length} channels: ${content}`)
const messages = (await sendToChannels(channels, content, embed)).filter((x): x is PromiseFulfilledResult<Message | Message[]> => x.status == "fulfilled").map(x => x.value).flat()

for (const message of messages)
if (message instanceof Message && message.channel.type === ChannelType.GuildAnnouncement)
try {
await message.crosspost()
} catch (error) {
Logger.error(`Unable to publish to ${message.channel.id} from ${message.guild?.id}: `, error)
}

return messages
try {
const messages = (await sendToChannels(channels, content, embed)).filter((x): x is Message => x !== undefined)

for (const message of messages)
if (message instanceof Message && message.channel.type === ChannelType.GuildAnnouncement)
try {
await message.crosspost()
} catch (error) {
Logger.error(`Unable to publish to ${message.channel.id} from ${message.guild?.id}: `, error)
}
return messages
} catch (error) {
Logger.error(`Unable to send to ${channels.length} channels: `, error)
return []
}
}
}
62 changes: 31 additions & 31 deletions src/utils/Utils.ts
Expand Up @@ -13,42 +13,37 @@ const Logger = log4js.getLogger("Utils")
* @param embed Possible embed/attachment to send
* @returns All the messages send
*/
export async function sendToChannels(channels: {channelID: Snowflake, pingRole?: string}[] | undefined, content?: string, embed?: EmbedBuilder): Promise<PromiseSettledResult<Message | Message[]>[]> {
const messages = []
if (!channels) return Promise.all([])

for (const channel of channels) {
try {
const chanObj = await client.channels.fetch(channel.channelID)
if (!(chanObj && chanObj.isTextBased())) {
Logger.error(`Invalid channel ${channel.channelID}`)
continue
}
export async function sendToChannels(channels: {channelID: Snowflake, pingRole?: string}[] | undefined, content?: string, embed?: EmbedBuilder): Promise<(Message | undefined)[]> {
if (!channels) return []

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

if (embed && ((content && content.length > 0) || (channel.pingRole && channel.pingRole.length > 0)))
messages.push(chanObj.send({
content: `${channel.pingRole && channel.pingRole != "" ? `<@&${channel.pingRole}> ` : ""}${content ?? ""}`.trim(),
embeds: [embed],
allowedMentions: { roles: channel.pingRole ? [channel.pingRole] : [] }
}))
else if (embed)
messages.push(chanObj.send({ embeds: [embed] }))
else if (content)
messages.push(chanObj.send(content))
} catch (error) {
Logger.error(`Failed to fetch ${channel}`)
export async function sendToChannel(channel: {channelID: Snowflake, pingRole?: string}, content?: string, embed?: EmbedBuilder): Promise<Message | undefined> {
try {
const chanObj = await client.channels.fetch(channel.channelID)
if (!(chanObj && chanObj.isTextBased())) {
Logger.error(`Invalid channel ${channel.channelID}`)
return undefined
}
}

return Promise.allSettled(messages)
if (embed && ((content && content.length > 0) || (channel.pingRole && channel.pingRole.length > 0)))
return await chanObj.send({
content: `${channel.pingRole && channel.pingRole != "" ? `<@&${channel.pingRole}> ` : ""}${content ?? ""}`.trim(),
embeds: [embed],
allowedMentions: { roles: channel.pingRole ? [channel.pingRole] : [] }
})
else if (embed)
return await chanObj.send({ embeds: [embed] })
else if (content)
return await chanObj.send(content)
} catch (error) {
Logger.error(`Failed to fetch ${channel.channelID}`)
return undefined
}
}


/**
* Send something to the error log channels
* @param content Content to send
Expand All @@ -57,7 +52,12 @@ export async function sendToChannels(channels: {channelID: Snowflake, pingRole?:
*/
export async function sendError(content: string, embed?: EmbedBuilder): Promise<Message[]> {
Logger.error(content)
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()
try {
return (await sendToChannels((config.errorLog as Snowflake[]).map(x => ({ channelID: x })), content, embed)).filter((x): x is Message => x !== undefined)
} catch (error) {
Logger.error("Error occurred while sending error", error)
return []
}
}

export const PAD_START = 0
Expand Down

0 comments on commit 666edad

Please sign in to comment.