From 2bec2a56c474120851dd4f925e031a123f77b177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=86=E3=81=95=E3=81=BF=E3=82=87=E3=82=93/myon2019?= <56076195+mtripg6666tdr@users.noreply.github.com> Date: Thu, 27 Jun 2024 00:25:11 +0900 Subject: [PATCH] Feat: add item more smoothly --- src/Component/playManager.ts | 2 +- src/Component/queueManager.ts | 74 ++++++++++++++++++++++------- src/Structure/GuildDataContainer.ts | 2 +- src/types/General.d.ts | 19 ++++++++ 4 files changed, 78 insertions(+), 19 deletions(-) create mode 100644 src/types/General.d.ts diff --git a/src/Component/playManager.ts b/src/Component/playManager.ts index 5972a1b18..978c642ae 100644 --- a/src/Component/playManager.ts +++ b/src/Component/playManager.ts @@ -272,7 +272,7 @@ export class PlayManager extends ServerManagerBase { return null; }); messageIsSending = false; - }, 3e3).unref(); + }, 2e3).unref(); messageSendingScheduledAt = Date.now(); } diff --git a/src/Component/queueManager.ts b/src/Component/queueManager.ts index 4fe5e392e..33da308cc 100644 --- a/src/Component/queueManager.ts +++ b/src/Component/queueManager.ts @@ -17,7 +17,6 @@ */ import type { InteractionCollector } from "./collectors/InteractionCollector"; -import type { ResponseMessage } from "./commandResolver/ResponseMessage"; import type { TaskCancellationManager } from "./taskCancellationManager"; import type { AudioSourceBasicJsonFormat } from "../AudioSource"; import type { GuildDataContainer } from "../Structure"; @@ -25,12 +24,14 @@ import type { AddedBy, QueueContent } from "../types/QueueContent"; import type { AnyTextableGuildChannel, EditMessageOptions, Message, MessageActionRow } from "oceanic.js"; import { lock, LockObj } from "@mtripg6666tdr/async-lock"; +import { CommandMessage as LibCommandMessage } from "@mtripg6666tdr/oceanic-command-resolver"; import { MessageActionRowBuilder, MessageButtonBuilder, MessageEmbedBuilder } from "@mtripg6666tdr/oceanic-command-resolver/helper"; import i18next from "i18next"; import { Member } from "oceanic.js"; import ytmpl from "yt-mix-playlist"; import ytdl from "ytdl-core"; +import { ResponseMessage } from "./commandResolver/ResponseMessage"; import * as AudioSource from "../AudioSource"; import { getCommandExecutionContext } from "../Commands"; import { ServerManagerBase } from "../Structure"; @@ -272,7 +273,7 @@ export class QueueManager extends ServerManagerBase { channel?: undefined, } | { fromSearch?: undefined, - message: ResponseMessage, + message: ResponseMessage | LibCommandMessage, channel?: undefined, } | { fromSearch?: undefined, @@ -281,9 +282,13 @@ export class QueueManager extends ServerManagerBase { }) ): Promise { this.logger.info("AutoAddQueue Called"); - let uiMessage: Message | ResponseMessage | null = null; + const { t } = getCommandExecutionContext(); + let uiMessage: Message | LibCommandMessage | ResponseMessage | null = null; + let uiMessageTimeout: NodeJS.Timeout | null = null; + let uiMessageSending = false; + try{ // UI表示するためのメッセージを特定する作業 if(options.fromSearch){ @@ -306,13 +311,32 @@ export class QueueManager extends ServerManagerBase { }else if(options.message){ // すでに処理中メッセージがある場合 this.logger.info("AutoAddQueue will report statuses to the specified message"); - uiMessage = options.message; + const optionsMessage = uiMessage = options.message; + if(optionsMessage instanceof LibCommandMessage){ + uiMessageTimeout = setTimeout(async () => { + uiMessageSending = true; + uiMessage = await optionsMessage.reply({ + content: t("loadingInfoPleaseWait"), + }).catch(er => { + this.logger.error(er); + return null; + }); + uiMessageSending = false; + }, 2e3).unref(); + } }else if(options.channel){ // まだないの場合(新しくUI用のメッセージを生成する) this.logger.info("AutoAddQueue will make a message that will be used to report statuses"); - uiMessage = await options.channel.createMessage({ - content: t("loadingInfoPleaseWait"), - }); + uiMessageTimeout = setTimeout(async () => { + uiMessageSending = true; + uiMessage = await options.channel.createMessage({ + content: t("loadingInfoPleaseWait"), + }).catch(er => { + this.logger.error(er); + return null; + }); + uiMessageSending = false; + }, 2e3).unref(); } // キューの長さ確認 @@ -341,7 +365,7 @@ export class QueueManager extends ServerManagerBase { this.logger.info("AutoAddQueue worked successfully"); // UIを表示する - if(uiMessage){ + if(uiMessageTimeout || uiMessage){ // 曲の時間取得&計算 const _t = Number(info.basicInfo.lengthSeconds); const [min, sec] = Util.time.calcMinSec(_t); @@ -459,7 +483,7 @@ export class QueueManager extends ServerManagerBase { this.once("changeWithoutCurrent", destroyCollector); } - let messageContent: EditMessageOptions | null = null; + let messageContent: ExcludeNullValue | null = null; if(typeof info.basicInfo.thumbnail === "string"){ embed.setThumbnail(info.basicInfo.thumbnail); messageContent = { @@ -482,7 +506,20 @@ export class QueueManager extends ServerManagerBase { }; } - const lastReply = await uiMessage.edit(messageContent); + if(uiMessageTimeout){ + clearTimeout(uiMessageTimeout); + } + + if(uiMessageSending){ + await Util.waitForEnteringState(() => !uiMessageSending, 10e3, { rejectOnTimeout: false }); + } + + const lastReply: Message | ResponseMessage = uiMessage + ? uiMessage instanceof LibCommandMessage + ? await uiMessage.reply(messageContent) + : await uiMessage.edit(messageContent) + : await options.channel!.createMessage(messageContent); + collector?.setMessage(lastReply); } return info; @@ -490,14 +527,17 @@ export class QueueManager extends ServerManagerBase { catch(e){ this.logger.error("AutoAddQueue failed", e); if(uiMessage){ - uiMessage.edit({ - content: `:weary: ${t("components:queue.failedToAdd")}${ - typeof e === "object" && "message" in e ? `(${e.message})` : "" - }`, + const errorMessage = Util.stringifyObject(e); + const errorMessageContent = { + content: `:weary: ${t("components:queue.failedToAdd")}${errorMessage ? `(${errorMessage})` : ""}`, embeds: [], - }) - .catch(this.logger.error) - ; + }; + + if(uiMessage instanceof LibCommandMessage){ + uiMessage.reply(errorMessageContent).catch(this.logger.error); + }else{ + uiMessage.edit(errorMessageContent).catch(this.logger.error); + } } return null; } diff --git a/src/Structure/GuildDataContainer.ts b/src/Structure/GuildDataContainer.ts index 33e5d8bd8..16fbc9c3f 100644 --- a/src/Structure/GuildDataContainer.ts +++ b/src/Structure/GuildDataContainer.ts @@ -767,7 +767,7 @@ export class GuildDataContainer extends LogEmitter { url: rawArg, addedBy: message.member, first, - message: await message.reply(`${t("pleaseWait")}...`), + message, cancellable, privateSource, }); diff --git a/src/types/General.d.ts b/src/types/General.d.ts new file mode 100644 index 000000000..0a80e38a3 --- /dev/null +++ b/src/types/General.d.ts @@ -0,0 +1,19 @@ +/* + * Copyright 2021-2024 mtripg6666tdr + * + * This file is part of mtripg6666tdr/Discord-SimpleMusicBot. + * (npm package name: 'discord-music-bot' / repository url: ) + * + * mtripg6666tdr/Discord-SimpleMusicBot is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free Software Foundation, + * either version 3 of the License, or (at your option) any later version. + * + * mtripg6666tdr/Discord-SimpleMusicBot is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with mtripg6666tdr/Discord-SimpleMusicBot. + * If not, see . + */ + +type ExcludeNullValue = { [key: keyof T]: Exclude };