Skip to content

Commit

Permalink
Merge pull request #309 from elecdeer/feat/#308_replyHelper
Browse files Browse the repository at this point in the history
Feat/#308 reply helper
  • Loading branch information
elecdeer authored Jun 19, 2022
2 parents 1bfe665 + a9443c5 commit 07e8443
Show file tree
Hide file tree
Showing 7 changed files with 555 additions and 162 deletions.
12 changes: 8 additions & 4 deletions src/handler/command/testCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,19 @@ export class TestCommand extends CommandHandler {
},
}),
},
{
type: "commandInteraction",
destination: context.interaction,
},
{
messageContent: createYosugaEmbed({
message: "toggle test",
}),
ephemeral: true,
scene: {
type: "textChannel",
channel: context.textChannel,
},
rootTarget: {
type: "commandInteraction",
interaction: context.interaction,
},
}
);

Expand Down
9 changes: 1 addition & 8 deletions src/inquirer/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { TypedEventEmitter } from "../util/typedEventEmitter";
import { createPromptCollector } from "./promptCollector";
import { createPromptController } from "./promptController";

import type { ReplyDestination } from "../util/replyHelper";
import type {
AnswerStatus,
PromptCollector,
Expand All @@ -17,7 +16,6 @@ import type {

export const prompt = async <T extends Record<string, PromptComponent<unknown>>>(
components: T | [keyof T, T[keyof T]][],
replyDestination: ReplyDestination,
param: PromptParam
): Promise<{
controller: PromptController;
Expand All @@ -38,12 +36,7 @@ export const prompt = async <T extends Record<string, PromptComponent<unknown>>>
answerStatus.set(key, status);
});

const controller = await createPromptController(
componentCollection,
event,
replyDestination,
param
);
const controller = await createPromptController(componentCollection, event, param);
const collector = createPromptCollector(answerStatus, event);

return {
Expand Down
57 changes: 22 additions & 35 deletions src/inquirer/promptController.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { resolveLazy } from "../util/lazy";
import { createReplyHelper } from "../util/replyHelper";
import { createReplyHelper } from "../util/replyHelpter";

import type { ReplyDestination } from "../util/replyHelper";
import type { ReplyTarget } from "../util/replyHelpter";
import type { TypedEventEmitter } from "../util/typedEventEmitter";
import type { PromptComponent, PromptController, PromptEvent, PromptParam } from "./promptTypes";
import type { Awaitable, Collection, Message } from "discord.js";

export const createPromptController = async <T extends Record<string, PromptComponent<unknown>>>(
componentCollection: Collection<keyof T, T[keyof T]>,
event: TypedEventEmitter<PromptEvent<T>>,
replyDestination: ReplyDestination,
param: PromptParam
): Promise<PromptController> => {
const replyHelper = createReplyHelper(replyDestination, {});
const replyHelper = await createReplyHelper(param.scene);

const renderActionRows = () => {
return componentCollection.map((item) => item.renderComponent()).flat();
Expand Down Expand Up @@ -40,26 +39,26 @@ export const createPromptController = async <T extends Record<string, PromptComp
.filter((item) => !!item) as (() => Awaitable<void>)[];
};

const post = async (destination?: ReplyDestination) => {
const post = async (target?: ReplyTarget) => {
const actionRows = renderActionRows();

if (destination) {
replyHelper.changeDestination(destination);
}

const message = await replyHelper.reply({
embeds: [resolveLazy(param.messageContent)],
components: actionRows,
ephemeral: param.ephemeral,
});
const message = await replyHelper.reply(
{
embeds: [resolveLazy(param.messageContent)],
components: actionRows,
ephemeral: param.ephemeral,
},
target
);

await hookComponents(message);
};

//これまでに送ったMessageからcomponentsを削除
const removeComponentsFromSendMessages = async (rerender?: boolean) => {
await Promise.all(
replyHelper.postedMessages
replyHelper
.postedMessages()
.filter((msg) => msg.components.length > 0)
.map(async (msg) => {
await msg.edit(
Expand All @@ -85,35 +84,23 @@ export const createPromptController = async <T extends Record<string, PromptComp
const edit = async () => {
const actionRows = renderActionRows();

const lastMessage = replyHelper.postedMessages.at(-1);
if (!lastMessage) {
return;
}
const editMessage = await replyHelper.edit({
embeds: [resolveLazy(param.messageContent)],
components: actionRows,
});

if (param.ephemeral === true && replyDestination.type === "commandInteraction") {
const editMessage = await replyDestination.destination.editReply({
embeds: [resolveLazy(param.messageContent)],
components: actionRows,
});
await hookComponents(editMessage);
} else {
const editMessage = await lastMessage.edit({
embeds: [resolveLazy(param.messageContent)],
components: actionRows,
});
await hookComponents(editMessage);
}
await hookComponents(editMessage);
};

const repost = async (destination: ReplyDestination, rerender?: boolean) => {
const repost = async (target: ReplyTarget, rerender?: boolean) => {
await removeComponentsFromSendMessages(rerender);
await post(destination);
await post(target);
};

//初回post
//コンポーネントのlazyでpromptの返り値を使えるようにするため、lazyの解決をpromptが返った後にする
setImmediate(() => {
void post(replyDestination);
void post(param.rootTarget);
});

const controller = {
Expand Down
14 changes: 12 additions & 2 deletions src/inquirer/promptTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Lazy } from "../util/lazy";
import type { ReplyDestination } from "../util/replyHelper";
import type { ReplyScene, ReplyTarget } from "../util/replyHelpter";
import type { Message, MessageActionRow, MessageEmbed, Awaitable } from "discord.js";

export type PromptEvent<T extends Record<string, PromptComponent<unknown>>> = {
Expand Down Expand Up @@ -32,13 +32,23 @@ export type PromptParamMessage = {
* 送信するMessageに含まれるコンテント
*/
messageContent: Lazy<MessageEmbed>;

/**
* promptの送信先
*/
scene: ReplyScene;

/**
* 最初のMessageの送信先
*/
rootTarget: ReplyTarget;
};

export type PromptParam = PromptParamHook & PromptParamMessage;

export interface PromptController {
close: (rerender?: boolean) => Promise<void>;
repost: (destination: ReplyDestination, rerender?: boolean) => Promise<void>;
repost: (target: ReplyTarget, rerender?: boolean) => Promise<void>;
edit: () => Promise<void>;
}

Expand Down
Loading

0 comments on commit 07e8443

Please sign in to comment.