Skip to main content
 首页 » 编程设计

python之Discord.py 打印邀请链接

2024年12月31日9zhenyulu

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

<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 

请您参考如下方法:

根据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)