如何遍历 discord.py 中的通道?

How to iterate through channels in discord.py?

我正在制作一个 discord.py 重写的 discord 机器人,我想在一个特定的频道中发送消息,该频道不是命令将被使用的地方。如果命令在 X 上使用,消息应该在 Y 通道上发送。我想到的一个选择是遍历服务器的通道,但我无法做到。请帮助任何人。

您可以使用 discord.utils.get():

from discord.utils import get
from discord.ext import commands

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

@bot.command()
async def test(ctx):
    #Get your channel by name:
    channel = get(ctx.guild.text_channels, name="Channel Name")

    #Get your channel by ID:
    channel = get(ctx.guild.text_channels, id=channel_id)

    await channel.send("Some message")

One option in my mind was to iterate through the channels of the server but I am unable to do it.

什么不起作用?因为 guild.text_channels 是所有服务器文本通道的 列表 ,您应该能够正常地对其进行迭代。