我在尝试将邀请链接打印到控制台中时遇到问题,但它会打印出来

<generator object Client.create_invite at 0x000001D310A183B8>
<generator object Client.create_invite at 0x000001D310A65410>


我正在使用此代码:

@client.event
async def on_ready():

    #await client.change_presence(game=Game(name="with humans"))
    print("Logged in as " + client.user.name)
    await asyncio.sleep(3)
    for server in client.servers:
        for channel in server.channels:
            channel_type = channel.type
            if str(channel_type) == 'text':
                invitelinknew = client.create_invite(destination=channel, xkcd=True, max_uses=100)
                print(str(invitelinknew))
                break


我尝试将print(invitelinknew)更改为print(str(invitelinknew)),但是并没有改变结果

编辑:

使用invitelinknew2 = list(invitelinknew)使用生成器时出现新错误
print(invitelinknew2)

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 128, in on_ready
    invitelinknew2 = list(invitelinknew)
  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 137, in request
    r = yield from self.session.request(method, url, **kwargs)
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\client.py", line 555, in __iter__
    resp = yield from self._coro
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\client.py", line 202, in _request
    yield from resp.start(conn, read_until_eof)
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\client_reqrep.py", line 640, in start
    message = yield from httpstream.read()
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\streams.py", line 641, in read
    result = yield from super().read()
  File "C:\Program Files\Python36\lib\site-packages\aiohttp\streams.py", line 476, in read
    yield from self._waiter
AssertionError: yield from wasn't used with future
Future exception was never retrieved
future: <Future finished exception=ServerDisconnectedError()>
aiohttp.errors.ServerDisconnectedError

最佳答案

根据documentationcreate_invite是协程,并且需要await关键字

更改您的代码以包含它,例如

if channel.type == discord.ChannelType.text:
  invitelinknew = await client.create_invite(destination=channel, xkcd=True, max_uses=100)
  print(invitelinknew.url)

关于python - Discord.py打印邀请链接,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52516651/

10-12 20:03