Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Discord机器人无法离开语音频道';非类型';对象没有属性';断开';_Python_Python 3.x_Discord_Discord.py - Fatal编程技术网

Python Discord机器人无法离开语音频道';非类型';对象没有属性';断开';

Python Discord机器人无法离开语音频道';非类型';对象没有属性';断开';,python,python-3.x,discord,discord.py,Python,Python 3.x,Discord,Discord.py,所以我开始做一个音乐机器人,当我想离开的时候,我一直都会得到这个。加入语音频道很好 discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'disconnect' 我的代码是 @client.command(name = "join", pass_context

所以我开始做一个音乐机器人,当我想离开的时候,我一直都会得到这个。加入语音频道很好

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'disconnect'
我的代码是

@client.command(name = "join",
                pass_context = True)
async def join(ctx):
    channel = ctx.message.author.voice.voice_channel
    await client.join_voice_channel(channel)
    print("Bot joined the voice channel")

@client.command(name = "leave",
                pass_context = True)
async def leave(ctx):
    server = ctx.message.server
    voice_client = client.voice_client_in(server)
    await voice_client.disconnect()
    print("Bot left the voice channel")
此外,我还使用以下库:

import discord
from discord.ext.commands import Bot
from discord.ext import commands
from discord import Game
import random

如果该服务器中没有
VoiceClient
,则将返回
None
。在尝试断开连接之前,应检查是否存在此问题

@client.command(name = "leave",
                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()
        print("Bot left the voice channel")
    else:
        print("Bot was not in channel")

由@PatrickHaugh输入的固定版本为:

进口:

import discord
from discord.ext.commands import Bot
from discord.ext import commands
from discord import Game
from ctypes.util import find_library
import random
from discord import opus
import nacl
import asyncio
代码:


嗨,帕特里克,机器人肯定和我一起在频道里,据我所知,“等待客户。加入语音频道(频道)”将以语音客户的首字母缩写。但是用你的代码,我只是不断得到机器人不在频道,而它肯定是is@Dezert你从中得到了什么?它仍然表明机器人不在语音频道中。根据建议,我编写了
@client.command(name=“check”,pass\u context=True)异步定义检查(ctx):server=ctx.message.server如果client.is\u voice\u已连接(server):打印(“是”)否则:打印(“否”)
我继续发现机器人不在通道中。您是否从您的
join
命令中看到任何错误消息或类似消息?您是一位冠军帕特里克!这就解决了它,只需使用
find_library
,因为它由于某种原因无法自己找到它
@client.command(name = "join",
                pass_context = True)
async def join(ctx):
    opus_path = find_library('opus')
    discord.opus.load_opus(opus_path)
    if not opus.is_loaded():
        print('Opus was not loaded')
    else:
        channel = ctx.message.author.voice.voice_channel
        await client.join_voice_channel(channel)
        print("Bot joined the voice channel")

@client.command(name = "check",
                 pass_context = True)
async def check(ctx):
    server = ctx.message.server
    if client.is_voice_connected(server):
        print("Yes")
    else:
        print("No")


@client.command(pass_context = True)
async def leave(ctx):
    for x in client.voice_clients:
        if(x.server == ctx.message.server):
            return await x.disconnect()

    return await client.say("I am not connected to any voice channel on this server!")