如何从频道 ID 获取频道 object?

How do you get a channel object from a channel ID?

基本上是标题中所说的。我想获得一个频道 object,并且我有频道 ID,既有数字形式,也有 <# 和 >。我如何从 discord.py 中的任何一个获取频道 object?

如果您想提及或获取其名称或 ID,您不会在使用通道对象时包含太多其他内容:

@client.command()
async def channel(ctx):
    channel_name = ctx.channel.mention
    channel_id = ctx.channel.id
    
    await ctx.send("""
    Channel name: {channel_name}
    Channel ID: {channel_id}
    """)

您可以通过将频道 ID 存储在 this_channel 变量中,然后使用命令,获得可以在 on_message 或任务循环或发送相同消息的命令中使用的频道 ID,它可以检索该变量并从那里向频道发送任何您想要的消息。

    this_channel = ctx.channel.id

     
    channel = client.get_channel(int(this_channel))
    await channel.send('Heres the message')