Skip to content

Commit 3967719

Browse files
committed
Enhance error handling and improve embed modification in info_ modal
1 parent a82f035 commit 3967719

1 file changed

Lines changed: 87 additions & 63 deletions

File tree

modals/info_.js

Lines changed: 87 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,89 @@
11
module.exports = {
2-
modal: {
3-
name: "info_",
4-
},
5-
run: async (client, interaction, prisma) => {
6-
const judge_msg = await prisma.build.findUnique({
7-
where: {
8-
id: parseInt(interaction.customId.split("_")[1]),
9-
},
10-
});
11-
12-
//get the message
13-
const message = await client.channels.cache
14-
.get(process.env.JUDGE_CHANNEL)
15-
.messages.fetch(judge_msg.judge_msg.toString());
16-
let newMessage = message;
17-
18-
const possible_Inputs = [
19-
"3d_view",
20-
"street_view",
21-
"street_view_link",
22-
"other_info",
23-
];
24-
const value = possible_Inputs
25-
.map((v) => {
26-
const value = interaction.fields.getTextInputValue(v);
27-
if (!value || value === "") return;
28-
switch (v) {
29-
case "3d_view":
30-
return `3D-View benutzt: ${value}`;
31-
case "street_view":
32-
return `Street-View benutzt: ${value}`;
33-
case "street_view_link":
34-
return `Link zu Street-View: ${value}`;
35-
case "other_info":
36-
return `Sonstige Informationen: ${value}`;
2+
modal: {
3+
name: "info_",
4+
},
5+
run: async (client, interaction, prisma) => {
6+
try {
7+
// Get the judge message
8+
const judge_msg = await prisma.build.findUnique({
9+
where: {
10+
id: parseInt(interaction.customId.split("_")[1]),
11+
},
12+
});
13+
14+
if (!judge_msg || !judge_msg.judge_msg) {
15+
console.error("No message ID found in judge_msg.");
16+
return;
3717
}
38-
})
39-
.filter((v) => v !== undefined)
40-
.join("\n");
41-
42-
if (!newMessage.embeds[0].fields) {
43-
newMessage.embeds[0].fields = [];
44-
}
45-
46-
newMessage.embeds[0].fields.push({
47-
name: "Zusätzliche Informationen",
48-
value: value,
49-
});
50-
51-
console.log(newMessage.embeds);
52-
53-
//edit the message
54-
await message.edit({
55-
content: newMessage.content,
56-
embeds: newMessage.embeds,
57-
});
58-
59-
//reply to the user
60-
await interaction.reply({
61-
content: "Zusätzliche Informationen hinzugefügt.",
62-
ephemeral: true,
63-
});
64-
},
65-
};
18+
19+
// Get the message from the channel
20+
const channel = await client.channels.fetch(process.env.JUDGE_CHANNEL);
21+
const message = await channel.messages.fetch(judge_msg.judge_msg.toString());
22+
23+
let newEmbeds = [...message.embeds]; // Clone all existing embeds
24+
25+
// Check if the message has at least one embed
26+
if (newEmbeds.length > 0) {
27+
// Clone the first embed and modify it
28+
const firstEmbed = newEmbeds[0].toJSON();
29+
30+
const possible_Inputs = [
31+
"3d_view",
32+
"street_view",
33+
"street_view_link",
34+
"other_info",
35+
];
36+
37+
const value = possible_Inputs
38+
.map((v) => {
39+
const inputValue = interaction.fields.getTextInputValue(v);
40+
if (!inputValue || inputValue === "") return;
41+
switch (v) {
42+
case "3d_view":
43+
return `3D-View benutzt: ${inputValue}`;
44+
case "street_view":
45+
return `Street-View benutzt: ${inputValue}`;
46+
case "street_view_link":
47+
return `Link zu Street-View: ${inputValue}`;
48+
case "other_info":
49+
return `Sonstige Informationen: ${inputValue}`;
50+
}
51+
})
52+
.filter((v) => v !== undefined)
53+
.join("\n");
54+
55+
// Modify the fields of the first embed
56+
if (!firstEmbed.fields) {
57+
firstEmbed.fields = []; // Initialize fields if they don't exist
58+
}
59+
60+
firstEmbed.fields.push({
61+
name: "Zusätzliche Informationen",
62+
value: value,
63+
});
64+
65+
// Replace the first embed with the modified one
66+
newEmbeds[0] = firstEmbed;
67+
}
68+
69+
// Edit the message with the updated embeds (all the original embeds + modified first embed)
70+
await message.edit({
71+
content: message.content, // Keep the original content (if you want)
72+
embeds: newEmbeds, // Pass the modified array of embeds
73+
});
74+
75+
// Reply to the user
76+
await interaction.reply({
77+
content: "Zusätzliche Informationen hinzugefügt.",
78+
ephemeral: true,
79+
});
80+
} catch (error) {
81+
console.error("Error during operation:", error);
82+
await interaction.reply({
83+
content: "Etwas ist schiefgegangen. Bitte versuche es später noch einmal.",
84+
ephemeral: true,
85+
});
86+
}
87+
},
88+
};
89+

0 commit comments

Comments
 (0)