Skip to content

Instantly share code, notes, and snippets.

@5S
Created February 2, 2019 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5S/8217a4d0278b83fe31593cf5e14a4e96 to your computer and use it in GitHub Desktop.
Save 5S/8217a4d0278b83fe31593cf5e14a4e96 to your computer and use it in GitHub Desktop.
discord.py 1.0.0a で discord.py 0.16.12 まで使えていたログイン方法を復活させるパッチ 使用方法: git apply diff.patch
diff --git a/client.py b/client.py
index f832efc..18e44e0 100755
--- a/client.py
+++ b/client.py
@@ -117,6 +117,7 @@ class Client:
"""
def __init__(self, *, loop=None, **options):
self.ws = None
+ self.email = None
self.loop = asyncio.get_event_loop() if loop is None else loop
self._listeners = {}
self.shard_id = options.get('shard_id')
@@ -315,13 +316,37 @@ class Client:
# login state management
- async def login(self, token, *, bot=True):
+ async def _login_1(self, token, **kwargs):
+ log.info('logging in using static token')
+ bot = kwargs.pop('bot', True)
+ await self.http.static_login(token, bot=bot)
+ self.email = None
+ self._connection.is_bot = bot
+
+ async def _login_2(self, email, password, **kwargs):
+ log.info('logging in using email and password')
+ await self.http.email_login(email, password)
+ self.email = email
+ self._connection.is_bot = False
+
+ async def login(self, *args, **kwargs):
"""|coro|
Logs in the client with the specified credentials.
This function can be used in two different ways.
+ .. code-block:: python
+
+ await client.login('token')
+
+ # or
+
+ await client.login('email', 'password')
+
+ More than 2 parameters or less than 1 parameter raises a
+ :exc:`TypeError`.
+
.. warning::
Logging on with a user token is against the Discord
@@ -336,7 +361,8 @@ class Client:
anything as the library will do it for you.
bot: bool
Keyword argument that specifies if the account logging on is a bot
- token or not.
+ token or not. Only useful for logging in with a static token.
+ Ignored for the email and password combo. Defaults to ``True``.
Raises
------
@@ -346,11 +372,15 @@ class Client:
An unknown HTTP related error occurred,
usually when it isn't 200 or the known incorrect credentials
passing status code.
+ TypeError
+ The incorrect number of parameters is passed.
"""
- log.info('logging in using static token')
- await self.http.static_login(token, bot=bot)
- self._connection.is_bot = bot
+ n = len(args)
+ if n in (2, 1):
+ await getattr(self, '_login_' + str(n))(*args, **kwargs)
+ else:
+ raise TypeError('login() takes 1 or 2 positional arguments but {} were given'.format(n))
async def logout(self):
"""|coro|
diff --git a/http.py b/http.py
index 5ed862d..234d874 100755
--- a/http.py
+++ b/http.py
@@ -239,6 +239,22 @@ class HTTPClient:
# login management
+ async def email_login(self, email, password):
+ payload = {
+ 'email': email,
+ 'password': password
+ }
+
+ try:
+ data = await self.request(Route('POST', '/auth/login'), json=payload)
+ except HTTPException as e:
+ if e.response.status == 400:
+ raise LoginFailure('Improper credentials have been passed.') from e
+ raise
+
+ self._token(data['token'], bot=False)
+ return data
+
async def static_login(self, token, *, bot):
old_token, old_bot = self.token, self.bot_token
self._token(token, bot=bot)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment