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

Discord.py生成服务器邀请链接时出现InviteTarget未定义错误

问题

尝试为机器人已加入的服务器生成邀请链接,代码中未直接使用InviteTarget,但运行时触发NameError: name 'InviteTarget' is not defined错误。

代码

@commands.hybrid_command(name='makeinvite', with_app_command=True)
    @commands.has_permissions(administrator=True)
    async def makeinvite(self, ctx, channel: discord.abc.GuildChannel = None):
        """
        Generates an invitation link for any server the bot is in.

        Parameters:
        channel: The ID of channel you want to generate an invitation for. If not provided, it will use the current channel where command was used.
        """
        if channel is None:
            invite = await ctx.channel.create_invite(max_uses=1,unique=True)
        else:
            invite = await channel.create_invite(max_uses=1, unique=True)

报错信息

Traceback (most recent call last):
  File "C:\Users\[redacted]\PycharmProjects\bot-ze-fourth\venv\lib\site-packages\discord\app_commands\commands.py", line 827, in _do_call
    return await self._callback(self.binding, interaction, **params)  # type: ignore
  File "C:\Users\[redacted]\PycharmProjects\bot-ze-fourth\cogs\admin.py", line 162, in makeinvite
    invite = await ctx.channel.create_invite(max_uses=1, unique=True)
  File "C:\Users\[redacted]\PycharmProjects\bot-ze-fourth\venv\lib\site-packages\discord\abc.py", line 1249, in create_invite
    if target_type is InviteTarget.unknown:
NameError: name 'InviteTarget' is not defined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\[redacted]\PycharmProjects\bot-ze-fourth\venv\lib\site-packages\discord\ext\commands\hybrid.py", line 438, in _invoke_with_namespace
    value = await self._do_call(ctx, ctx.kwargs)  # type: ignore
  File "C:\Users\[redacted]\PycharmProjects\bot-ze-fourth\venv\lib\site-packages\discord\app_commands\commands.py", line 846, in _do_call
    raise CommandInvokeError(self, e) from e
discord.app_commands.errors.CommandInvokeError: Command 'makeinvite' raised an exception: NameError: name 'InviteTarget' is not defined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\[redacted]\PycharmProjects\bot-ze-fourth\venv\lib\site-packages\discord\ext\commands\hybrid.py", line 438, in _invoke_with_namespace
    value = await self._do_call(ctx, ctx.kwargs)  # type: ignore
  File "C:\Users\[redacted]\PycharmProjects\bot-ze-fourth\venv\lib\site-packages\discord\app_commands\commands.py", line 846, in _do_call
    raise CommandInvokeError(self, e) from e
discord.ext.commands.errors.HybridCommandError: Hybrid command raised an error: Command 'makeinvite' raised an exception: NameError: name 'InviteTarget' is not defined
解决方案

这个错误是discord.py库内部的引用问题,并非你的代码导致。InviteTarget是discord.py内部用于标识邀请目标类型的枚举类,但你安装的库版本中,discord/abc.py文件未正确导入该类,或安装过程中文件损坏。

解决步骤:

  1. 卸载当前安装的discord.py:
pip uninstall discord.py
  1. 重新安装稳定版本的discord.py:
pip install discord.py

若问题依旧,可尝试指定具体稳定版本(比如2.3.2):

pip install discord.py==2.3.2

如果使用的是开发版discord.py,建议切换到正式稳定版,开发版可能存在此类未修复的bug。

内容的提问来源于stack exchange,提问作者Chris Ze Third

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 07:45:10