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

Discord.py斜杠命令中fetch_user用完整用户ID提示无效整数问题

Discord Unban斜杠命令ID输入报错问题

我在Discord服务器中创建了unban斜杠命令用于解封用户,命令本身可以正常运行,但粘贴完整的封禁用户ID时,系统会提示“Input a valid Integer”。虽然Python3不存在整数上限,但删除该ID前3位数字后,报错提示就消失了;仅删除2位时仍然会报错。我刚接触编程,还没尝试任何解决方法,不知道该从哪里入手。

相关代码如下:

bot.tree.command(name="unban")
async def unban(interaction: discord.Interaction, user_id: int=None, * , reason: str=None):
    if user_id is None:
        embed=discord.Embed(title="'user_id' missing!", description="Specify a user ID in order to unban that User.", color=0xff0000)
        await interaction.response.send_message(embed=embed)
    elif reason is None:
        embed=discord.Embed(title="'reason' missing!", description=f"Specify a for what you want to unban {user_id.name} for.", color=0xff0000)
        await interaction.response.send_message(embed=embed)
    else:
        try:
            user = await bot.fetch_user(user_id)
            ban_entry = await interaction.guild.fetch_ban(user)
            if ban_entry is None:
                embed = discord.Embed(title=f"{user.name} already is unbanned!", description="Please recheck if you have used the correct User ID.", color=0xff0000)
                await interaction.response.send_message(embed=embed)
            else:
                await interaction.guild.unban(user)
                embed=discord.Embed(title=f"Unbanned {user.name}", description=f"{user.name} with the ID: {user_id}, has been unbanned because {reason}" , color=0xff0000)
                await interaction.response.send_message(embed=embed)
        except discord.NotFound:
            embed=discord.Embed(title=f"{user_id} not found!", description="Please recheck if you have typed in the User ID correctly.", color=0xff0000)
            await interaction.response.send_message(embed=embed)

报错截图:

  • 粘贴完整ID时的报错:完整ID输入报错
  • 删除前3位ID后的状态:删除3位ID后正常
  • 删除2位ID时仍报错:删除2位ID仍报错

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 20:55:22