Skip to content

Commit

Permalink
fix: snyk issues
Browse files Browse the repository at this point in the history
* Add promise rejection handling
* Add more debugging info to ban handler
* Clean up ticket.ts nesting
  • Loading branch information
totallytavi committed Dec 21, 2023
1 parent 6d5c384 commit 91c701a
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 68 deletions.
18 changes: 12 additions & 6 deletions src/commands/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,18 @@ export async function run(client: CustomClient, interaction: ChatInputCommandInt
link: m.url
})
// Then add the message to the ticket
.then((m) => ticket.addMsg(m));
// Edit the original interaction
mi.editReply({
content: 'Report submitted. Here is a copy. A copy as also been sent to your DMs if I can DM you.',
embeds: [reportEmbed]
});
.then((m) => {
ticket.addMsg(m);
// Edit the original interaction
mi.editReply({
content: 'Report submitted. Here is a copy. A copy as also been sent to your DMs if I can DM you.',
embeds: [reportEmbed]
});
})
.catch(() => {
ticket.destroy({ force: true });
mi.editReply({ content: 'Failed to create the report. Please re-run the command' });
});
// Return
return;
}
5 changes: 3 additions & 2 deletions src/commands/userid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const data = new SlashCommandBuilder()
return option.setName('username').setDescription('Roblox username').setRequired(true);
});
export async function run(
client: CustomClient,
_client: CustomClient,
interaction: ChatInputCommandInteraction,
options: CommandInteractionOptionResolver
) {
Expand All @@ -24,7 +24,8 @@ export async function run(
`https://thumbnails.roblox.com/v1/users/avatar?userIds=${roblox.user.id}&size=720x720&format=Png&isCircular=false`
)
.then((r) => r.json())
.then((r) => r.data[0].imageUrl);
.then((r) => r.data[0].imageUrl)
.catch(() => 'https://cdn.discordapp.com/embed/avatars/5.png?size=1024');
// Reply to interaction
return interaction.editReply({
embeds: [
Expand Down
3 changes: 2 additions & 1 deletion src/commands/username.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export async function run(
`https://thumbnails.roblox.com/v1/users/avatar?userIds=${roblox.user.id}&size=720x720&format=Png&isCircular=false`
)
.then((r) => r.json())
.then((r) => r.data[0].imageUrl);
.then((r) => r.data[0].imageUrl)
.catch(() => 'https://cdn.discordapp.com/embed/avatars/5.png');
return interaction.editReply({
embeds: [
{
Expand Down
8 changes: 6 additions & 2 deletions src/functions/ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ export async function handleBans(client: CustomClient): Promise<void> {
// Fetch the victim's data from Roblox
const victim: { id: number; name: string; displayName: string } = await fetch(
`https://users.roblox.com/v1/users/${ban.user}`
).then((r) => r.json());
)
.then((r) => r.json())
.catch(() => {});
// Add moderator placeholder data
let moderator: { id: number; name: string; displayName: string } = {
id: 0,
Expand Down Expand Up @@ -101,7 +103,9 @@ export async function handleBans(client: CustomClient): Promise<void> {
}
// Can't find them, skip and log for later
if (!discord) {
console.log(`[BAN] Failed to find Discord user for ${moderator.name} (${moderator.id})`);
console.log(`[BAN] Failed to find Discord user. Dumping data:`);
console.log(`[BAN] M: ${moderator.name}/${moderator.id} V: ${victim.name}/${victim.id}`);
console.log(`[BAN] Database data: ID/${ban.banId} VIC/${ban.user} MOD/${ban.mod}`);
return;
}

Expand Down
144 changes: 87 additions & 57 deletions src/functions/tickets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,18 @@ async function transferTicket({ interaction, ticket, client }: ticketFunctionArg
}) as ActionRowBuilder<ButtonBuilder>
]
});
client.users.fetch(ticket.author).then((u) =>
u.send({
content: `[\`#${ticket.ticketId}\`] Your ticket has been transferred to ${division.name} division. You can expect a response soon`
})
);
client.users
.fetch(ticket.author)
.then((u) =>
u.send({
content: `[\`#${ticket.ticketId}\`] Your ticket has been transferred to ${division.name} division. You can expect a response soon`
})
)
.catch(() => {
cCheck.send({
content: `[\`#${ticket.ticketId}\`] Transferred ticket. User was not notified (DMs closed)`
});
});
}
// Return (un)modified ticket
return ticket;
Expand Down Expand Up @@ -143,11 +150,18 @@ async function closeTicket({ interaction, ticket, client }: ticketFunctionArgs):
await Promise.all(ep);
// Inform the user
await interaction.editReply({ content: 'This ticket has been closed' });
await client.users.fetch(ticket.author).then((u) =>
u.send({
content: `[\`#${ticket.ticketId}\`] Your ticket has been closed. If you have any further issues, please open a new ticket.`
})
);
await client.users
.fetch(ticket.author)
.then((u) =>
u.send({
content: `[\`#${ticket.ticketId}\`] Your ticket has been closed. If you have any further issues, please open a new ticket.`
})
)
.catch(() => {
interaction.channel.send({
content: `[\`#${ticket.ticketId}\`] Ticket closed. User was not notified (DMs closed)`
});
});
}
async function replyTicket({ interaction, ticket, client }: ticketFunctionArgs): Promise<tickets> {
const tId = (interaction as ButtonInteraction).customId.split('-').slice(1).join('-');
Expand Down Expand Up @@ -216,49 +230,54 @@ async function replyTicket({ interaction, ticket, client }: ticketFunctionArgs):
// Send the message
let m: Message;
if (!interaction.inGuild()) {
m = await contactChannel.send({
const c = new ActionRowBuilder({
components: [
new ButtonBuilder({ customId: `reply-${ticket.ticketId}`, label: 'Reply', style: ButtonStyle.Primary }),
new ButtonBuilder({
customId: `transfer-${ticket.ticketId}`,
label: 'Transfer',
style: ButtonStyle.Secondary
}),
new ButtonBuilder({ customId: `close-${ticket.ticketId}`, label: 'Close', style: ButtonStyle.Danger }),
new ButtonBuilder({ customId: `unclaim-${ticket.ticketId}`, label: 'Unclaim', style: ButtonStyle.Danger })
]
}) as ActionRowBuilder<ButtonBuilder>;
const msgData = {
content: `A reply has been received! Ticket is claimed by <@${ticket.claimer}>`,
embeds: [{ description: content + `\n> ${(interaction as ButtonInteraction).user.toString()}`, color: 0xaae66e }],
components: [
new ActionRowBuilder({
components: [
new ButtonBuilder({ customId: `reply-${ticket.ticketId}`, label: 'Reply', style: ButtonStyle.Primary }),
new ButtonBuilder({
customId: `transfer-${ticket.ticketId}`,
label: 'Transfer',
style: ButtonStyle.Secondary
}),
new ButtonBuilder({ customId: `close-${ticket.ticketId}`, label: 'Close', style: ButtonStyle.Danger }),
new ButtonBuilder({ customId: `unclaim-${ticket.ticketId}`, label: 'Unclaim', style: ButtonStyle.Danger })
]
}) as ActionRowBuilder<ButtonBuilder>
],
components: [c],
target: (interaction as ButtonInteraction).message
});
};
m = await contactChannel.send(msgData);
} else {
m = await client.users.fetch(ticket.author).then((u) =>
u.send({
content: `[\`#${ticket.ticketId}\`] Your ticket has been replied to. You can view the reply by clicking below`,
embeds: [{ description: content + `\n> ${interaction.user.toString()}`, color: 0xaae66e }],
components: [
new ActionRowBuilder({
components: [
new ButtonBuilder({
customId: `reply-${ticket.ticketId}`,
label: 'Reply',
style: ButtonStyle.Success
})
]
}) as ActionRowBuilder<ButtonBuilder>
],
target: (interaction as ButtonInteraction).message
})
);
const c = new ActionRowBuilder({
components: [
new ButtonBuilder({
customId: `reply-${ticket.ticketId}`,
label: 'Reply',
style: ButtonStyle.Success
})
]
}) as ActionRowBuilder<ButtonBuilder>;
const msgData = {
content: `[\`#${ticket.ticketId}\`] Your ticket has been replied to. You can view the reply by clicking below`,
embeds: [{ description: content + `\n> ${interaction.user.toString()}`, color: 0xaae66e }],
components: [c],
target: (interaction as ButtonInteraction).message
};
m = await client.users
.fetch(ticket.author)
.then((u) => u.send(msgData))
.catch(() => null);
}
// Insert message into the database
await msgs
.create({ tick: ticket.ticketId, content, author: interaction.user.id, link: m.url })
.then((m) => ticket.addMsg(m));
.then((m) => ticket.addMsg(m))
.catch(() => {
i.editReply({ content: 'There was an error replicating the message to the database' });
return Promise.reject('Database error');
});
await i.editReply({ content: 'Your reply has been sent' });
// Return the ticket
return ticket;
Expand All @@ -285,13 +304,17 @@ async function claimTicket({ interaction, ticket, client }: ticketFunctionArgs):
await ticket.save();
// Inform the user
await interaction.editReply({ content: `Ticket claimed successfully!` });
await client.users.fetch(ticket.author).then((u) =>
u.send({
content: `[\`#${ticket.ticketId}\`] Your ticket has been claimed by ${
(interaction.member as GuildMember).nickname || interaction.user.username
} (${interaction.user.toString()}). You can expect a response shortly!`
})
);
await client.users
.fetch(ticket.author)
.then((u) =>
u.send({
content: `[\`#${ticket.ticketId}\`] Your ticket has been claimed by ${
(interaction.member as GuildMember).nickname || interaction.user.username
} (${interaction.user.toString()}). You can expect a response shortly!`
})
)
// This message isn't mission critical
.catch(() => null);
await (interaction as ButtonInteraction).message.edit({
content: `Ticket claimed by ${interaction.user.toString()}`,
components: [
Expand Down Expand Up @@ -340,11 +363,18 @@ async function unclaimTicket({ interaction, ticket, client }: ticketFunctionArgs
await ticket.save();
// Inform the user
await interaction.editReply({ content: 'You have unclaimed this ticket' });
await client.users.fetch(ticket.author).then((u) =>
u.send({
content: `[\`#${ticket.ticketId}\`] Your ticket has been released. A new member will be assisting you soon!`
})
);
await client.users
.fetch(ticket.author)
.then((u) =>
u.send({
content: `[\`#${ticket.ticketId}\`] Your ticket has been released. A new member will be assisting you soon!`
})
)
.catch(() => {
interaction.channel.send({
content: `[\`#${ticket.ticketId}\`] Ticket released. User was not notified (DMs closed)`
});
});
// Return the ticket
return ticket;
}
Expand Down

0 comments on commit 91c701a

Please sign in to comment.