我有以下功能:
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。谢谢!
答案 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")