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

使用discord.py调用create_guild报错:name 'create_guild' is not defined

问题原因与解决方法

错误原因

你遇到的NameError是因为直接调用了create_guild这个未定义的名称。在discord.py中,创建服务器的方法是discord.Client类的实例方法,必须通过你初始化的client对象来调用,不能直接单独使用函数名。

解决方法

修改on_ready事件中的调用代码,通过client实例来调用create_guild方法,完整代码如下:

import discord

client = discord.Client(intents=discord.Intents.default())

@client.event 
async def on_ready():
    print(f"已登录为 {client.user}")
    # 通过client实例调用创建服务器的方法
    guild = await client.create_guild(name="Bot World")
    print(f"成功创建服务器:{guild.name}")

client.run("你的Bot令牌")

额外注意事项

  • 普通Discord账号最多只能创建10个服务器,确保你的Bot账号未达到此上限
  • 绝对不要公开你的Bot令牌,泄露后他人可完全控制你的Bot
  • 如果后续出现权限报错,检查你的Bot账号是否被限制了创建服务器的权限

内容的提问来源于stack exchange,提问作者Lv 97 Retard

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 23:20:27