typescriptを使ってdiscord.jsで読み上げbotを作っているときにハマったポイントに書いていく。
発生したエラー
botをボイスチャンネルに接続させる処理で、互換性がないとコンパイル時にエラーを吐かれた。
パッケージのバージョンは
- discord.js : 13.1.0
- discordjs/voice : 0.6.0
であり、該当コードは以下の通り
join.ts
joinVoiceChannel({
channelId: VC.id,
guildId: VC.guildId,
adapterCreator: VC.guild.voiceAdapterCreator
})
このとき以下のようなエラーが発生した。
TSError: ⨯ Unable to compile TypeScript:
src/commands/join.ts(27,9): error TS2322: Type 'InternalDiscordGatewayAdapterCreator' is not assign
Types of parameters 'methods' and 'methods' are incompatible.
Type 'DiscordGatewayAdapterLibraryMethods' is not assignable to type 'InternalDiscordGatewayAda
Types of property 'onVoiceStateUpdate' are incompatible.
Type '(data: import("/app/node_modules/discord-api-types/payloads/v9/voice").GatewayVoiceSt
Types of parameters 'data' and 'data' are incompatible.
Type 'import("/app/node_modules/discord.js/node_modules/discord-api-types/payloads/v9/v
Types of property 'member' are incompatible.
Type 'import("/app/node_modules/discord.js/node_modules/discord-api-types/payloads/
Type 'import("/app/node_modules/discord.js/node_modules/discord-api-types/payload
Types of property 'user' are incompatible.
Type 'import("/app/node_modules/discord.js/node_modules/discord-api-types/pay
Type 'import("/app/node_modules/discord.js/node_modules/discord-api-types/p
Types of property 'flags' are incompatible.
Type 'import("/app/node_modules/discord.js/node_modules/discord-api-typ
Type 'UserFlags.None' is not assignable to type 'UserFlags | undefine
at createTSError (/app/node_modules/ts-node/src/index.ts:692:12)
at reportTSError (/app/node_modules/ts-node/src/index.ts:696:19)
at getOutput (/app/node_modules/ts-node/src/index.ts:883:36)
at Object.compile (/app/node_modules/ts-node/src/index.ts:1185:30)
at Module.m._compile (/app/node_modules/ts-node/src/index.ts:1309:30)
at Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Object.require.extensions.<computed> [as .ts] (/app/node_modules/ts-node/src/index.ts:1313:1
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19) {
diagnosticText: "src/commands/join.ts(27,9): error TS2322: Type 'InternalDiscordGatewayAdapterCre
" Types of parameters 'methods' and 'methods' are incompatible.\n" +
" Type 'DiscordGatewayAdapterLibraryMethods' is not assignable to type 'InternalDiscordGatew
" Types of property 'onVoiceStateUpdate' are incompatible.\n" +
` Type '(data: import("/app/node_modules/discord-api-types/payloads/v9/voice").GatewayVo
" Types of parameters 'data' and 'data' are incompatible.\n" +
` Type 'import("/app/node_modules/discord.js/node_modules/discord-api-types/payloads
" Types of property 'member' are incompatible.\n" +
` Type 'import("/app/node_modules/discord.js/node_modules/discord-api-types/payl
` Type 'import("/app/node_modules/discord.js/node_modules/discord-api-types/pa
" Types of property 'user' are incompatible.\n" +
` Type 'import("/app/node_modules/discord.js/node_modules/discord-api-type
` Type 'import("/app/node_modules/discord.js/node_modules/discord-api-ty
" Types of property 'flags' are incompatible.\n" +
` Type 'import("/app/node_modules/discord.js/node_modules/discord-ap
" Type 'UserFlags.None' is not assignable to type 'UserFlags | und
diagnosticCodes: [ 2322 ]
}
ちなみにdiscord.js/voiceの公式sample1でもほぼ同じコードが使われている。
voice/blob/main/examples/music-bot/src/bot.ts
...
joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
}),
...
このコードで一時は正常に動いていたのだが、dockerイメージを作り直すとエラーを吐かれてしまった。
解決法
adapterCreaterの型はDiscordGatewayAdapterCreatorなので、voiceAdapterCreatorの戻り値をInternalDiscordGatewayAdapterCreatorからDiscordGatewayAdapterCreatorにダウンキャストしてやればいい。下に修正後のコードを提示する。
join.ts
voice.joinVoiceChannel({
channelId: VC.id,
guildId: VC.guildId,
adapterCreator: VC.guild.voiceAdapterCreator as voice.DiscordGatewayAdapterCreator
})
ちなみに
githubでこの問題についてissue報告されており、v0.5.6で修正されているはずらしい。実際、最初は修正前のコードでエラーを吐かずに動作していた。