Discord.pyでのCommandInvokeErrorについて

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 = True19client = discord.Client(intents=Intents)20cmdtree = app_commands.CommandTree(client)21vcclient = None22vcon = False23ajon = False24def_vcid = 025config = []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.event71async 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 return126 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 return133 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 return176client.run(os.environ['TOKEN'])

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

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

コメントを投稿

0 コメント