Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
fix: get the thread autoresponding working
Browse files Browse the repository at this point in the history
  • Loading branch information
naomi-lgbt committed Jun 6, 2024
1 parent 3fcc9c0 commit f1ee6ee
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/modules/threads/autorespondToThreads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ export const autorespondToThreads = async (bot: ExtendedClient) => {
.sort((a, b) => (b.createdTimestamp ?? 0) - (a.createdTimestamp ?? 0));

for (const thread of threadsToProcess) {
/**
* If it's somehow archived but never got labelled, mark it as inactive.
*/
if (thread.archived) {
await thread.setArchived(false);
await thread.setAppliedTags([
...thread.appliedTags,
bot.cache.inactiveTag,
]);
await thread.setArchived(true);
continue;
}
const lastMessageQuery = await thread.messages
.fetch({ limit: 1 })
.catch(async (err) => {
Expand Down Expand Up @@ -87,19 +99,26 @@ export const autorespondToThreads = async (bot: ExtendedClient) => {
* Archived threads should be labelled, too.
*/
if (
(lastMessage.createdTimestamp < Date.now() - 1000 * 60 * 60 * 24 * 14 &&
!thread.appliedTags.includes(bot.cache.inactiveTag)) ||
thread.archived
lastMessage.createdTimestamp < Date.now() - 1000 * 60 * 60 * 24 * 14 &&
!thread.appliedTags.includes(bot.cache.inactiveTag)
) {
/**
* Threads that are 14 days old should have been auto-archived by Discord as
* this is the maximum TTL for a thread. So we just need to label them.
*/
if (thread.archived) {
/**
* Cannot apply tags without unarchiving. Theoretically this should never be
* true, as archived threads are handled earlier. But let's be safe.
*/
await thread.setArchived(false);
}
await thread
.setAppliedTags([...thread.appliedTags, bot.cache.inactiveTag])
.catch(
async (err) => await errorHandler(bot, "set inactive tag", err)
);
await thread.setArchived(true);
continue;
}
/**
Expand All @@ -108,7 +127,7 @@ export const autorespondToThreads = async (bot: ExtendedClient) => {
* Note that this won't work as expected for posts moved by
* the bot.
*/
if (lastMessage.author.id === owner.id) {
if (lastMessage.author.id === owner.id && owner.id !== bot.user?.id) {
continue;
}
/**
Expand Down Expand Up @@ -137,7 +156,7 @@ export const autorespondToThreads = async (bot: ExtendedClient) => {
) {
continue;
}
if (lastMessage.createdTimestamp > Date.now() - 1000 * 60 * 60 * 24 * 7) {
if (lastMessage.createdTimestamp < Date.now() - 1000 * 60 * 60 * 24 * 7) {
/**
* When the last message is from the bot, it's the auto-response
* message. Instead of responding again, we just want to close it.
Expand Down

0 comments on commit f1ee6ee

Please sign in to comment.