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

时间:2021-04-21 14:01:04

标签: python discord discord.py

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

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

我有代码,但 channelID 行不起作用。

# 2 "c.c" 2
# 1 "swap.h" 1
# 3 "c.c" 2

1 个答案:

答案 0 :(得分:0)

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

看看下面的代码:

@bot.command()
async def voice(ctx, member: discord.Member):
    voice_state = None if not member.voice else member.voice.channel
    if voice_state:
        await ctx.send(f"Name: {voice_state} / ID: {voice_state.id}")
    else:
        await ctx.send("Not in a voice channel.")

我们做了什么?

  • 内置于 member : discord.Member 作为需要的参数
  • voice_state 定义为我们正在寻找的“渠道”
  • voice_state 检查成员是否在语音频道中
  • 内置 if/else 语句以处理 None 或实际通道

如果您想以简单的方式使用它:

await ctx.send(f"{member.voice.channel}") # Gives out the name
await ctx.send(f"{member.voice.channel.id}") # Gives out the ID

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