“Nonetype” 对象没有名为 “leave” 的属性 nextcord。

huangapple go评论65阅读模式
英文:

Nonetype object has no a attribute leave nextcord

问题

错误:

`await server.leave("server_id")`
          ^^^^^^^^^^^^
AttributeError: 'NoneType' 对象没有属性 'leave'

能有人帮我找到解决这个错误的方法吗?

我尝试在加入服务器时获取服务器ID。 将其写入文本文件,然后离开该服务器。 我不知道如何离开服务器。 在阅读文档后,我完全不知所措。
英文:

CODE:

@client.event
async def on_guild_join(guild):
    with open('Servers.txt', "w", encoding="utf-8")as f:
        server_id = guild.id
        str_id = str(server_id)
        f.write("\n " + str_id)
    server = client.get_guild("server_id")
    await server.leave()

Error:

await server.leave("server_id")
^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'leave'

Can someone help me find solution for this error?

I'm trying to get server ID upon joining. Write it into text file and then leave the server. I don't know how to leave the server. Upon reading documentation I'm completly clueless.

答案1

得分: 1

提供的公会对象已经具有 id 属性。
使用公会对象提供的 id,可以节省 API 调用(或很可能是内部缓存查找)。

修复版本

@client.event
async def on_guild_join(guild):
    # 根据您的意图,您可能想要将其附加到现有的 "Servers.txt"。
    # 如果是这样,请在打开文件时使用 "a" 而不是 "w"。
    with open('Servers.txt', 'a', encoding='utf-8') as f:
        f.write("\n " + str(guild.id))
    await guild.leave()
英文:

The guild object provided already has an attribute for the id.
Using the provided id from the guild object, you can save an API call (or most likely an internal cache lookup).

Fixed version

@client.event
async def on_guild_join(guild):
    #  Depending on your intention you might have meant to append to the existing "Servers.txt". 
    #  If so use "a" instead of "w" when you are opening the file.
    with open('Servers.txt', "w", encoding="utf-8")as f:
        f.write("\n " + str(guild.id))
    await guild.leave()

答案2

得分: 0

你正在向get_guild函数提供一个string。你应该去掉引号。

英文:

You are supplying a string to the get_guild function. You should remove the quotes.

答案3

得分: 0

get_guild 似乎是问题所在,但在这段代码中完全不需要,因为你已经在 guild 变量中拥有了服务器对象。尝试简单地将 server.leave() 更改为 guild.leave()

英文:

get_guild seems to be the problem, but it is completely unnecessary in this code as you already have the server object in the guild variable. Try simply changing server.leave() to guild.leave()

huangapple
  • 本文由 发表于 2023年6月19日 20:06:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定