Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
theimperious1 committed Dec 23, 2024
1 parent 5f11549 commit d452947
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 74 deletions.
105 changes: 57 additions & 48 deletions src/discord/commands/global/d.tripsitmode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
} from 'discord.js';
import { stripIndents } from 'common-tags';
import { DateTime } from 'luxon';
import { ticket_status } from '@prisma/client';
import { ticket_status, user_tickets } from '@prisma/client';
import { SlashCommand } from '../../@types/commandDef';
import { embedTemplate } from '../../utils/embedTemplate';
// import {embedTemplate} from '../../utils/embedTemplate';
Expand Down Expand Up @@ -251,10 +251,13 @@ async function tripsitmodeOn(
if (minutes > 5) { // Switch to seconds > 10 for dev server
const helperString = `and/or ${roleHelper}`;
try {
// eslint-disable-next-line max-len
metaMessage = `Hey ${roleTripsitter} ${guildData.role_helper ? helperString : ''} team, ${interaction.member} has indicated that ${target.displayName} needs assistance!`;
} catch (err) {
// If for example helper role has been deleted but the ID is still stored, do this
// eslint-disable-next-line max-len
metaMessage = `Hey ${roleTripsitter} team, ${interaction.member} has indicated that ${target.displayName} needs assistance!`;
// eslint-disable-next-line max-len
log.error(F, `Stored Helper ID for guild ${guildData.id} is no longer valid. Role is unfetchable or deleted.`);
}
} else {
Expand Down Expand Up @@ -287,6 +290,7 @@ async function tripsitmodeOn(
}
}

log.info(F, 'TripSit Mode was triggered in Condition 1 (Update)');
ticketData = await db.user_tickets.update({
where: {
id: ticketData.id,
Expand Down Expand Up @@ -343,55 +347,60 @@ async function tripsitmodeOn(
const triage = i.fields.getTextInputValue('triageInput');
const intro = i.fields.getTextInputValue('introInput');

const threadHelpUser = await tripSitMe(i, target, triage, intro) as ThreadChannel;
const result = (await tripSitMe(i, target, triage, intro)) as [ThreadChannel | null, user_tickets] | null;

const replyMessage = stripIndents`
Hey ${i.member}, you activated tripsit mode on ${target.displayName}!
Click here to be taken to their private room: ${threadHelpUser}
You can also click in your channel list to see their private room!`;
const embed = embedTemplate()
.setColor(Colors.DarkBlue)
.setDescription(replyMessage);
await i.editReply({ embeds: [embed] });
if (result !== null) {
const [threadHelpUser, newTicketData] = result;
const replyMessage = stripIndents`
Hey ${i.member}, you activated tripsit mode on ${target.displayName}!
Click here to be taken to their private room: ${threadHelpUser}
You can also click in your channel list to see their private room!`;
const embed = embedTemplate()
.setColor(Colors.DarkBlue)
.setDescription(replyMessage);
await i.editReply({ embeds: [embed] });

if (!ticketData) {
ticketData = await db.user_tickets.create({
data: {
user_id: target.id,
type: 'TRIPSIT',
status: 'OPEN' as ticket_status,
tripsit_mode: true,
description: '',
first_message_id: '',
thread_id: threadHelpUser.id,
archived_at: env.NODE_ENV === 'production'
? DateTime.local().plus({ days: 3 }).toJSDate()
: DateTime.local().plus({ minutes: 1 }).toJSDate(),
deleted_at: env.NODE_ENV === 'production'
? DateTime.local().plus({ days: 6 }).toJSDate()
: DateTime.local().plus({ minutes: 2 }).toJSDate(),
},
});
} else {
// Create or update thread to toggle tripsit_mode on
ticketData = await db.user_tickets.update({
where: {
id: ticketData.id, // Assuming 'id' is unique; use a dummy value if `ticketData` is null
},
data: {
status: 'OPEN' as ticket_status,
tripsit_mode: true,
reopened_at: new Date(),
archived_at: env.NODE_ENV === 'production'
? DateTime.local().plus({ days: 3 }).toJSDate()
: DateTime.local().plus({ minutes: 1 }).toJSDate(),
deleted_at: env.NODE_ENV === 'production'
? DateTime.local().plus({ days: 6 }).toJSDate()
: DateTime.local().plus({ minutes: 2 }).toJSDate(),
},
});
if (!ticketData) {
log.info(F, 'TripSit Mode was triggered in Condition 2 (Creation)');
ticketData = await db.user_tickets.create({
data: {
user_id: userData.id,
type: 'TRIPSIT',
status: 'OPEN' as ticket_status,
tripsit_mode: true,
description: '',
first_message_id: '',
thread_id: threadHelpUser !== null ? threadHelpUser.id : '',
archived_at: env.NODE_ENV === 'production'
? DateTime.local().plus({ days: 3 }).toJSDate()
: DateTime.local().plus({ minutes: 1 }).toJSDate(),
deleted_at: env.NODE_ENV === 'production'
? DateTime.local().plus({ days: 6 }).toJSDate()
: DateTime.local().plus({ minutes: 2 }).toJSDate(),
},
});
} else {
// Create or update thread to toggle tripsit_mode on
log.info(F, 'TripSit Mode was triggered in Condition 3 (Update)');
ticketData = await db.user_tickets.update({
where: {
id: newTicketData.id, // Assuming 'id' is unique; use a dummy value if `ticketData` is null
},
data: {
status: 'OPEN' as ticket_status,
tripsit_mode: true,
reopened_at: new Date(),
archived_at: env.NODE_ENV === 'production'
? DateTime.local().plus({ days: 3 }).toJSDate()
: DateTime.local().plus({ minutes: 1 }).toJSDate(),
deleted_at: env.NODE_ENV === 'production'
? DateTime.local().plus({ days: 6 }).toJSDate()
: DateTime.local().plus({ minutes: 2 }).toJSDate(),
},
});
}
}
});

Expand Down
59 changes: 33 additions & 26 deletions src/discord/utils/tripsitme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -888,11 +888,13 @@ export async function tripsitmeUserClose(
},
},
},
orderBy: {
thread_id: 'desc',
},
});

if (ticketData?.tripsit_mode && targetId === actor.id) {
log.debug(F, 'They requested to close their ticket, but are in TripSit Mode. Request denied.');
// await interaction.editReply({ content: 'Only a TripSitter or Moderator can close your ticket.' });
const rejectMessage = stripIndents`
Hey ${actor.displayName}, since a team member made this ticket for you, only a team member can close it.
Expand Down Expand Up @@ -1332,7 +1334,7 @@ export async function tripSitMe(
memberInput:GuildMember | null,
triage:string,
intro: string = '',
):Promise<ThreadChannel | null> {
):Promise<[ThreadChannel | null, user_tickets] | null> {
log.info(F, await commandContext(interaction));
// await interaction.deferReply({ ephemeral: true });

Expand Down Expand Up @@ -1527,7 +1529,7 @@ export async function tripSitMe(
const embedTripsitter = embedTemplate()
.setColor(Colors.DarkBlue)
.setDescription(stripIndents`
${issue ? `A tripsitter has put ${target} into tripsitmode!\n` : `${target} has requested assistance!\n`}
${issue ? `${interaction.member} has put ${target} into tripsitmode!\n` : `${target} has requested assistance!\n`}
**They've taken:** ${triage ? `\n${triage}` : noInfo}
${intro !== '' ? `${issue}\n` : ''}
Expand Down Expand Up @@ -1606,27 +1608,29 @@ export async function tripSitMe(
type: 'TRIPSIT',
status: 'OPEN',
first_message_id: '',
tripsit_mode: !!issue,
archived_at: archiveTime.toJSDate(),
deleted_at: deleteTime.toJSDate(),
} as user_tickets;

// log.debug(F, `newTicketData: ${JSON.stringify(newTicketData, null, 2)}`);

// Update the ticket in the DB
await db.user_tickets.create({
const newTicket = await db.user_tickets.create({
data: {
user_id: newTicketData.user_id,
description: newTicketData.description,
thread_id: newTicketData.thread_id,
type: newTicketData.type,
status: newTicketData.status,
first_message_id: newTicketData.first_message_id,
tripsit_mode: newTicketData.tripsit_mode,
archived_at: newTicketData.archived_at,
deleted_at: newTicketData.deleted_at,
},
});

return threadHelpUser;
return [threadHelpUser, newTicket];
}

export async function tripsitmeButton(
Expand Down Expand Up @@ -1955,30 +1959,33 @@ export async function tripsitmeButton(
await i.deferReply({ ephemeral: true });
const triage = i.fields.getTextInputValue('triageInput');

const threadHelpUser = await tripSitMe(i, target, triage) as ThreadChannel;
const result = (await tripSitMe(i, target, triage)) as [ThreadChannel | null, user_tickets] | null;

if (!threadHelpUser) {
if (result !== null) {
const [threadHelpUser] = result;
if (!threadHelpUser) {
const embed = embedTemplate()
.setColor(Colors.DarkBlue)
.setDescription(stripIndents`Hey ${interaction.member}, there was an error creating your help thread! The Guild owner should get a message with specifics!`);
await i.editReply({ embeds: [embed] });
return;
}

const replyMessage = stripIndents`
Hey ${target}, thank you for asking for assistance!
Click here to be taken to your private room: ${threadHelpUser.toString()}
You can also click in your channel list to see your private room!`;
const embed = embedTemplate()
.setColor(Colors.DarkBlue)
.setDescription(stripIndents`Hey ${interaction.member}, there was an error creating your help thread! The Guild owner should get a message with specifics!`);
await i.editReply({ embeds: [embed] });
return;
}

const replyMessage = stripIndents`
Hey ${target}, thank you for asking for assistance!
Click here to be taken to your private room: ${threadHelpUser.toString()}
You can also click in your channel list to see your private room!`;
const embed = embedTemplate()
.setColor(Colors.DarkBlue)
.setDescription(replyMessage);
try {
await i.editReply({ embeds: [embed] });
} catch (err) {
log.error(F, `There was an error responding to the user! ${err}`);
log.error(F, `Error: ${JSON.stringify(err, null, 2)}`);
.setDescription(replyMessage);
try {
await i.editReply({ embeds: [embed] });
} catch (err) {
log.error(F, `There was an error responding to the user! ${err}`);
log.error(F, `Error: ${JSON.stringify(err, null, 2)}`);
}
}
});
}

0 comments on commit d452947

Please sign in to comment.