v0.10.0으로 이동¶
v0.10.0은 라이브러리가 작동하는 방식의 변화 때문에, 라이브러리에 가장 큰 변화가 이루어진 버전입니다.
가장 큰 변화는 라이브러리가 Python 3.4.2 버전 밑의 버전들을 지원하지 않는다는것 입니다. 이것은 asyncio`를 지원하기 위함인데, 더 자세하게 말하면, :issue:`통신, 응답 관련 문제
때문입니다. 다시 말하자면, 결론적으로, ** Python 2.7과 3.3은 더이상 지원을 하지 않는다는 것 입니다.
아래에 있는 내용은 v0.9.0에서 v0.10.0 사이에 일어난 큰 변화들 입니다.
이벤트 등록¶
이벤트들은 Client.event`로 먼저 등록이 됩니다. 그 사이에 이것은 여전히 가능합니다 : 이벤트들은 ``@asyncio.coroutine`()
으로 데코레이팅 되야 합니다.
이전:
@client.event
def on_message(message):
pass
이후:
@client.event
@asyncio.coroutine
def on_message(message):
pass
또는 Python 3.5+ 에서:
@client.event
async def on_message(message):
pass
타이핑 해야할 것이 많이 때문에, 유틸리티 데코레이터인(Client.async_event()
)가 편하게 등록을 하기 위해서 제공됩니다.예시 :
@client.async_event
def on_message(message):
pass
하지만 조심하세요, 그것은 아직도 코루틴이고, 당신의 다른 코루틴 함수들은 반드시 @asyncio.coroutine``로 데코레이팅 되거나 ``async def
형식이여야 합니다.
이벤트 변화들¶
Some events in v0.9.0 were considered pretty useless due to having no separate states. The main
events that were changed were the _update
events since previously they had no context on what
was changed.
이전:
def on_channel_update(channel): pass
def on_member_update(member): pass
def on_status(member): pass
def on_server_role_update(role): pass
def on_voice_state_update(member): pass
def on_socket_raw_send(payload, is_binary): pass
이후:
def on_channel_update(before, after): pass
def on_member_update(before, after): pass
def on_server_role_update(before, after): pass
def on_voice_state_update(before, after): pass
def on_socket_raw_send(payload): pass
Note that on_status
was removed. If you want its functionality, use on_member_update()
.
See Event Reference for more information. Other removed events include on_socket_closed
, on_socket_receive
, and on_socket_opened
.
Coroutines¶
The biggest change that the library went through is that almost every function in Client
was changed to be a coroutine. Functions
that are marked as a coroutine in the documentation must be awaited from or yielded from in order
for the computation to be done. For example…
이전:
client.send_message(message.channel, 'Hello')
이후:
yield from client.send_message(message.channel, 'Hello')
# or in python 3.5+
await client.send_message(message.channel, 'Hello')
In order for you to yield from
or await
a coroutine then your function must be decorated
with @asyncio.coroutine
or async def
.
Iterables¶
For performance reasons, many of the internal data structures were changed into a dictionary to support faster lookup. As a consequence, this meant that some lists that were exposed via the API have changed into iterables and not sequences. In short, this means that certain attributes now only support iteration and not any of the sequence functions.
The affected attributes are as follows:
Client.servers
Server.channels
Server.members
Some examples of previously valid behaviour that is now invalid
if client.servers[0].name == "test":
# do something
Since they are no longer list
s, they no longer support indexing or any operation other than iterating.
In order to get the old behaviour you should explicitly cast it to a list.
servers = list(client.servers)
# work with servers
경고
Due to internal changes of the structure, the order you receive the data in is not in a guaranteed order.
Enumerations¶
Due to dropping support for versions lower than Python 3.4.2, the library can now use enum — 열거형 지원 in places where it makes sense.
The common places where this was changed was in the server region, member status, and channel type.
이전:
server.region == 'us-west'
member.status == 'online'
channel.type == 'text'
이후:
server.region == discord.ServerRegion.us_west
member.status = discord.Status.online
channel.type == discord.ChannelType.text
The main reason for this change was to reduce the use of finicky strings in the API as this could give users a false sense of power. More information can be found in the Enumerations page.
Properties¶
A lot of function calls that returned constant values were changed into Python properties for ease of use in format strings.
The following functions were changed into properties:
Before |
After |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Member Management¶
Functions that involved banning and kicking were changed.
Before |
After |
|
|
|
|
Renamed Functions¶
Functions have been renamed.
Before |
After |
|
|
All the Permissions
related attributes have been renamed and the can_ prefix has been
dropped. So for example, can_manage_messages
has become manage_messages
.
Forced Keyword Arguments¶
Since 3.0+ of Python, we can now force questions to take in forced keyword arguments. A keyword argument is when you
explicitly specify the name of the variable and assign to it, for example: foo(name='test')
. Due to this support,
some functions in the library were changed to force things to take said keyword arguments. This is to reduce errors of
knowing the argument order and the issues that could arise from them.
The following parameters are now exclusively keyword arguments:
Client.send_message()
tts
Client.logs_from()
before
after
Client.edit_channel_permissions()
allow
deny
In the documentation you can tell if a function parameter is a forced keyword argument if it is after \*,
in the function signature.
Running the Client¶
In earlier versions of discord.py, client.run()
was a blocking call to the main thread
that called it. In v0.10.0 it is still a blocking call but it handles the event loop for you.
However, in order to do that you must pass in your credentials to Client.run()
.
Basically, before:
client.login('token')
client.run()
이후:
client.run('token')
경고
Like in the older Client.run
function, the newer one must be the one of
the last functions to call. This is because the function is blocking. Registering
events or doing anything after Client.run()
will not execute until the function
returns.
This is a utility function that abstracts the event loop for you. There’s no need for the run call to be blocking and out of your control. Indeed, if you want control of the event loop then doing so is quite straightforward:
import discord
import asyncio
client = discord.Client()
@asyncio.coroutine
def main_task():
yield from client.login('token')
yield from client.connect()
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main_task())
except:
loop.run_until_complete(client.logout())
finally:
loop.close()