获取用户语音通道ID

时间:2017-05-22 14:41:38

标签: python discord.py

我有以下功能:

async def play_youtube_url(youtube_url):
channel = client.get_channel('VOICE_CHANNEL_ID')
if youtube_url.startswith('https://www.youtube.com/watch?v='):
    voice = await client.join_voice_channel(channel)
    player = await voice.create_ytdl_player(youtube_url)
    player.start()
else:
    return 'URL_ERROR'

我的问题是,如何获取输入命令的用户的语音通道ID。我知道如何获取服务器ID,但我找不到如何获取文档中的语音通道ID。谢谢!

1 个答案:

答案 0 :(得分:1)

使用命令扩展和上下文:

import discord
from discord.ext import commands

...

client = commands.Bot(command_prefix="!")
@client.command(pass_context=True)
async def play_youtube_url(self, ctx, youtube_url):
    channel = ctx.message.author.voice.voice_channel 
    # http://discordpy.readthedocs.io/en/latest/api.html#discord.Member.voice
    # http://discordpy.readthedocs.io/en/latest/api.html#discord.VoiceState.voice_channel
    if youtube_url.startswith('https://www.youtube.com/watch?v='):
        voice = await client.join_voice_channel(channel)
        player = await voice.create_ytdl_player(youtube_url)
        player.start()
    else:
        return 'URL_ERROR'

...

client.run("token")