Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add item more smoothly #2432

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Component/playManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export class PlayManager extends ServerManagerBase<PlayManagerEvents> {
return null;
});
messageIsSending = false;
}, 3e3).unref();
}, 2e3).unref();
messageSendingScheduledAt = Date.now();
}

Expand Down
74 changes: 57 additions & 17 deletions src/Component/queueManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@
*/

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";
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";
Expand Down Expand Up @@ -272,7 +273,7 @@ export class QueueManager extends ServerManagerBase<QueueManagerEvents> {
channel?: undefined,
} | {
fromSearch?: undefined,
message: ResponseMessage,
message: ResponseMessage | LibCommandMessage,
channel?: undefined,
} | {
fromSearch?: undefined,
Expand All @@ -281,9 +282,13 @@ export class QueueManager extends ServerManagerBase<QueueManagerEvents> {
})
): Promise<QueueContent | null> {
this.logger.info("AutoAddQueue Called");
let uiMessage: Message<AnyTextableGuildChannel> | ResponseMessage | null = null;

const { t } = getCommandExecutionContext();

let uiMessage: Message<AnyTextableGuildChannel> | LibCommandMessage | ResponseMessage | null = null;
let uiMessageTimeout: NodeJS.Timeout | null = null;
let uiMessageSending = false;

try{
// UI表示するためのメッセージを特定する作業
if(options.fromSearch){
Expand All @@ -306,13 +311,32 @@ export class QueueManager extends ServerManagerBase<QueueManagerEvents> {
}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();
}

// キューの長さ確認
Expand Down Expand Up @@ -341,7 +365,7 @@ export class QueueManager extends ServerManagerBase<QueueManagerEvents> {
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);
Expand Down Expand Up @@ -459,7 +483,7 @@ export class QueueManager extends ServerManagerBase<QueueManagerEvents> {
this.once("changeWithoutCurrent", destroyCollector);
}

let messageContent: EditMessageOptions | null = null;
let messageContent: ExcludeNullValue<EditMessageOptions> | null = null;
if(typeof info.basicInfo.thumbnail === "string"){
embed.setThumbnail(info.basicInfo.thumbnail);
messageContent = {
Expand All @@ -482,22 +506,38 @@ export class QueueManager extends ServerManagerBase<QueueManagerEvents> {
};
}

const lastReply = await uiMessage.edit(messageContent);
if(uiMessageTimeout){
clearTimeout(uiMessageTimeout);
}

if(uiMessageSending){
await Util.waitForEnteringState(() => !uiMessageSending, 10e3, { rejectOnTimeout: false });
}

const lastReply: Message<AnyTextableGuildChannel> | ResponseMessage = uiMessage
? uiMessage instanceof LibCommandMessage
? await uiMessage.reply(messageContent)
: await uiMessage.edit(messageContent)
: await options.channel!.createMessage(messageContent);

collector?.setMessage(lastReply);
}
return info;
}
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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Structure/GuildDataContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ export class GuildDataContainer extends LogEmitter<GuildDataContainerEvents> {
url: rawArg,
addedBy: message.member,
first,
message: await message.reply(`${t("pleaseWait")}...`),
message,
cancellable,
privateSource,
});
Expand Down
19 changes: 19 additions & 0 deletions src/types/General.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2021-2024 mtripg6666tdr
*
* This file is part of mtripg6666tdr/Discord-SimpleMusicBot.
* (npm package name: 'discord-music-bot' / repository url: <https://github.com/mtripg6666tdr/Discord-SimpleMusicBot> )
*
* 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 <https://www.gnu.org/licenses/>.
*/

type ExcludeNullValue<T> = { [key: keyof T]: Exclude<T[key], null> };