gpt4 book ai didi

python-3.x - python3.6.1 |不和谐.py |以用户身份登录

转载 作者:行者123 更新时间:2023-12-04 03:07:11 26 4
gpt4 key购买 nike

我正在尝试制作一些简单的程序,向终端显示收到的消息。现在我试图向用户询问用于登录的电子邮件和密码,但发生了一些奇怪的错误。这是我的代码的样子:

import discord


class DiscordClient(discord.Client):
def __init__(self, *args, **kwargs):
discord.Client.__init__(self, **kwargs)

async def on_ready(self):
print('Success!')


if __name__ == '__main__':
dc = DiscordClient()
dc.login(input('email : '), input('password : '), bot=False)
dc.run()

错误是:
Traceback (most recent call last):
File "/Users/jan/PycharmProjects/TheRealUltron/Main.py", line 16, in <module>
dc.run()
File "/Users/jan/TheRealZeldaPython/lib/python3.6/site-packages/discord/client.py", line 519, in run
self.loop.run_until_complete(self.start(*args, **kwargs))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py", line 466, in run_until_complete
return future.result()
File "/Users/jan/TheRealZeldaPython/lib/python3.6/site-packages/discord/client.py", line 490, in start
yield from self.login(*args, **kwargs)
File "/Users/jan/TheRealZeldaPython/lib/python3.6/site-packages/discord/client.py", line 418, in login
raise TypeError('login() takes 1 or 2 positional arguments but {} were given'.format(n))
TypeError: login() takes 1 or 2 positional arguments but 0 were given
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x103881fd0>

那么,我做错了什么,或者代码应该是什么样子。我使用 discord.py 所做的只是 on_message() 和一些基本命令,如 send_message()。

最佳答案

client.login是一个协程,所以它应该是(未经测试):

await dc.login(input('email : '), input('password : '), bot=False)
请注意,在这种情况下,不需要 bot 参数。
但是,要使用 客户登录 ,您需要使用客户端循环。为避免这种情况,您可以简单地执行以下操作:
dc.run(email, password)
这将登录和连接,然后开始循环。
之后,您可以(在 on_ready 函数中)从 获取所需的服务器dc.servers 和一个合适的 channel 发送到那里,例如,一个“你好消息”与 dc.send_message .
完成连接后,执行 self.close() 从 DiscordClient 类中。
的工作示例Python 3.4 (根据需要替换 Python 3.6 的关键字)
import discord
import asyncio
import datetime


class DiscordClient(discord.Client):
def __init__(self, *args, **kwargs):
discord.Client.__init__(self, **kwargs)

@asyncio.coroutine
def on_ready(self):
servers = list(self.servers)
for server in servers:
if server.name == 'My server':
break

for channel in server.channels:
if channel.name == 'general':
break

now = datetime.datetime.now()
yield from self.send_message(channel, 'Api Success! at ' + str(now))
print('Success!')
yield from self.close()


if __name__ == '__main__':
dc = DiscordClient()
email = input('email : ')
password = input('password : ')
dc.run(email, password)

关于python-3.x - python3.6.1 |不和谐.py |以用户身份登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47856972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com