Skip to content

Commit

Permalink
Add modal for additional information input and update message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Mic-R committed Feb 9, 2025
1 parent c26b13a commit 1898945
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 14 deletions.
53 changes: 53 additions & 0 deletions buttons/info_.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const { ModalBuilder, TextInputBuilder, TextInputStyle, ActionRowBuilder } = require('@discordjs/builders');

module.exports = {
button: {
name: "info_"
},
run: async (client, interaction, prisma) => {
// Create the modal
const modal = new ModalBuilder()
.setCustomId(interaction.customId)
.setTitle('Zusätzliche Informationen');

//boolean input "3D-View benutzt"
const input1 = new TextInputBuilder()
.setCustomId('3d_view')
.setLabel('3D-View benutzt')
.setPlaceholder('Ja/Nein')
.setStyle(TextInputStyle.DEFAULT);

//boolean input "Street-View benutzt"
const input2 = new TextInputBuilder()
.setCustomId('street_view')
.setLabel('Street-View benutzt')
.setPlaceholder('Ja/Nein')
.setStyle(TextInputStyle.DEFAULT);

//text input "Link zu Street-View"
const input3 = new TextInputBuilder()
.setCustomId('street_view_link')
.setLabel('Link zu Street-View')
.setPlaceholder('Link')
.setStyle(TextInputStyle.DEFAULT);

//text input "Sonstige Informationen"
const input4 = new TextInputBuilder()
.setCustomId('other_info')
.setLabel('Sonstige Informationen')
.setPlaceholder('Informationen')
.setStyle(TextInputStyle.DEFAULT);


// Add inputs to the modal
modal.addComponents(
new ActionRowBuilder().addComponents(input1),
new ActionRowBuilder().addComponents(input2),
new ActionRowBuilder().addComponents(input3),
new ActionRowBuilder().addComponents(input4)
);

// Show the modal to the user
await interaction.showModal(modal);
}
}
14 changes: 8 additions & 6 deletions commands/judge.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ module.exports = {
let embeds = [
{
title: `#${build.id.toString()}`,
description: `Koordinaten: ${build.location}`,
description: `Koordinaten: ${build.location} \n Grundpunkte: ${base_points ? "Ja" : "Nein"} \n Bewertet von: ${interaction.member.user.username}`,
url: "https://bte-germany.de",
color: 16761344,
author: {
Expand Down Expand Up @@ -231,17 +231,19 @@ module.exports = {
});
});
await client.channels.cache
.get(process.env.JUDGE_CHANNEL)
.messages.fetch(build.judge_msg.toString())
.get(process.env.SUBMISSION_CHANNEL)
.messages.fetch(build.message.toString())
.then((message) => {
message.edit({
content: " ",
embeds: embeds,
});
});
await client.channels.cache
.get(process.env.SUBMISSION_CHANNEL)
.messages.fetch(build.message.toString())

embeds[0].description += `\n Bewertet von: <@${build.judges[0]}> und <@${interaction.member.user.id}>`;
await client.channels.cache
.get(process.env.JUDGE_CHANNEL)
.messages.fetch(build.judge_msg.toString())
.then((message) => {
message.edit({
content: " ",
Expand Down
14 changes: 14 additions & 0 deletions event/messageCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ module.exports = {
.send({
content: " ",
embeds: embeds,
components: [
{
type: 1,
components: [
{
type: 2,
style: 2,
label: "Zusätzliche Informationen",
custom_id: `info_${obj.id}`,
emoji: "📍"
}
]
}
]
})
.then(async (message) => {
await prisma.build.update({
Expand Down
43 changes: 35 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const api = require("./api/api.js");

const commands = [];
const buttons = [];
const modals = [];
client.on("ready", () => {
console.log(new Date().toLocaleString(), "Ready!");
});
Expand Down Expand Up @@ -73,20 +74,28 @@ client.once("ready", async () => {
}

const buttonFiles = fs
.readdirSync("./buttons")
.filter((file) => file.endsWith(".js"));
.readdirSync("./buttons")
.filter((file) => file.endsWith(".js"));
for (const file of buttonFiles) {
let data = require(`./buttons/${file}`);
buttons.push(data);
}

const modalFiles = fs
.readdirSync("./modals")
.filter((file) => file.endsWith(".js"));
for (const file of modalFiles) {
let data = require(`./modals/${file}`);
modals.push(data);
}

await api.start();
});

client.on("interactionCreate", async (interaction) => {
if (interaction.isCommand()) {
const command = commands.find(
(command) => command.command.name === interaction.commandName
(command) => command.command.name === interaction.commandName
);
if (!command) return;
try {
Expand All @@ -100,16 +109,34 @@ client.on("interactionCreate", async (interaction) => {
}
}
if (interaction.isButton()) {
const button = buttons.find(
(button) => interaction.customId.startsWith(button.button.name)
)
if(!button) return;
const button = buttons.find((button) =>
interaction.customId.startsWith(button.button.name)
);
if (!button) return;
try {
await button.run(client, interaction, prisma);
} catch (error) {
console.error(error);
await interaction.reply({
content: "Beim Ausführen dieser Schaltfläche ist ein Fehler aufgetreten! ",
content:
"Beim Ausführen dieser Schaltfläche ist ein Fehler aufgetreten! ",
ephemeral: true,
});
}
}

//is modal
if (interaction.isModalSubmit()) {
const modal = modals.find((modal) =>
interaction.customId.startsWith(modal.modal.name)
);
if (!modal) return;
try {
await modal.run(client, interaction, prisma);
} catch (error) {
console.error(error);
await interaction.reply({
content: "Beim Ausführen dieses Modals ist ein Fehler aufgetreten! ",
ephemeral: true,
});
}
Expand Down
43 changes: 43 additions & 0 deletions modals/info_.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module.exports = {
modal: {
name: "info_"
},
run: async (client, interaction, prisma) => {
//handle modal submit
//add information to judge_msg in discord
//get judge_msg
const judge_msg = await prisma.build.findUnique({
where: {
id: interaction.customId.split("_")[1]
}
});

//get the message
const message = await client.channels.cache.get(process.env.JUDGE_CHANNEL).messages.fetch(judge_msg.judge_msg.toString());
let newMessage = message;
message.embeds[0].fields.push({
name: "Zusätzliche Informationen",
value: interaction.values.map((v) => {
switch(v.customId) {
case "3d_view":
return `3D-View benutzt: ${v.value}`;
case "street_view":
return `Street-View benutzt: ${v.value}`;
case "street_view_link":
return `Link zu Street-View: ${v.value}`;
case "other_info":
return `Sonstige Informationen: ${v.value}`;
}
}).join("\n")
});

//edit the message
message.edit(
{
content: newMessage.content,
embeds: newMessage.embeds
}
)

}
}

0 comments on commit 1898945

Please sign in to comment.