【问题标题】:Python Discord.py | invite link on_ready issuePython Discord.py |邀请链接 on_ready 问题
【发布时间】:2019-03-01 09:01:11
【问题描述】:

使用我的代码,我尝试向它所在的每台服务器的控制台发送邀请链接,在 discord.py 的 API 中,它说你可以编写服务器或频道,但服务器似乎不适合我。

@client.event
async def on_ready():

    print(client.servers)
    for value in client.servers:
        invitelinknew = await client.create_invite(destination=value)
        print(invitelinknew)

我收到以下错误:

Ignoring exception in on_ready
Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\discord\client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "C:/Users/Rasmus/Python/discordbot/botnoggi2.py", line 126, in on_ready
    invitelinknew = await client.create_invite(destination=value)
  File "C:\Program Files\Python36\lib\site-packages\discord\client.py", line 2628, in create_invite
    data = yield from self.http.create_invite(destination.id, **options)
  File "C:\Program Files\Python36\lib\site-packages\discord\http.py", line 198, in request
    raise NotFound(r, data)
discord.errors.NotFound: NOT FOUND (status code: 404): Unknown Channel

我的问题是使用on_ready命令和for循环检查每个服务器,和When running bot sample code, I get this error不一样

编辑:

@client.event
async def on_ready():
    for server in client.servers:
        channel = next(iter(server.channels))
        invitelinknew = await client.create_invite(destination=channel)
        print(invitelinknew)

此代码返回:

Traceback (most recent call last):
  File "C:\Program Files\Python36\lib\site-packages\discord\client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "C:/Users/Rasmus/Python/discordbot/botnoggi2.py", line 126, in on_ready
    invitelinknew = await client.create_invite(destination=channel)
  File "C:\Program Files\Python36\lib\site-packages\discord\client.py", line 2628, in create_invite
    data = yield from self.http.create_invite(destination.id, **options)
  File "C:\Program Files\Python36\lib\site-packages\discord\http.py", line 198, in request
    raise NotFound(r, data)
discord.errors.NotFound: NOT FOUND (status code: 404): Unknown Channel

【问题讨论】:

标签: python python-3.x python-3.6 discord discord.py


【解决方案1】:

在这一点上,文档有点过时了。您只能为频道创建邀请。

过去服务器有一个默认频道,因此服务器邀请实际上是对该默认频道的邀请。

较旧的服务器拥有默认频道,但新服务器将没有默认频道。

【讨论】:

  • 您需要从server.channels 中选择一个频道,然后向该频道发送邀请。您可以选择迭代中的第一个频道,或者按名称搜索(大多数服务器都有一个“通用”频道)。这取决于你想要什么。
  • 刚学python,不知道怎么提取iterable中的第一项。
  • channel = next(server.channels)
  • 原来:TypeError: 'dict_values' object is not an iterator
  • channel = next(iter(server.channels)),首先从可迭代对象中获取迭代器
【解决方案2】:

您可以只创建一个到第一个可用频道的邀请,这通常是在应用程序本身中完成的,所以只需这样做:

@client.event
async def on_ready():
    print(client.servers)
    for server in client.servers:
        for channel in server.channels:
            if channel.type == 'Text':
                invitelinknew = await client.create_invite(destination=channel])
                print(invitelinknew)
                break

【讨论】:

  • server.channels 不支持索引。 TypeError: 'dict_values' 对象不支持索引
  • 你使用的是重写还是异步分支?
  • 事实上,放弃它,试试我的重写它(编辑答案)
猜你喜欢
  • 2019-03-02
  • 1970-01-01
  • 1970-01-01
  • 2021-09-10
  • 1970-01-01
  • 2018-03-29
  • 1970-01-01
  • 2021-07-18
  • 2011-12-03
相关资源
最近更新 更多