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

如何修复Discord机器人移动用户时的CommandInvokeError与AttributeError

解决Discord机器人移动用户时的AttributeError问题

错误原因

你代码中直接将整数类型的频道ID传给了member.move_to()方法,但该方法要求传入的是**discord.VoiceChannel类型的频道对象**,而非纯数字ID。当方法尝试访问这个整数的id属性时,自然会抛出AttributeError: 'int' object has no attribute 'id'错误。

修复方案

需要先通过频道ID获取对应的频道对象,再将对象传入move_to()方法。可以使用client.get_channel()方法来完成这一步,同时建议增加频道存在性判断,避免因ID无效或权限问题导致的后续报错。

修正后的代码

@client.command(pass_context=True)
async def asw(ctx, member: discord.Member):
    # 定义目标频道ID
    target_channel_id = 1062106353230950430
    # 通过ID获取频道对象
    target_channel = client.get_channel(target_channel_id)
    
    if target_channel:
        # 频道存在则移动用户
        await member.move_to(channel=target_channel)
    else:
        # 频道不存在时发送提示
        await ctx.send("目标频道不存在,或机器人无权限访问该频道")

额外说明

  • 确保机器人拥有移动成员的权限,以及目标语音频道的查看和连接权限,否则可能会触发权限相关错误。
  • 如果使用的是Discord.py v2.x版本,pass_context=True可以省略,因为该版本默认启用上下文传递。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 10:10:49