如何从通道ID中获取通道对象



基本上就是它在标题中所说的。我想要获得一个通道对象,并且我有通道ID,其形式既有数字,也有<#以及>。如何从具有discord.py的通道对象中获取通道对象?

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

this_channel = ctx.channel.id

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

如果您想提及或获取通道对象的名称或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}
""")

最新更新