Discord Bot无法离开语音频道“NoneType”对象没有属性“disconnect”

2024-03-28 14:35:59 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我开始制作一个音乐机器人,当我试图离开的时候,我总是得到这个。加入语音频道效果很好。在

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'disconnect'

我的代码是

^{pr2}$

我还使用以下库:

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

Tags: fromimportan音乐机器人语音频道ext
2条回答

如果服务器中没有VoiceClient,那么{a1}将返回{}。在尝试断开连接之前,您应该检查一下。在

@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

代码:

^{pr2}$

相关问题 更多 >