简体   繁体   English

从用户 id discord.py 获取语音通道 id

[英]Get voice channel id from user id discord.py

My question is how can i get the voice channel id a user is in without that person typing in any chats, if i know the persons user ID.我的问题是,如果我知道用户的用户 ID,我如何才能获得用户所在的语音频道 ID,而无需该人在任何聊天中输入。

Example Code:示例代码:

USER_ID = 1234578654

@bot.command()
async def join():
    account = bot.get_user(USER_ID)
    channel = account.voice.channel
    voice = await channel.connect()

In steps在步骤

  1. Figure out which channel a user is in with there id找出用户所在的频道 ID
  2. join that channel加入那个频道

You can try and get the channel by the member argument.您可以尝试通过member参数获取频道。 You can now either use the name or ID of the user.您现在可以使用用户的名称或 ID。

Take a look at the following example:看看下面的例子:

@bot.command()
async def join(ctx, member : discord.Member):
    try:
        channel = member.voice.channel
        if channel: # If user is in a channel
            await channel.connect() # Connect
            await ctx.send("User is connected to a channel, joining...")
        else:
            await ctx.send("I am already connected to a channel.") # If the bot is already connected
    except AttributeError:
        return await ctx.send("User is not in a channel, can't connect.") # Error message

What did we do?我们做了什么?

  • Get the voice channel of discord.Member .获取discord.Member的语音通道。
  • Connect to the channel if the member is in a voice channel.如果成员在语音频道中,则连接到频道。
  • Give out an error if the member is not connected to a channel.如果member未连接到频道,则发出错误。

To define a user you can use the following function:要定义用户,您可以使用以下 function:

from discord.utils import get

@bot.command()
async def join(ctx):
    try:
        member = ctx.guild.get_member(YourID) # Get the member by ID
        if member:  # If it is the defined member
            await member.voice.channel.connect()  # Connect
            return await ctx.send("User is connected to a channel, joining...")
        else:
            await ctx.send("Not the defined user.")
    except AttributeError:
        return await ctx.send("The defined user is not in a channel.")  # Error message

For more information you can also look at the docs有关更多信息,您还可以查看文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM