python3.6.1 |discord.py |以用户身份登录 [英] python3.6.1 | discord.py | Logging in as user
问题描述
我正在尝试制作一些简单的程序,向终端显示收到的消息.现在,我试图向用户询问用于登录的电子邮件和密码,但是会发生一些奇怪的错误.这是我的代码的样子:
I am trying to make some simple program, showing received messages to terminal. Now I am trying to ask user for email and password for login, but some weird error occurs. This is how my code look like:
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().
So, what am I doing wrong, or should code look like. All I was making with discord.py is on_message() and some basic commands like send_message().
推荐答案
client.login 是协程,因此应(未试用)
client.login is a coroutine so it should be (untested)
await dc.login(input('email : '), input('password : '), bot=False)
请注意,在这种情况下,不需要 bot 参数.但是,要使用 client.login ,您需要使用 client循环.为避免这种情况,您只需执行
Note that in this case, bot parameter is unneeded. However, to use client.login, you need to use the client loop. To avoid that, you can simply do
dc.run(email, password)
将同时登录和连接,然后开始循环.
Which will both login and connect and then start the loop.
此后,您可以在 on_ready 函数中,从 dc.servers 获取所需的服务器,并通过合适的渠道向其发送消息,例如"Hello消息".与 dc.send_message .
After that you can, in the on_ready function, get a desired server from dc.servers and a suitable channel to send there, for example, a 'Hello message' with dc.send_message.
完成连接后,请在 DiscordClient 类中执行 self.close().
When you are done with the connection, do self.close() from within the DiscordClient class.
Python 3.4 的工作示例(替换Python 3.6所需的关键字)
Working example for Python 3.4 (replace keywords as necessary for 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 |discord.py |以用户身份登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!