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

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

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

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

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

1回答

1038閲覧

Discord.pyでのCommandInvokeErrorについて

yoyuhihami

総合スコア26

Discord

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

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2023/04/30 03:05

実現したいこと

DiscordのBotがちゃんと動くようにしたい。

エラーメッセージ

[2023-04-30 09:43:36] [ERROR ] discord.voice_client: Disconnected from voice... Reconnecting in 1.63s. Traceback (most recent call last): File "C:\Users\User\anaconda3\lib\site-packages\discord\voice_client.py", line 456, in poll_voice_ws await self.ws.poll_event() File "C:\Users\User\anaconda3\lib\site-packages\discord\gateway.py", line 1006, in poll_event raise ConnectionClosed(self.ws, shard_id=None, code=self._close_code) discord.errors.ConnectionClosed: Shard ID None WebSocket closed with 1006 [2023-04-30 09:43:38] [INFO ] discord.voice_client: The voice handshake is being terminated for Channel ID ******************* (Guild ID *******************) [2023-04-30 09:43:38] [INFO ] discord.voice_client: Connecting to voice... [2023-04-30 09:43:38] [INFO ] discord.voice_client: Starting voice handshake... (connection attempt 2) [2023-04-30 09:43:38] [INFO ] discord.voice_client: Voice handshake complete. Endpoint found japan11048.discord.media[2023-04-30 09:44:25] [ERROR ] discord.voice_client: Disconnected from voice... Reconnecting in 0.93s. Traceback (most recent call last): File "C:\Users\User\anaconda3\lib\site-packages\discord\voice_client.py", line 456, in poll_voice_ws await self.ws.poll_event() File "C:\Users\User\anaconda3\lib\site-packages\discord\gateway.py", line 1006, in poll_event raise ConnectionClosed(self.ws, shard_id=None, code=self._close_code) discord.errors.ConnectionClosed: Shard ID None WebSocket closed with 1006 [2023-04-30 09:44:26] [INFO ] discord.voice_client: The voice handshake is being terminated for Channel ID ******************* (Guild ID *******************) [2023-04-30 09:44:26] [INFO ] discord.voice_client: Connecting to voice... [2023-04-30 09:44:26] [INFO ] discord.voice_client: Starting voice handshake... (connection attempt 3) [2023-04-30 09:44:27] [INFO ] discord.voice_client: Voice handshake complete. Endpoint found japan11049.discord.media[2023-04-30 09:44:51] [ERROR ] discord.voice_client: Failed to connect to voice... Retrying... Traceback (most recent call last): File "C:\Users\User\anaconda3\lib\site-packages\discord\voice_client.py", line 456, in poll_voice_ws await self.ws.poll_event() File "C:\Users\User\anaconda3\lib\site-packages\discord\gateway.py", line 1006, in poll_event raise ConnectionClosed(self.ws, shard_id=None, code=self._close_code) discord.errors.ConnectionClosed: Shard ID None WebSocket closed with 1006 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\User\anaconda3\lib\site-packages\discord\voice_client.py", line 395, in connect self.ws = await self.connect_websocket() File "C:\Users\User\anaconda3\lib\site-packages\discord\voice_client.py", line 366, in connect_websocket await ws.poll_event() File "C:\Users\User\anaconda3\lib\site-packages\discord\gateway.py", line 1006, in poll_event raise ConnectionClosed(self.ws, shard_id=None, code=self._close_code) discord.errors.ConnectionClosed: Shard ID None WebSocket closed with 4006

(DiscordのユーザIDやGuildIDは伏せてあります。)

コード

Python

1import discord 2from discord import app_commands 3import subprocess 4import win32gui 5import time 6import os 7import json 8import time 9import mcrcon 10from dotenv import load_dotenv 11load_dotenv() 12rcserver = { 13 'address' : os.environ['SERVER_ADDR'], 14 'port' : os.environ['SERVER_PORT'], 15 'server_pass': os.environ['SERVER_PASS'] 16} 17Intents = discord.Intents.all() 18Intents.members = True 19client = discord.Client(intents=Intents) 20cmdtree = app_commands.CommandTree(client) 21vcclient = None 22vcon = False 23ajon = False 24def_vcid = 0 25config = [] 26 27def load_settings(filepath): 28 try: 29 with open(filepath) as f: 30 settings = json.load(f) 31 except FileNotFoundError: 32 print(f"{filepath} is not found.") 33 settings = {} 34 return settings 35 36def rcon_cmd(command): 37 global rcserver 38 with mcrcon.MCRcon(rcserver['address'], rcserver['server_pass'], int(rcserver['port'])) as mcr: 39 log=mcr.command(command) 40 return log 41 42def write_json(filename): 43 global vcon 44 global ajon 45 global def_vcid 46 f = open(filename, "w") 47 f.write(str('{"vcon":'+'"'+vcon+'"'+',"ajon":'+'"'+ajon+'"'+',"def_vcid":'+'"'+str(def_vcid)+'"'+'}')) 48 f.close() 49 print('change setting(s) : ') 50 print(str('{"vcon":'+"'"+vcon+"'"+',"ajon":'+'"'+ajon+"'"+',"def_vcid":'+"'"+str(def_vcid)+"'"+'}')) 51 52 53async def init(): 54 global vcon 55 global ajon 56 global def_vcid 57 global config 58 global vcclient 59 config = load_settings('C:/minecraft_servers/script/settings.json') 60 vcon = config.get('vcon') 61 ajon = config.get('ajon') 62 def_vcid = int(config.get('def_vcid')) 63 if vcclient == None: 64 if ajon == 'true': 65 vcclient = await client.get_channel(def_vcid).connect() 66 else: 67 await vcclient.disconnect() 68 69 70@client.event 71async def on_ready(): 72 global vcclient 73 await init() 74 if ajon == 'true': 75 if vcclient.is_playing(): 76 vcclient.stop() 77 vcclient.play(discord.FFmpegPCMAudio("C:/minecraft_servers/script/python/sounds/yomiageon.mp3")) 78 channel = client.get_channel(*******************) 79 await channel.send(" :warning: WARNING : This is a test version(v1.2.0).") 80 print(str('config : ')) 81 print(config) 82 print('vcclient : ') 83 print(vcclient) 84 await cmdtree.sync() 85 86@cmdtree.command(name="vc-cfg", description="VCチャンネルでのサーバー起動通知に関する設定をします。") 87@discord.app_commands.choices( 88 run_cmd=[ 89 discord.app_commands.Choice(name="AutoJoin-On", value="ajon"), 90 discord.app_commands.Choice(name="AutoJoin-Off", value="ajoff"), 91 discord.app_commands.Choice(name="Join",value="join"), 92 discord.app_commands.Choice(name="Quit",value="quit"), 93 discord.app_commands.Choice(name="VC-On", value="vcon"), 94 discord.app_commands.Choice(name="VC-Off", value="vcoff"), 95 discord.app_commands.Choice(name="Reload", value="reload") 96 ] 97) 98async def vc_cfg(interaction, run_cmd:str, set_vcid:str = None): 99 global vcclient 100 global vcon 101 global ajon 102 global def_vcid 103 await interaction.response.defer() 104 await interaction.followup.send('executing command(s)...') 105 cnl = def_vcid 106 print('cnl : ') 107 print(cnl) 108 channel = client.get_channel(cnl) 109 print('channel : ') 110 print(channel) 111 if not set_vcid == None: 112 def_vcid = set_vcid 113 write_json('C:/minecraft_servers/script') 114 if run_cmd == 'reload': 115 await init() 116 print(vcclient) 117 print(vcon) 118 if vcon == 'true' and vcclient: 119 if vcclient.is_playing(): 120 vcclient.stop() 121 vcclient.play(discord.FFmpegPCMAudio("C:/minecraft_servers/script/python/sounds/reload.mp3")) 122 elif run_cmd == 'join': 123 if not channel.type == discord.ChannelType.voice: 124 await interaction.response.send_message('Error : Specified channel is invalid or not a voice channel') 125 return 126 else: 127 await interaction.followup.send(str('connect to <#' + str(cnl) + '>')) 128 vcclient = await channel.connect() 129 elif run_cmd == 'quit': 130 if vcclient == None: 131 await interaction.followup.send(str('Error: Unable to disconnect: Not connected to voice channel')) 132 return 133 else: 134 await vcclient.disconnect() 135 136 elif run_cmd == 'vcon': 137 write_json('C:/minecraft_servers/script/settings.json') 138 vcon = 'true' 139 if vcon == 'true' and vcclient: 140 if vcclient.is_playing(): 141 vcclient.stop() 142 vcclient.play(discord.FFmpegPCMAudio("C:/minecraft_servers/script/python/sounds/vcon.mp3")) 143 144 elif run_cmd == 'vcoff': 145 vcon = 'false' 146 ajon = 'false' 147 write_json('C:/minecraft_servers/script/settings.json') 148 if vcon == 'true' and vcclient: 149 if vcclient.is_playing(): 150 vcclient.stop() 151 vcclient.play(discord.FFmpegPCMAudio("C:/minecraft_servers/script/python/sounds/vcoff.mp3")) 152 await vcclient.disconnect() 153 154 elif run_cmd == 'ajon': 155 ajon = 'true' 156 if def_vcid == None: 157 interaction.followup.send('Error: def_vcid value is invalid. Please check if it is the correct value') 158 write_json('C:/minecraft_servers/script/settings.json') 159 if vcon == 'true' and vcclient: 160 if vcclient.is_playing(): 161 vcclient.stop() 162 vcclient.play(discord.FFmpegPCMAudio("C:/minecraft_servers/script/python/sounds/ajon.mp3")) 163 164 elif run_cmd == 'ajoff': 165 ajon = 'false' 166 write_json('C:/minecraft_servers/script/settings.json') 167 if vcon == 'true' and vcclient: 168 if vcclient.is_playing(): 169 vcclient.stop() 170 vcclient.play(discord.FFmpegPCMAudio("C:/minecraft_servers/script/python/sounds/ajoff.mp3")) 171 else: 172 await interaction.response.send_message(str('invaild run_cmd : ' + run_cmd)) 173 return 174 await interaction.followup.send(":white_check_mark: Operation completed successfully.") 175 return 176client.run(os.environ['TOKEN'])

(これもユーザIDなどは伏せてあります。)

環境

Windows 11 (更新などはすべて適用済み) Discord.py Python 3.10.6

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

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

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

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

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

yoyuhihami

2023/04/30 03:06

Pythonのコードが文字数の関係ですべてのせることができませんでした。 なので一応エラーが出ていると思われる個所(vc-cfgコマンドの箇所)とimport文、変数定義の箇所のみ載せました。
guest

回答1

0

エラーメッセージから判断すると fnaf あなたが使用しているDiscordのボイス接続に関するエラーが発生しているようです。特に、WebSocketの接続が閉じられたことを示す「WebSocket closed with 1006」というエラーコードが表示されています。

このエラーコードは、通信が異常終了したことを示しています。ボイス接続の問題を解決するために、いくつかのステップを試してみることができます。

Discord.pyのバージョンを確認してください。最新バージョンにアップデートすることで、既知の問題が修正される場合があります。

Discordの開発者ポータルで、ボットトークンや権限などが正しく設定されていることを確認してください。必要な権限が与えられていない場合、ボイス接続に問題が発生する可能性があります。

Discord.pyのIntentsを正しく設定していることを確認してください。例えば、以下のようにIntentsのvoice_statesとguildsをTrueに設定します。

python
Intents = discord.Intents.default()
Intents.voice_states = True
Intents.guilds = True
client = discord.Client(intents=Intents)
インターネット接続に問題がないことを確認してください。ネットワーク接続が不安定な場合、ボイス接続に問題が生じる可能性があります。

Discordのボイスサーバーが正常に稼働していることを確認してください。一時的な問題やメンテナンスによって、ボイス接続に影響を及ぼす場合があります。

これらの手順を試しても問題が解決しない場合は、コードの特定の部分に問題がある可能性があります。その場合は、該当の部分をより詳細に調査することが必要になります。

ただし、注意点として、私はコードの実行環境やネットワーク状況を把握することができませんので、提供した情報を参考にして解決策を見つける必要があります。問題が続く場合は、Discord.pyの公式ドキュメントやディスカッションフォーラムでサポートを受けることをおすすめし ます。

投稿2023/05/18 02:01

damdam23

総合スコア33

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問