断开连接命令在Discord bot中不起作用,运行时出错

时间:2019-05-06 19:23:41

标签: python discord discord.py disconnect

因此,我试图编写一个不和谐的bot,但是每当我尝试断开它的连接时,就会出现以下错误:

Ignoring exception in command leave
Traceback (most recent call last):
  File "D:\Python36\lib\site-packages\discord\ext\commands\core.py", line 50, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "D:\new\main.py", line 31, in leave
    await voice_client.disconnect()
  File "D:\Python36\lib\site-packages\discord\voice_client.py", line 297, in disconnect
    yield from self.ws.close()
  File "D:\Python36\lib\site-packages\websockets\protocol.py", line 419, in close
    yield from asyncio.shield(self.close_connection_task)
  File "D:\Python36\lib\site-packages\discord\gateway.py", line 686, in close_connection
    yield from super().close_connection(force=force)
TypeError: close_connection() got an unexpected keyword argument 'force'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "D:\Python36\lib\site-packages\discord\ext\commands\bot.py", line 846, in process_commands
    yield from command.invoke(ctx)
  File "D:\Python36\lib\site-packages\discord\ext\commands\core.py", line 374, in invoke
    yield from injected(*ctx.args, **ctx.kwargs)
  File "D:\Python36\lib\site-packages\discord\ext\commands\core.py", line 54, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: close_connection() got an unexpected keyword argument 'force'

我尝试过改变断开机器人连接的方式,但是似乎并不想通过以下代码进行操作:

等待voice_client.disconnect()

这是不起作用的代码。

import discord
from discord.ext import commands

BOT_PREFIX = ('!')
client = commands.Bot(command_prefix=BOT_PREFIX)

@client.command(pass_context = True)
async def leave(ctx):
    server = ctx.message.server
    voice_client = client.voice_client_in(server)
    if voice_client:
        await voice_client.disconnect()
        await client.say('Disconnected')
    else:
        await client.say('I am not in a voice channel')

1 个答案:

答案 0 :(得分:0)

我建议您更改代码,例如:

voice_client = client.voice_client_in(server)
print(voice_client)
print(dir(voice_client)) # show available methods/attributes of the object

为了再次检查voice_client对象是否符合您的期望。从MDN中看不到.voice_client_in()。但是,有一个documentation返回一个布尔值。尽管更符合您的代码的是VoiceClient.is_connected() method,它返回所有已连接的语音聊天的列表。因此,在您的情况下,该返回值如下所示:

if list_of_chats_connected:
    await list_of_chats_connected.disconnect()

这没有意义,因为需要对列表进行索引,以便能够像disconnect()那样调用其内部对象的方法