You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Discord机器人获取所有服务器中所有文本频道的ID?

获取Discord机器人所有服务器文本频道ID的正确实现

你的代码存在几处错误,以下是修正后的实现方案:

错误点说明

  • server.channels是属性而非方法,不能通过括号调用
  • 频道类型的正确写法是discord.TextChannel(或discord.ChannelType.text),原代码拼写错误且未正确引用
  • 必须开启Discord机器人的Intents权限,否则无法获取服务器及频道数据

正确代码示例

import discord
from discord.ext import commands

# 初始化机器人,启用所有意图(生产环境可按需调整)
bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())

@bot.event
async def on_ready():
    print(f"已登录为 {bot.user}")
    # 遍历机器人加入的所有服务器
    for guild in bot.guilds:
        print(f"\n服务器: {guild.name} (ID: {guild.id})")
        # 直接获取当前服务器的所有文本频道
        for channel in guild.text_channels:
            print(f"文本频道: {channel.name} → ID: {channel.id}")

# 替换为你的机器人令牌
bot.run("YOUR_BOT_TOKEN")

替代筛选方式(按需使用)

如果不想直接用guild.text_channels,也可以手动筛选频道类型:

for channel in guild.channels:
    if isinstance(channel, discord.TextChannel):
        print(channel.id)

内容的提问来源于stack exchange,提问作者MoonGoose

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 11:54:15