Python 使用discord.py获取用户当前所在频道的ID

Python 使用discord.py获取用户当前所在频道的ID,python,discord,discord.py,Python,Discord,Discord.py,我正在努力获取用户当前处于discord状态的语音频道的ID(或名称)。 我希望能够将用户移动到特定的频道,但在一段时间间隔后将用户移回其所在的频道 所以基本上是一种“超时”方法 我有代码,但channelID行不行 from discord.ext import commands import discord import os import time bot = commands.Bot(command_prefix = '$') @bot.command(pass_context=Tr

我正在努力获取用户当前处于discord状态的语音频道的ID(或名称)。 我希望能够将用户移动到特定的频道,但在一段时间间隔后将用户移回其所在的频道

所以基本上是一种“超时”方法

我有代码,但channelID行不行

from discord.ext import commands
import discord
import os
import time

bot = commands.Bot(command_prefix = '$')

@bot.command(pass_context=True)
async def goout(ctx, member: discord.Member):

    channelID = member.voice_channel.id

    channel = discord.utils.get(ctx.guild.voice_channels, name='Go out')
    channel2 = discord.utils.get(ctx.guild.voice_channels, id=channelID)

    while True:
        await member.edit(voice_channel=channel)
        time.sleep(3)
        await member.edit(voice_channel=channel2)
        return False

您必须以不同的方式定义频道/可以以不同的方式请求频道

查看以下代码:

@bot.command()
异步def语音(ctx,成员:discord.member):
voice_state=如果不是成员,则无。voice else成员。voice.channel
如果语音状态为:
等待ctx.send(f“Name:{voice_state}/ID:{voice_state.ID}”)
其他:
等待ctx.send(“不在语音频道中”)
我们做了什么?

  • 内置
    成员:discord.member
    作为所需参数
  • voice\u state
    定义为我们正在寻找的“频道”
  • voice\u state
    检查成员是否在语音频道中
  • 内置
    if/else
    语句来处理
    None
    或实际通道
如果您希望以简单的方式使用它:

wait ctx.send(f“{member.voice.channel}”)#给出了名称
等待ctx.send(f“{member.voice.channel.id}”)#给出id

请注意,这可能只是回答您的问题,我没有用您的代码迁移它

member.voice.channel
成功了!但感谢您的全部投入,让它自己发挥作用并不是一个愚蠢的想法