Skip to content

Commit a8a2036

Browse files
committed
Fix bugs in Content management
* Not properly checking commands and versions * Not prorperly using defer/reply/followUp
1 parent 8a8212d commit a8a2036

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

src/commands/moderation/prefixCommands/functions/deleteContent.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ const noContentEmbed = (command: string, version: string) => makeEmbed({
1313
color: Colors.Red,
1414
});
1515

16+
const noCommandEmbed = (command: string) => makeEmbed({
17+
title: 'Prefix Commands - Delete Content - No Command',
18+
description: `Failed to delete command content for command ${command} as the command does not exist or there are more than one matching.`,
19+
color: Colors.Red,
20+
});
21+
22+
const noVersionEmbed = (version: string) => makeEmbed({
23+
title: 'Prefix Commands - Delete Content - No Version',
24+
description: `Failed to delete command content for version ${version} as the version does not exist or there are more than one matching.`,
25+
color: Colors.Red,
26+
});
27+
1628
const failedEmbed = (version: string) => makeEmbed({
1729
title: 'Prefix Commands - Delete Content - Failed',
1830
description: `Failed to delete the prefix command content with version ${version}.`,
@@ -52,7 +64,7 @@ const modLogEmbed = (moderator: User, commandName: string, versionName: string,
5264
value: `${moderator}`,
5365
},
5466
],
55-
color: Colors.Green,
67+
color: Colors.Red,
5668
});
5769

5870
const noModLogs = makeEmbed({
@@ -80,10 +92,25 @@ export async function handleDeletePrefixCommandContent(interaction: ChatInputCom
8092
await interaction.followUp({ embeds: [noModLogs], ephemeral: true });
8193
}
8294

83-
const foundVersion = await PrefixCommandVersion.findOne({ name: version });
84-
const { versionId } = foundVersion ?? { versionId: 'GENERIC' };
8595
const foundCommand = await PrefixCommand.findOne({ name: command });
86-
const [existingContent] = foundCommand?.contents.filter((content) => content.versionId === versionId) ?? [];
96+
if (!foundCommand) {
97+
await interaction.followUp({ embeds: [noCommandEmbed(command)], ephemeral: true });
98+
return;
99+
}
100+
let versionId = '';
101+
let foundVersions = null;
102+
if (version === 'GENERIC' || version === 'generic') {
103+
versionId = 'GENERIC';
104+
} else {
105+
foundVersions = await PrefixCommandVersion.find({ name: version });
106+
if (foundVersions && foundVersions.length === 1) {
107+
[{ _id: versionId }] = foundVersions;
108+
} else {
109+
await interaction.followUp({ embeds: [noVersionEmbed(version)], ephemeral: true });
110+
return;
111+
}
112+
}
113+
const existingContent = foundCommand.contents.find((content) => content.versionId.toString() === versionId.toString());
87114

88115
if (foundCommand && existingContent) {
89116
const { title, content, image } = existingContent;
@@ -97,7 +124,7 @@ export async function handleDeletePrefixCommandContent(interaction: ChatInputCom
97124
versionName = foundVersion.name || '';
98125
}
99126
try {
100-
foundCommand.contents.find((con) => con.versionId === versionId)?.deleteOne();
127+
foundCommand.contents.find((con) => con.versionId.toString() === versionId.toString())?.deleteOne();
101128
await foundCommand.save();
102129
await refreshSinglePrefixCommandCache(foundCommand, foundCommand);
103130
await interaction.followUp({ embeds: [successEmbed(`${commandName}`, `${versionName}`)], ephemeral: true });

src/commands/moderation/prefixCommands/functions/setContent.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ const noModLogs = makeEmbed({
6969
});
7070

7171
export async function handleSetPrefixCommandContent(interaction: ChatInputCommandInteraction<'cached'>) {
72-
await interaction.deferReply({ ephemeral: true });
73-
7472
const conn = getConn();
7573
if (!conn) {
7674
await interaction.reply({ embeds: [noConnEmbed], ephemeral: true });
@@ -86,7 +84,7 @@ export async function handleSetPrefixCommandContent(interaction: ChatInputComman
8684
foundCommands = await PrefixCommand.find({ aliases: { $in: [command] } });
8785
}
8886
if (!foundCommands || foundCommands.length !== 1) {
89-
await interaction.followUp({ embeds: [noCommandEmbed(command)], ephemeral: true });
87+
await interaction.reply({ embeds: [noCommandEmbed(command)], ephemeral: true });
9088
return;
9189
}
9290

@@ -101,13 +99,12 @@ export async function handleSetPrefixCommandContent(interaction: ChatInputComman
10199
if (foundVersions && foundVersions.length === 1) {
102100
[{ _id: versionId }] = foundVersions;
103101
} else {
104-
await interaction.followUp({ embeds: [noVersionEmbed(version)], ephemeral: true });
102+
await interaction.reply({ embeds: [noVersionEmbed(version)], ephemeral: true });
105103
return;
106104
}
107105
}
108106

109-
const foundContent = foundCommand.contents.find((c) => c.versionId === versionId);
110-
107+
const foundContent = foundCommand.contents.find((c) => c.versionId.toString() === versionId.toString());
111108
const contentModal = new ModalBuilder({
112109
customId: 'commandContentModal',
113110
title: `Content for ${command} - ${version}`,

0 commit comments

Comments
 (0)