Skip to content

Commit

Permalink
resolve type issues
Browse files Browse the repository at this point in the history
by with declare module "node:events" in discord.js

see: discordjs/discord.js#10360
  • Loading branch information
cm-ayf committed Jun 22, 2024
1 parent 69e6c0d commit 6a3bb6a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions src/pipeline.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -115,7 +119,7 @@ export default class Pipeline extends EventEmitter<PipelineEventsMap> {
}

async ready(signal?: AbortSignal) {
await once(this, "ready", { signal });
await Pipeline.once(this, "ready", { signal });
}

play() {
Expand Down Expand Up @@ -150,7 +154,7 @@ export default class Pipeline extends EventEmitter<PipelineEventsMap> {

async disconnect(signal?: AbortSignal) {
setImmediate(() => this.connection?.disconnect());
await once(this, "disconnect", { signal });
await Pipeline.once(this, "disconnect", { signal });
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/synthesis/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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, {
Expand Down

0 comments on commit 6a3bb6a

Please sign in to comment.