获取用户语音通道 ID



我有以下功能:

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。谢谢!

使用命令扩展和上下文:

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")

最新更新