質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Discord

Discordは、ゲーマー向けのボイスチャットアプリです。チャット・通話がブラウザ上で利用可能で、個人専用サーバーも開設できます。通話中でも音楽を流したり、PC画面を共有できるなど多機能な点が特徴です。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Q&A

解決済

1回答

1071閲覧

Discord.js v14の GatewayIntentBitsの定義になぜ{}を付けるのか

nenemineko3

総合スコア1

Discord

Discordは、ゲーマー向けのボイスチャットアプリです。チャット・通話がブラウザ上で利用可能で、個人専用サーバーも開設できます。通話中でも音楽を流したり、PC画面を共有できるなど多機能な点が特徴です。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

0グッド

0クリップ

投稿2023/01/21 11:00

前提

Discord.js(v14)でBotを作っています。
最初に

javascript

1const { GatewayIntentBits } = require("discord.js")

と書く際に、なぜGatewayIntentBitsを{}で囲わなければいけないのかが調べてもよくわかりませんでした。

{}で囲わずに実行したときに出たエラーメッセージ

javascript

1C:\DiscordBotProject\node_modules\discord.js\src\util\BitField.js:168 2 throw new DiscordjsRangeError(ErrorCodes.BitFieldInvalid, bit); 3 ^ 4 5RangeError [BitFieldInvalid]: Invalid bitfield flag or number: class BaseClient extends EventEmitter { 6 constructor(options = {}) { 7 super({ captureRejections: true }); 8 9 if (typeof options !== 'object' || options === null) { 10 throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'options', 'object', true); 11 } 12 13 /** 14 * The options the client was instantiated with 15 * @type {ClientOptions} 16 */ 17 this.options = mergeDefault(Options.createDefault(), options); 18 19 /** 20 * The REST manager of the client 21 * @type {REST} 22 */ 23 this.rest = new REST(this.options.rest); 24 } 25 26 /** 27 * Destroys all assets used by the base client. 28 * @returns {void} 29 */ 30 destroy() { 31 this.rest.requestManager.clearHashSweeper(); 32 this.rest.requestManager.clearHandlerSweeper(); 33 } 34 35 /** 36 * Increments max listeners by one, if they are not zero. 37 * @private 38 */ 39 incrementMaxListeners() { 40 const maxListeners = this.getMaxListeners(); 41 if (maxListeners !== 0) { 42 this.setMaxListeners(maxListeners + 1); 43 } 44 } 45 46 /** 47 * Decrements max listeners by one, if they are not zero. 48 * @private 49 */ 50 decrementMaxListeners() { 51 const maxListeners = this.getMaxListeners(); 52 if (maxListeners !== 0) { 53 this.setMaxListeners(maxListeners - 1); 54 } 55 } 56 57 toJSON(...props) { 58 return flatten(this, ...props); 59 } 60}. 61 at IntentsBitField.resolve (C:\DiscordBotProject\node_modules\discord.js\src\util\BitField.js:168:11) 62 at C:\DiscordBotProject\node_modules\discord.js\src\util\BitField.js:163:54 63 at Array.map (<anonymous>) 64 at new BitField (C:\DiscordBotProject\node_modules\discord.js\src\util\BitField.js:33:38) 65 at new IntentsBitField (C:\DiscordBotProject\node_modules\discord.js\src\util\IntentsBitField.js:9:1) 66 at Client._validateOptions (C:\DiscordBotProject\node_modules\discord.js\src\client\Client.js:491:25) 67 at new Client (C:\DiscordBotProject\node_modules\discord.js\src\client\Client.js:78:10) 68 at Object.<anonymous> (C:\DiscordBotProject\index.js:13:16) 69 at Module._compile (node:internal/modules/cjs/loader:1218:14) { 70 code: 'BitFieldInvalid' 71} 72 73Node.js v18.13.0

該当のソースコード

javascript

1const GatewayIntentBits = require("discord.js");

試したこと

検索

補足情報(FW/ツールのバージョンなど)

Node.js v18.13.0.
Discord.js v14.7.1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

なぜGatewayIntentBitsを{}で囲わなければいけないのか

js

1const { GatewayIntentBits } = require("discord.js")

これは 分割代入といって
require("discord.js")で読み込んだオブジェクトの中の、GatewayIntentBits を、変数 GatewayIntentBits に代入しています。

以下の処理と同等です:

js

1const GatewayIntentBits = require("discord.js").GatewayIntentBits;

仮に

js

1const GatewayIntentBits = require("discord.js")

としてしまうと、変数 GatewayIntentBits には discord.js が エクスポートしているオブジェクトがバインドされてしまうので、うまく動作しません。

なので最初のように

js

1const { GatewayIntentBits } = require("discord.js")

とするか、
あるいは(冗長ですが)

js

1const a = require("discord.js"); 2const GatewayIntentBits = a.GatewayIntentBits;

とします。

投稿2023/01/21 14:16

編集2023/01/21 14:18
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問