Skip to content

Commit

Permalink
Handle closed/locked tickets properly
Browse files Browse the repository at this point in the history
Signed-off-by: cobalt <[email protected]>
  • Loading branch information
RedGuy12 committed Jul 10, 2024
1 parent 399f743 commit 51809f7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 43 deletions.
79 changes: 42 additions & 37 deletions modules/threads/auto-close.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,46 +159,51 @@ export async function autoClose(
thread: AnyThreadChannel,
): Promise<void> {
if (thread.guild.id !== config.guild.id) return;
const options = getThreadConfig(thread);
if (thread.archived && options.keepOpen) await thread.setArchived(false, "Keeping thread open");

if (thread.archived) {
const options = getThreadConfig(thread);
if (options.keepOpen) await thread.setArchived(false, "Keeping thread open");
return;
}

if (
!wasLocked &&
thread.locked &&
(thread.type === ChannelType.PrivateThread || thread.parent?.isThreadOnly())
) {
const date = Date.now() + 43_200_000;
remindersDatabase.data = [
...remindersDatabase.data,
!thread.locked ||
wasLocked ||
(thread.type !== ChannelType.PrivateThread && !thread.parent?.isThreadOnly())
)
return;

const date = Date.now() + 43_200_000;
remindersDatabase.data = [
...remindersDatabase.data,
{
channel: thread.id,
date: date,
reminder: undefined,
user: client.user.id,
id: SpecialReminders.CloseThread,
},
];

await thread.send({
content: `${constants.emojis.statuses.yes} I’ll close this thread ${time(
Math.round(date / 1000),
TimestampStyles.RelativeTime,
)}!`,
components: [
{
channel: thread.id,
date: date,
reminder: undefined,
user: client.user.id,
id: SpecialReminders.CloseThread,
type: ComponentType.ActionRow,
components: [
{
type: ComponentType.Button,
label: "Cancel",
customId: "close_cancelThreadChange",
style: ButtonStyle.Danger,
},
],
},
];

await thread.send({
content: `${constants.emojis.statuses.yes} I’ll close this thread ${time(
Math.round(date / 1000),
TimestampStyles.RelativeTime,
)}!`,
components: [
{
type: ComponentType.ActionRow,
components: [
{
type: ComponentType.Button,
label: "Cancel",
customId: "close_cancelThreadChange",
style: ButtonStyle.Danger,
},
],
},
],
});
],
});

await queueReminders();
}
await queueReminders();
}
23 changes: 17 additions & 6 deletions modules/tickets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
GuildMember,
TextInputStyle,
channelLink,
userMention,
} from "discord.js";
import {
client,
Expand Down Expand Up @@ -318,23 +319,33 @@ defineEvent("threadUpdate", async (oldThread, newThread) => {
if (
newThread.parent?.id !== config.channels.tickets?.id ||
newThread.type !== ChannelType.PrivateThread ||
oldThread.archived === newThread.archived
oldThread.archived === newThread.archived ||
!newThread.editable
)
return;
const memberId = getIdFromName(newThread.name);
if (!memberId) return;

const existing = TICKETS_BY_MEMBER[memberId];
if (newThread.archived) {
TICKETS_BY_MEMBER[memberId] = undefined;
if (!newThread.locked) {
if (newThread.sendable && !newThread.locked) {
await newThread.setArchived(false, "To lock it");
await newThread.setLocked(true, "Was closed");
await newThread.edit({ archived: true, locked: true, reason: "Was closed" });
}
} else if (newThread.locked) {
TICKETS_BY_MEMBER[memberId] = undefined;
} else if (TICKETS_BY_MEMBER[memberId]) {
await newThread.setArchived(true, "Reopened while another ticket is already open");
await newThread.setLocked(true, "Reopened while another ticket is already open");
} else if (existing) {
await newThread.send(
`${constants.emojis.statuses.no} ${userMention(
memberId,
)} already has another ticket open! Please use ${existing.toString()}.`,
);
await newThread.edit({
archived: true,
locked: true,
reason: "Reopened while another ticket is already open",
});
} else {
TICKETS_BY_MEMBER[memberId] = newThread;
}
Expand Down

0 comments on commit 51809f7

Please sign in to comment.