回答編集履歴

3

追記2

2022/10/21 14:26

投稿

退会済みユーザー
test CHANGED
@@ -73,3 +73,27 @@
73
73
 
74
74
  client.run('discordのトークン')
75
75
  ```
76
+
77
+ # 追記2
78
+
79
+ 下記のエラーから、discord botの権限設定に問題があると考えられます。
80
+ > line 672, in connect raise PrivilegedIntentsRequired(exc.shard_id) from None discord.errors.PrivilegedIntentsRequired: Shard ID None is requesting privileged intents that have not been explicitly enabled in the developer portal. It is recommended to go to https://discord.com/developers/applications/ and explicitly enable the privileged intents within your application's page.
81
+
82
+ > 訳:「672行目で、接続中 discord.errors.PrivilegedIntentsRequired 例外が None discord.errors.PrivilegedIntentsRequired から発生しています。Shard ID None は、開発者ポータルで明示的に有効化されていない特権的なインテントを要求しています。https://discord.com/developers/applications/ にアクセスし、アプリケーションのページ内で特権的なインテントを明示的に有効にすることが推奨されます」
83
+
84
+
85
+ このエラーを解消するには、BOT に intent の読み取り権限を付与する必要があると考えられます。
86
+ 下記の手順をお試しください。
87
+
88
+ <具体的手順>
89
+ ① https://discord.com/developers/applications/ にアクセスします。
90
+ ② ログインID、パスワードを求められた場合は、開発者のログインID、パスワードを入力します。
91
+ ③ Applications で、権限付与したいアプリのアイコンをクリックします
92
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-10-21/02bf2d4a-2f86-4e84-a886-8e77f16ca2cc.png)
93
+ ④ 左のペインの「Bot」をクリックし、右側を下の方までスクロールして
94
+ MESSAGE CONTENT INTENT の右下にあるスイッチをONにします。
95
+ ![イメージ説明](https://ddjkaamml8q8x.cloudfront.net/questions/2022-10-21/1b7dd349-3ddc-411a-8775-7ca3fcfcf4d1.jpeg)
96
+
97
+ この手順を行った後、再度、上記の「追記」に書いたコードで、コマンドプロンプトから実行してみて下さい。
98
+
99
+

2

追記

2022/10/20 10:13

投稿

退会済みユーザー
test CHANGED
@@ -9,6 +9,7 @@
9
9
  # 対策
10
10
  すでに実行中のJupyter Notebookのイベントループを取得し、そのイベントループにクライアントを開始するコルーチンを投げるようにしてください。
11
11
 
12
+ Jupyter Notebookで動かす場合
12
13
  ```python
13
14
  import discord
14
15
  import asyncio
@@ -45,3 +46,30 @@
45
46
  参考:https://stackoverflow.com/questions/69767952/how-to-run-discord-py-interactively-in-jupyter-notebook
46
47
 
47
48
  ※python 3.8.6、discord 2.0.1、 vscode内にインストールしたipykernel7.70にて確認。
49
+
50
+ ---
51
+
52
+ # 追記
53
+ Jupyter Notebookではない、通常のローカル環境に普通にインストールした Python 環境下で動かす場合:
54
+ ```
55
+ import discord
56
+
57
+ intents = discord.Intents.default()
58
+ intents.message_content = True
59
+
60
+ client = discord.Client(intents=intents)
61
+
62
+ @client.event
63
+ async def on_ready():
64
+ print(f'We have logged in as {client.user}')
65
+
66
+ @client.event
67
+ async def on_message(message):
68
+ if message.author == client.user:
69
+ return
70
+
71
+ if message.content.startswith('$hello'):
72
+ await message.channel.send('Hello!')
73
+
74
+ client.run('discordのトークン')
75
+ ```

1

 

2022/10/19 22:51

投稿

退会済みユーザー
test CHANGED
@@ -1,12 +1,9 @@
1
- # 他サイトをコピペしてもうまく動かないそもそもの原因
1
+
2
2
  > jupyternotebook を使用
3
3
 
4
- 貴方が見た他サイトは Jupyter Notebook を使ってdiscord を動かそうなんてしてないです。
5
4
  python を通常通りローカルにインストールした環境等であれば問題なく動きます。
6
-
7
- # なぜダメなのか
8
- 「RuntimeError: asyncio.run() cannot be called from a running event loop」
5
+ Jupyter Notebook で実行したときに「RuntimeError: asyncio.run() cannot be called from a running event loop」のエラーが起こるのは
9
- Jupyter Notebook内でイベントループが走っている状態で、新たに discord ライブラリ内でイベントループを発生させようとするため、そのようなエラーになります。
6
+ Jupyter Notebook内でイベントループが走っている状態で、新たに discord ライブラリ内でイベントループを発生させようとするためす。
10
7
 
11
8
 
12
9
  # 対策
@@ -47,4 +44,4 @@
47
44
 
48
45
  参考:https://stackoverflow.com/questions/69767952/how-to-run-discord-py-interactively-in-jupyter-notebook
49
46
 
50
- ※python 3.8.6、discord 2.0.1 で検証
47
+ ※python 3.8.6、discord 2.0.1 vscode内にインストールしたipykernel7.70にて確認。