python3.6.1 |不和谐.py |以用户身份登录
Posted
技术标签:
【中文标题】python3.6.1 |不和谐.py |以用户身份登录【英文标题】:python3.6.1 | discord.py | Logging in as user 【发布时间】:2018-05-31 02:49:25 【问题描述】:我正在尝试制作一些简单的程序,将收到的消息显示到终端。现在我试图向用户询问登录的电子邮件和密码,但出现了一些奇怪的错误。这就是我的代码的样子:
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()。
【问题讨论】:
【参考方案1】:client.login
是一个协程,所以它应该是(未经测试的):
await dc.login(input('email : '), input('password : '), bot=False)
请注意,在这种情况下,不需要 bot 参数。 但是,要使用 client.login,您需要使用 client 循环。为避免这种情况,您可以简单地执行以下操作:
dc.run(email, password)
这将同时登录和连接,然后开始循环。
之后,您可以(在 on_ready 函数中)从 dc.servers 获取所需的服务器和合适的通道发送到那里,例如,“Hello 消息” ' 与 dc.send_message。
完成连接后,在 DiscordClient 类中执行 self.close()。
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)
【讨论】:
以上是关于python3.6.1 |不和谐.py |以用户身份登录的主要内容,如果未能解决你的问题,请参考以下文章