Skip to content

Commit d22bbb8

Browse files
committed
Cannot read properties of undefined (reading 'filter')
Fixes #27
1 parent aa376f4 commit d22bbb8

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/events/threadUpdate.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,16 @@ function wasUnSolved(oldThread, newThread, tagId) {
2020

2121
// Returns the tagId for the tag named tagName in the given channel
2222
function findTagIdByName(channel, tagName) {
23+
// Sometimes there are no available tags
24+
if (!channel.availableTags) {
25+
return undefined;
26+
}
27+
2328
const matchingTags = channel.availableTags.filter((item) => {
2429
return item.name.toLowerCase() == tagName.toLowerCase();
2530
});
2631

27-
return matchingTags.length > 0 ? matchingTags[0].id : 0;
32+
return matchingTags.length > 0 ? matchingTags[0].id : undefined;
2833
}
2934

3035
// Returns true if a thread was started farther back in time than
@@ -56,11 +61,35 @@ module.exports = {
5661
process.env.SOLVED_TAG_NAME
5762
);
5863

64+
if (tagId === undefined) {
65+
logger.warn(
66+
`Unable to lock thread "${newThread.name}": couldn't find tag name ${process.env.SOLVED_TAG_NAME} in channel #${newThread.parent.name}.`,
67+
{
68+
thread: newThread.name,
69+
solvedTag: process.env.SOLVED_TAG_NAME,
70+
channe: newThread.parent.name,
71+
}
72+
);
73+
return;
74+
}
75+
5976
if (wasSolved(oldThread, newThread, tagId)) {
6077
if (!isOldThread(newThread)) {
6178
await newThread.send(
6279
`Since this is resolved I'm locking the thread. For additional questions or similar issues please start a new thread in <#${newThread.parentId}>. Happy flying!`
6380
);
81+
} else {
82+
const createdDate = new Date(
83+
newThread.createdTimestamp
84+
).toUTCString();
85+
logger.info(
86+
`Not sending closed message to "${newThread.name}" since it was created ${createdDate} which is more than ${process.env.OLD_THREAD_AGE_IN_DAYS} days ago.`,
87+
{
88+
thread: newThread.name,
89+
createdDate,
90+
oldThreadAge: process.env.OLD_THREAD_AGE_IN_DAYS,
91+
}
92+
);
6493
}
6594

6695
await newThread.setLocked(true);
@@ -78,6 +107,9 @@ module.exports = {
78107
const lastMessage = newThread.lastMessage;
79108
if (lastMessage?.author.bot) {
80109
await newThread.lastMessage.delete();
110+
logger.debug(
111+
`Deleted the last message from the thread since it came from the bot`
112+
);
81113
}
82114
}
83115
} catch (error) {

0 commit comments

Comments
 (0)