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

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

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

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

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Q&A

解決済

2回答

2998閲覧

discord.jsでコマンドを送信した際に指定ユーザーにロールを付与したい

tubuan

総合スコア84

Discord

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

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

0グッド

0クリップ

投稿2021/03/22 15:56

前提・実現したいこと

discord.jsで、^levelup,<ユーザーID>,<レベル>(例:^levelup,723052392911863858,3)と送信し、指定したレベルに達していたらロールを付与するbotを作っています。

発生している問題・エラーメッセージ

(node:837) UnhandledPromiseRejectionWarning: ReferenceError: guild is not defined (node:837) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:837) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

該当のソースコード

js

1const discord = require("discord.js"); 2const client = new discord.Client(); 3 4client.on("ready", message => { 5 console.log("ready!"); 6}); 7 8client.on("message", async message => { 9 if (message.content.startsWith('^levelup')) { 10 const lvupdata = message.content.split(','); 11 message.channel.send(`<@!${lvupdata[1]}>さんが**Lv.${lvupdata[2]}**になりました\nおめでとうございます!`); 12 if(lvupdata[2] === '3') { 13 var lv3mem = guild.members.fetch(lvupdata[1]); 14 lv3mem.roles.add('823579630782906419'); 15 message.channel.send("Lv.3に到達したので、ロールを付与しました!"); 16 } 17 } 18}); 19 20client.login(process.env.TOKEN);

試したこと

色々コードを変えて試してみましたが、動きませんでした。

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

  • node.js v15.12.0
  • discord.js v12.5.1

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

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

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

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

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

guest

回答2

0

ベストアンサー

このコードの13行目にある var lv3mem = guild.members.fetch(lvupdata[1]);guildが定義されてないためにエラーが発生しております。
k239iさんの回答と被りますが、メッセージが送信されたguildにアクセスできれば良いと思いますので、message.guild にすると メンバーを取得できます。

そして、fetchは この場合 Promise<GuildMember> を返すため、awaitを用いてPromiseを解決する必要があります。

javascript

1if(lvupdata[2] === '3') { 2 var lv3mem = await message.guild.members.fetch(lvupdata[1]); 3 //guild.membersを await message.guild.membersに 4 lv3mem.roles.add('823579630782906419'); 5 message.channel.send("Lv.3に到達したので、ロールを付与しました!"); 6}

Promiseは理解しようとすると難しいため、Promiseを返すような場合にはasync/awaitを使うということを覚えておくとよいかと思います。

投稿2021/03/31 22:46

yutarou12

総合スコア155

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

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

tubuan

2021/04/01 13:31

回答ありがとうございます! 正常に動きました。 これからはpromiseに気をつけます。
guest

0

15行目のvar lv3mem = guild.members.fetch(lvupdata[1]);guildが定義されていません。
恐らくですがあなたはメッセージが送信されたギルドにアクセスしたいと思うのでmessage.guildを使用してください。

投稿2021/03/22 22:56

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

tubuan

2021/03/23 03:48

なるほど、`message.`が必要だったのですね。 早速追加して動かしてみたところ、 ``` (node:194) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'add' of undefined (node:194) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:194) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. ``` というエラーが出ました。 addがunderfinedと出ています。 ドキュメント https://discord.js.org/#/docs/main/stable/class/GuildMemberRoleManager?scrollTo=add を読んでも間違っているところが見つからないのですが、どうすれば動きますか?
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問