Event Reference#
This section outlines the different types of events listened by Client
.
There are two ways to register an event, the first way is through the use of
Client.event()
. The second way is through subclassing Client
and
overriding the specific events. For example:
import defectio
class MyClient(defectio.Client):
async def on_message(self, message):
if message.author == self.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello World!')
If an event handler raises an exception, on_error()
will be called
to handle it, which defaults to print a traceback and ignoring the exception.
Warning
All the events must be a coroutine. If they aren’t, then you might get unexpected
errors. In order to turn a function into a coroutine they must be async def
functions.
- on_ready()#
Called when the client is done preparing the data received from Revolt. Usually after login is successful.
Warning
This function is not guaranteed to be the first event called. Likewise, this function is not guaranteed to only be called once. This library implements reconnection logic and thus will end up calling this event whenever a RESUME request fails.
- on_error(event, *args, **kwargs)#
Usually when an event raises an uncaught exception, a traceback is printed to stderr and the exception is ignored. If you want to change this behaviour and handle the exception for whatever reason yourself, this event can be overridden. Which, when done, will suppress the default action of printing the traceback.
The information of the exception raised and the exception itself can be retrieved with a standard call to
sys.exc_info()
.If you want exception to propagate out of the
Client
class you can define anon_error
handler consisting of a single empty raise statement. Exceptions raised byon_error
will not be handled in any way byClient
.- Parameters
event (
str
) – The name of the event that raised the exception.args – The positional arguments for the event that raised the exception.
kwargs – The keyword arguments for the event that raised the exception.
- on_socket_raw_receive(msg)#
Called whenever a message is received from the WebSocket, before it’s processed. This event is always dispatched when a message is received and the passed data is not processed in any way.
This is only really useful for grabbing the WebSocket stream and debugging purposes.
Note
This is only for the messages received from the client WebSocket. The voice WebSocket will not trigger this event.
- on_socket_raw_send(payload)#
Called whenever a send operation is done on the WebSocket before the message is sent. The passed parameter is the message that is being sent to the WebSocket.
This is only really useful for grabbing the WebSocket stream and debugging purposes.
Note
This is only for the messages sent from the client WebSocket. The voice WebSocket will not trigger this event.
- on_typing(channel, user, when)#
Called when someone begins typing a message.
The
channel
parameter can be aabc.Messageable
instance. Which could either beTextChannel
,GroupChannel
, orDMChannel
.If the
channel
is aTextChannel
then theuser
parameter is aMember
, otherwise it is aUser
.- Parameters
channel (
abc.Messageable
) – The location where the typing originated from.user (Union[
User
,Member
]) – The user that started typing.when (
datetime.datetime
) – When the typing started as a naive datetime in UTC.
- on_message(message)#
Called when a
Message
is created and sent.Warning
Your bot’s own messages and private messages are sent through this event. This can lead cases of ‘recursion’ depending on how your bot was programmed. If you want the bot to not reply to itself, consider checking the user IDs.
- Parameters
message (
Message
) – The current message.
- on_message_delete(message)#
Called when a message is deleted. If the message is not found in the internal message cache, then this event will not be called. Messages might not be in cache if the message is too old or the client is participating in high traffic servers.
If this occurs increase the
max_messages
parameter or use theon_raw_message_delete()
event instead.- Parameters
message (
Message
) – The deleted message.
- on_raw_message_delete(payload)#
Called when a message is deleted. Unlike
on_message_delete()
, this is called regardless of the message being in the internal message cache or not.If the message is found in the message cache, it can be accessed via
RawMessageDeleteEvent.cached_message
- Parameters
payload (
RawMessageDeleteEvent
) – The raw event payload data.
- on_message_edit(before, after)#
Called when a
Message
receives an update event. If the message is not found in the internal message cache, then these events will not be called. Messages might not be in cache if the message is too old or the client is participating in high traffic servers.If this occurs increase the
max_messages
parameter or use theon_raw_message_edit()
event instead.The following non-exhaustive cases trigger this event:
The message content has been changed.
- Parameters
before (
Message
) – The previous version of the message.after (
Message
) – The current version of the message.
- on_raw_message_edit(payload)#
Called when a message is edited. Unlike
on_message_edit()
, this is called regardless of the state of the internal message cache.If the message is found in the message cache, it can be accessed via
RawMessageUpdateEvent.cached_message
. The cached message represents the message before it has been edited. For example, if the content of a message is modified and triggers theon_raw_message_edit()
coroutine, theRawMessageUpdateEvent.cached_message
will return aMessage
object that represents the message before the content was modified.- Parameters
payload (
RawMessageUpdateEvent
) – The raw event payload data.
- on_private_channel_delete(channel)#
- on_private_channel_create(channel)#
Called whenever a private channel is deleted or created.
- Parameters
channel (
abc.PrivateChannel
) – The private channel that got created or deleted.
- on_private_channel_update(before, after)#
Called whenever a private group DM is updated. e.g. changed name or topic.
- Parameters
before (
GroupChannel
) – The updated group channel’s old info.after (
GroupChannel
) – The updated group channel’s new info.
- on_server_channel_delete(channel)#
- on_server_channel_create(channel)#
Called whenever a server channel is deleted or created.
Note that you can get the server from
server
.- Parameters
channel (
abc.ServerChannel
) – The guild channel that got created or deleted.
- on_server_channel_update(before, after)#
Called whenever a server channel is updated. e.g. changed name, topic, permissions.
- Parameters
before (
abc.ServerChannel
) – The updated server channel’s old info.after (
abc.ServerChannel
) – The updated guild channel’s new info.
- on_member_join(member)#
- on_member_remove(member)#
Called when a
Member
leaves or joins aServer
.- Parameters
member (
Member
) – The member who joined or left.
- on_member_update(before, after)#
Called when a
Member
updates their profile.This is called when one or more of the following things change:
status
nickname
roles
- Parameters
before (
Member
) – The updated member’s old info.after (
Member
) – The updated member’s updated info.
- on_user_update(before, after)#
Called when a
User
updates their profile.This is called when one or more of the following things change:
avatar
username
- Parameters
before (
User
) – The updated user’s old info.after (
User
) – The updated user’s updated info.
- on_server_join(server)#
Called when a
Server
is either created by theClient
or when theClient
joins a server.- Parameters
server (
Server
) – The server that was joined.
- on_server_remove(server)#
Called when a
Server
is removed from theServer
.This happens through, but not limited to, these circumstances:
The client got banned.
The client got kicked.
The client left the server.
The client or the server owner deleted the server.
In order for this event to be invoked then the
Server
must have been part of the server to begin with. (i.e. it is part ofClient.servers
)- Parameters
server (
Server
) – The server that got removed.
- on_server_update(before, after)#
Called when a
Server
updates, for example:Changed name
etc
- Parameters
before (
Server
) – The server prior to being updated.after (
Server
) – The server after being updated.
- on_server_role_create(role)#
- on_server_role_delete(role)#
Called when a
Server
creates or deletes a newRole
.To get the server it belongs to, use
Role.server
.- Parameters
role (
Role
) – The role that was created or deleted.
- on_server_role_update(before, after)#
Called when a
Role
is changed server-wide.- Parameters
before (
Role
) – The updated role’s old info.after (
Role
) – The updated role’s updated info.
- on_member_ban(server, user)#
Called when user gets banned from a
Server
.- Parameters
server (
Server
) – The server the user got banned from.user (Union[
User
,Member
]) – The user that got banned. Can be eitherUser
orMember
depending if the user was in the server or not at the time of removal.
- on_member_unban(server, user)#
Called when a
User
gets unbanned from aServer
.- Parameters
server (
Server
) – The server the user got unbanned from.user (
User
) – The user that got unbanned.
- on_group_join(channel, user)#
- on_group_remove(channel, user)#
Called when someone joins or leaves a
GroupChannel
.- Parameters
channel (
GroupChannel
) – The group that the user joined or left.user (
User
) – The user that joined or left.
- on_relationship_add(relationship)#
- on_relationship_remove(relationship)#
Called when a
Relationship
is added or removed from theClientUser
.- Parameters
relationship (
Relationship
) – The relationship that was added or removed.
- on_relationship_update(before, after)#
Called when a
Relationship
is updated, e.g. when you block a friend or a friendship is accepted.- Parameters
before (
Relationship
) – The previous relationship status.after (
Relationship
) – The updated relationship status.