({
channel,
token,
onboardingTimestamp,
logger,
}: {
channel: channels;
token: string;
onboardingTimestamp: Date;
logger: Logger;
})
| 67 | } |
| 68 | |
| 69 | export async function getArchivedThreads({ |
| 70 | channel, |
| 71 | token, |
| 72 | onboardingTimestamp, |
| 73 | logger, |
| 74 | }: { |
| 75 | channel: channels; |
| 76 | token: string; |
| 77 | onboardingTimestamp: Date; |
| 78 | logger: Logger; |
| 79 | }) { |
| 80 | if (!channel.externalChannelId) { |
| 81 | return; |
| 82 | } |
| 83 | logger.log({ getArchivedThreads: 'started' }); |
| 84 | let has_more; |
| 85 | do { |
| 86 | const [err, result] = await to( |
| 87 | DiscordApi.getArchivedPublicThreads({ |
| 88 | externalChannelId: channel.externalChannelId, |
| 89 | limit: LIMIT, |
| 90 | before: has_more, |
| 91 | token, |
| 92 | }) |
| 93 | ); |
| 94 | if (err) { |
| 95 | logger.error({ getArchivedThreads: err }); |
| 96 | return; |
| 97 | } |
| 98 | const response = result as DiscordArchivedPublicThreads; |
| 99 | |
| 100 | has_more = response.has_more |
| 101 | ? response.threads[response.threads.length - 1].thread_metadata |
| 102 | ?.create_timestamp |
| 103 | : null; |
| 104 | |
| 105 | for (const thread of response.threads) { |
| 106 | if ( |
| 107 | thread.thread_metadata?.create_timestamp && |
| 108 | onboardingTimestamp > new Date(thread.thread_metadata.create_timestamp) |
| 109 | ) { |
| 110 | continue; |
| 111 | } |
| 112 | await processThread({ |
| 113 | thread, |
| 114 | onboardingTimestamp, |
| 115 | channel, |
| 116 | token, |
| 117 | logger, |
| 118 | }); |
| 119 | } |
| 120 | } while (has_more); |
| 121 | logger.log({ getArchivedThreads: 'finished' }); |
| 122 | } |
| 123 | |
| 124 | async function processThread({ |
| 125 | thread, |
no test coverage detected