Discord.py打印邀请链接

时间:2018-09-26 11:26:23

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

我遇到一个问题,我尝试将邀请链接打印到控制台中,但是打印出来了

<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

1 个答案:

答案 0 :(得分:1)

根据documentation create_invite是一个协程,并且需要await关键字

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

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