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

Disnake开发Discord机器人遇TypeError:'property'对象不可迭代问题求助

问题分析与解决方案

错误原因拆解

  1. 第一个错误:TypeError: 'property' object is not iterable
    你调用disnake.utils.get(disnake.Guild.text_channels, ...)时,disnake.Guild.text_channels是类的静态属性,并非具体服务器的可迭代频道列表。必须使用当前成员所属服务器实例的频道集合,比如member.guild.text_channels。此外,你克隆的是语音频道,却在搜索文本频道集合,逻辑本身就不匹配。

  2. 第二个错误:TypeError: get_channel() got an unexpected keyword argument 'name'
    bot.get_channel()仅支持传入频道ID作为参数,不支持通过name关键字搜索频道,用它按名称找频道完全不符合方法设计。

额外优化点

  • 原判断str(after) != str(before)逻辑冗余,直接用before.channel != after.channel就能准确判断用户是否切换了频道。
  • 克隆频道后无需再搜索,直接使用克隆返回的频道对象即可,既高效又避免名称冲突问题。

修正后的完整代码

bot = commands.InteractionBot()

@bot.event
async def on_ready():
    print("Индик's ready")
    guild = bot.get_guild(你的服务器ID)  # 替换为实际服务器ID
    print(guild)

@bot.event
async def on_voice_state_update(member, before, after):
    # 检查用户是否刚加入目标语音频道
    if after.channel and after.channel.name == 'Join to create channel' and before.channel != after.channel:
        # 克隆语音频道并命名为用户专属频道
        new_channel = await after.channel.clone(name=f"{member}'s channel")
        # 将用户移入新创建的频道
        await member.move_to(new_channel)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 22:40:16