如何将Discord Bot连接到语音通道?

时间:2020-09-22 11:35:19

标签: python python-3.x discord.py

查看了有关将discord bot连接到语音通道以播放声音的所有问题,但找不到所需的答案。我是Discord库的新手,没有解决问题的工作原理,因此当我尝试在站点中给出答案时,我大多会收到如下错误:

AttributeError: 'NoneType' object has no attribute 'channel'

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

我的代码如下

import discord
from dotenv import load_dotenv
from discord.ext import commands

load_dotenv()

token = "XXXXXXXXXXXXXXXXX"
client = commands.Bot(command_prefix='.')
voice = discord.VoiceChannel

@client.command(name="join")
async def join(ctx):
    channel = ctx.author.voice.channel
    voice = get(self.bot.voice_clients, guild=ctx.guild)

    if voice and voice.is_connected():
        await voice.move_to(channel)
    else:
        voice = await channel.connect()

client.run(Token)

这只是其他发送或事件部分正在工作的代码的语音部分。

2 个答案:

答案 0 :(得分:1)

这是我用的,我尽可能地评论了

@ client.command(name='join',aliases = ['summon']) # CREATING COMMAND "JOIN" WITH ALIAS SUMMON
async def _join(ctx, *, channel: discord.VoiceChannel = None): # TAKING ARGUMENT CHANNEL SO PPL CAN MAKE THE BOT JOIN A VOICE CHANNEL THAT THEY ARE NOT IN
    """Joins a voice channel."""

    destination = channel if channel else ctx.author.voice.channel # CHOOSING THE DESTINATION, MIGHT BE THE REQUESTED ONE, BUT IF NOT THEN WE PICK AUTHORS VOICE CHANNEL

    if ctx.voice_client: # CHECKING IF THE BOT IS PLAYING SOMETHING
        await ctx.voice_state.voice.move_to(destination) # IF THE BOT IS PLAYING WE JUST MOVE THE BOT TO THE DESTINATION
        return

    await destination.connect() # CONNECTING TO DESTINATION
    await ctx.send(f"Succesfully joined the voice channel: {destination.name} ({destination.id}).")

顺便说一句,BTW音乐和语音机器人真的很复杂,如果您是一个入门者,最好做一些简单的事情,例如审核命令和游戏。

答案 1 :(得分:1)

@client.command()
async def enter(ctx):
    connected = ctx.author.voice
    if not connected:
        await ctx.send("You need to be connected in a voice channel to use this command!")
        return
    global vc
    vc = await connected.channel.connect()

顺便说一句,不要忘记安装 discord,py[voice] 模块。我希望它有帮助:D