Skip to content

Commit

Permalink
Added peek (pls work)
Browse files Browse the repository at this point in the history
  • Loading branch information
coinmoles committed Mar 2, 2024
1 parent d7181fc commit f34fb90
Show file tree
Hide file tree
Showing 6 changed files with 499 additions and 79 deletions.
190 changes: 121 additions & 69 deletions src/commands/missionGive/postMission/operations.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,138 @@
import { ButtonInteraction, ChatInputCommandInteraction, EmbedBuilder, ModalSubmitInteraction, User } from "discord.js"
import { postMission } from "../../../db/actions/missionActions.js"
import { Mission } from "../../../interfaces/models/Mission.js"
import { checkAssociate } from "../../utils/checks/checkAssociate.js"
import { addConfirmCollector, createConfirmActionRow } from "../../utils/components/confirmActionRow.js"
import { createMissionModal, createMissionModalId, getMissionModalMission } from "../../utils/components/missionModal.js"
import { createMissionPreviewString, createMissionPreviewTitle } from "../../utils/createString/createMissionPreviewString.js"
import { errorEmbed } from "../../utils/errorEmbeds.js"
import { getQuarterDataFooter } from "../../utils/quarterData/getQuarterData.js"
import builders from "./builders.js"
import {
ButtonInteraction,
ChatInputCommandInteraction,
EmbedBuilder,
ModalSubmitInteraction,
User,
} from "discord.js";
import { postMission } from "../../../db/actions/missionActions.js";
import { Mission } from "../../../interfaces/models/Mission.js";
import { checkAssociate } from "../../utils/checks/checkAssociate.js";
import {
addConfirmCollector,
createConfirmActionRow,
} from "../../utils/components/confirmActionRow.js";
import {
createMissionModal,
createMissionModalId,
getMissionModalMission,
} from "../../utils/components/missionModal.js";
import {
createMissionPreviewString,
createMissionPreviewTitle,
} from "../../utils/createString/createMissionPreviewString.js";
import { errorEmbed } from "../../utils/errorEmbeds.js";
import { getQuarterDataFooter } from "../../utils/quarterData/getQuarterData.js";
import builders from "./builders.js";

const {
commandId,
invalidScoreEmbed,
replyEmbedPrototype,
successEmbedPrototype,
cancelEmbedPrototype,
} = builders
commandId,
invalidScoreEmbed,
replyEmbedPrototype,
successEmbedPrototype,
cancelEmbedPrototype,
} = builders;

export const readOptions = (interaction: ChatInputCommandInteraction) => ({
target: interaction.options.getUser("대상", true)
})
target: interaction.options.getUser("대상", true),
});

const onConfirm = (modalInteraction: ModalSubmitInteraction, target: User, index: number, mission: Mission) =>
async (buttonInteraction: ButtonInteraction) => {
await buttonInteraction.deferReply({ ephemeral: true })
const onConfirm =
(
modalInteraction: ModalSubmitInteraction,
target: User,
index: number,
mission: Mission
) =>
async (buttonInteraction: ButtonInteraction) => {
await buttonInteraction.deferReply({ ephemeral: true });

const success = await postMission(index, mission)
const success = await postMission(index, mission);

if (success) {
const successEmbed = new EmbedBuilder(successEmbedPrototype.toJSON())
.addFields({ name: createMissionPreviewTitle(mission, target), value: createMissionPreviewString(mission, target) })
if (success) {
const successEmbed = new EmbedBuilder(
successEmbedPrototype.toJSON()
).addFields({
name: createMissionPreviewTitle(mission, target),
value: createMissionPreviewString(mission, target),
});

await buttonInteraction.deleteReply()
await modalInteraction.editReply({ embeds: [successEmbed.setFooter(await getQuarterDataFooter())], components: [] })
}
else
await buttonInteraction.editReply({ embeds: [errorEmbed] })
}
await buttonInteraction.deleteReply();
await modalInteraction.editReply({
embeds: [successEmbed.setFooter(await getQuarterDataFooter())],
components: [],
});
} else await buttonInteraction.editReply({ embeds: [errorEmbed] });
};

const onCancel = (modalInteraction: ModalSubmitInteraction, target: User, mission: Mission) =>
async (buttonInteraction: ButtonInteraction) => {
await buttonInteraction.deferUpdate()
const onCancel =
(modalInteraction: ModalSubmitInteraction, target: User, mission: Mission) =>
async (buttonInteraction: ButtonInteraction) => {
await buttonInteraction.deferUpdate();

const cancelEmbed = new EmbedBuilder(cancelEmbedPrototype.toJSON())
.addFields({ name: createMissionPreviewTitle(mission, target), value: createMissionPreviewString(mission, target) })

await modalInteraction.editReply({ embeds: [cancelEmbed.setFooter(await getQuarterDataFooter())], components: [] })
}
const cancelEmbed = new EmbedBuilder(
cancelEmbedPrototype.toJSON()
).addFields({
name: createMissionPreviewTitle(mission, target),
value: createMissionPreviewString(mission, target),
});

export const doReply = async (interaction: ChatInputCommandInteraction, target: User, isEditing: boolean = false) => {
if (!await checkAssociate(interaction, target.id, true))
return
await modalInteraction.editReply({
embeds: [cancelEmbed.setFooter(await getQuarterDataFooter())],
components: [],
});
};

await interaction.showModal(createMissionModal(commandId, interaction.user, target))
export const doReply = async (
interaction: ChatInputCommandInteraction,
target: User,
isEditing: boolean = false
) => {
if (!(await checkAssociate(interaction, target.id, true))) return;

const modalInteraction = await interaction.awaitModalSubmit({
filter: mI => mI.customId === createMissionModalId(commandId, interaction.user),
time: 60_000
}).catch(_ => null)
if (modalInteraction === null)
return
await interaction.showModal(
createMissionModal(commandId, interaction.user, target)
);

const modalValue = getMissionModalMission(modalInteraction, target)
if (modalValue === undefined) {
await modalInteraction.reply({ embeds: [invalidScoreEmbed], ephemeral: true })
return
}
const modalInteraction = await interaction
.awaitModalSubmit({
filter: (mI) =>
mI.customId === createMissionModalId(commandId, interaction.user),
time: 60_000,
})
.catch((_) => null);
if (modalInteraction === null) return;

const { index, mission } = modalValue
const reply = await modalInteraction.reply({
embeds: [EmbedBuilder.from(replyEmbedPrototype)
.addFields({ name: createMissionPreviewTitle(mission, target), value: createMissionPreviewString(mission, target) })
.setFooter(await getQuarterDataFooter())],
components: [createConfirmActionRow(commandId, modalInteraction.user)],
ephemeral: true,
fetchReply: true
const modalValue = getMissionModalMission(modalInteraction, target);
if (modalValue === undefined) {
await modalInteraction.reply({
embeds: [invalidScoreEmbed],
ephemeral: true,
});
return;
}

const { index, mission } = modalValue;
const reply = await modalInteraction.reply({
embeds: [
EmbedBuilder.from(replyEmbedPrototype)
.addFields({
name: createMissionPreviewTitle(mission, target),
value: createMissionPreviewString(mission, target),
})
.setFooter(await getQuarterDataFooter()),
],
components: [createConfirmActionRow(commandId, modalInteraction.user)],
ephemeral: true,
fetchReply: true,
});

if (!isEditing)
await addConfirmCollector(
commandId, modalInteraction, reply,
onConfirm(modalInteraction, target, index, mission),
onCancel(modalInteraction, target, mission)
)
}
if (!isEditing)
await addConfirmCollector(
commandId,
modalInteraction,
reply,
onConfirm(modalInteraction, target, index, mission),
onCancel(modalInteraction, target, mission)
);
};
46 changes: 46 additions & 0 deletions src/commands/peek/builders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
ActionRowBuilder,
EmbedBuilder,
MessageActionRowComponentBuilder,
UserSelectMenuBuilder,
} from "discord.js";
import { normalColor } from "../utils/colors.js";

const noUserEmbed = new EmbedBuilder()
.setTitle("정회원 또는 준회원을 입력하세요")
.setColor(normalColor);
const viewListEmbedPrototype = new EmbedBuilder().setColor(normalColor);

const noAssociateEmbed = new EmbedBuilder()
.setTitle("아직 승격신청을 한 준회원이 없습니다.")
.setColor(normalColor);

const REGULAR_MENU_ID = "regular-menu";
const regularMenu = new UserSelectMenuBuilder()
.setCustomId(REGULAR_MENU_ID)
.setPlaceholder("승격조건 목록을 확인하고 싶은 정회원");
const ASSOCITE_MENU_ID = "associate-menu";
const associateMenu = new UserSelectMenuBuilder()
.setCustomId(ASSOCITE_MENU_ID)
.setPlaceholder("승격조건 목록을 확인하고 싶은 준회원");

const actionRow1 =
new ActionRowBuilder<MessageActionRowComponentBuilder>().addComponents(
regularMenu
);
const actionRow2 =
new ActionRowBuilder<MessageActionRowComponentBuilder>().addComponents(
associateMenu
);

export default {
noUserEmbed,
noAssociateEmbed,
viewListEmbedPrototype,
REGULAR_MENU_ID,
regularMenu,
ASSOCITE_MENU_ID,
associateMenu,
actionRow1,
actionRow2
};
21 changes: 12 additions & 9 deletions src/commands/peek/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import {
SlashCommandContainer,
SlashCommandSubcommandsOnlyContainer,
} from "../../interfaces/commands/CommandContainer.js";
import { SlashCommandContainer } from "../../interfaces/commands/CommandContainer.js";
import { CommandType } from "../../interfaces/commands/CommandTypes.js";
import { MySlashCommandBuilder } from "../MySlashCommandBuilder.js";
import reply from "./reply.js";

const name = "엿보기";
const description = "자신 외의 정회원 또는 준회원이 받은 승격조건을 확인합니다";

const subcommands = [];
const description =
"자신 외의 정회원 또는 준회원이 받은 승격조건을 확인합니다.";

const peek: SlashCommandContainer = {
commandType: CommandType.Public,
builder: new MySlashCommandBuilder()
.setName(name)
.setDescription(description),
callback: async () => {},
.setDescription(description)
.addUserOption((option) =>
option.setName("정회원").setDescription("승격조건 목록을 확인할 정회원")
)
.addUserOption((option) =>
option.setName("준회원").setDescription("승격조건 목록을 확인할 준회원")
),
callback: reply,
};

export default peek;
Loading

0 comments on commit f34fb90

Please sign in to comment.