From 6a3bb6ae8ff61df2f5a0082a3b6cc925d903b958 Mon Sep 17 00:00:00 2001 From: cm-ayf Date: Sat, 22 Jun 2024 09:36:51 +0900 Subject: [PATCH] resolve type issues by with declare module "node:events" in discord.js see: https://github.com/discordjs/discord.js/pull/10360 --- package-lock.json | 1 - src/pipeline.ts | 10 +++++++--- src/synthesis/index.ts | 7 +++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2e28e8db..a08fdba8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -54,7 +54,6 @@ "version": "0.4.1", "resolved": "git+ssh://git@github.com/discordjs-japan/om-syrinx.git#dafb14ba32fb8e3533edebfa8049b5d99c77e720", "hasInstallScript": true, - "license": "MIT", "engines": { "node": ">= 18" } diff --git a/src/pipeline.ts b/src/pipeline.ts index 1574b506..5eecf421 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -1,4 +1,8 @@ -import { EventEmitter, once } from "events"; +// do not 'import { EventEmitter } from "events"'; +// it would also refer to 'declare module "node:events" { /* snip */ } in discord.js +// and cause 'EventEmitter to be non-generic. +// pay attention to https://github.com/discordjs/discord.js/pull/10360 +import EventEmitter from "events"; import { joinVoiceChannel, type CreateVoiceConnectionOptions, @@ -115,7 +119,7 @@ export default class Pipeline extends EventEmitter { } async ready(signal?: AbortSignal) { - await once(this, "ready", { signal }); + await Pipeline.once(this, "ready", { signal }); } play() { @@ -150,7 +154,7 @@ export default class Pipeline extends EventEmitter { async disconnect(signal?: AbortSignal) { setImmediate(() => this.connection?.disconnect()); - await once(this, "disconnect", { signal }); + await Pipeline.once(this, "disconnect", { signal }); } } diff --git a/src/synthesis/index.ts b/src/synthesis/index.ts index 36ce0caf..12050ab4 100644 --- a/src/synthesis/index.ts +++ b/src/synthesis/index.ts @@ -1,4 +1,4 @@ -import { once } from "node:events"; +import { Readable } from "node:stream"; import { StreamType, createAudioResource } from "@discordjs/voice"; import { EncoderType, Syrinx } from "@discordjs-japan/om-syrinx"; import type { Message } from "discord.js"; @@ -22,7 +22,10 @@ export async function synthesize(message: Message) { const option = createSynthesisOption(message); const stream = syrinx.synthesize(inputText, option); - await once(stream, "readable"); + // HACK: while `EventEmitter.once` or `once` from "node:events" somehow does not accept `Readable` type, + // `Readable.once` does accept it because it refers to 'declare module "node:events" { /* snip */ }' in discord.js. + // pay attention to https://github.com/discordjs/discord.js/pull/10360 + await Readable.once(stream, "readable"); if (stream.readableLength === 0) return null; return createAudioResource(stream, {