discordapi.handler

Module Contents

Classes

EventHandler

Base client for EventHandler.

GeneratorEventHandler

Handler to be used within terminal, or with a simple bot.

MethodEventHandler

Handler to handle methods with defined methods.

DecoratorEventHandler

Handler to assign functions per events with decorator.

ThreadedMethodEventHandler

Handler that starts thread automatically per every events.

ThreadedDecoratorEventHandler

Same as ThreadedMethodEventHandler, but decorator version.

class discordapi.handler.EventHandler(client=None)

Base client for EventHandler.

self.client
set_client(self, client)

Sets client to be used.

Required to provide client-side interface and contexts to handlers.

abstract handle(self, event, obj)

Handler function to handle the event.

This method handles the event fired by the Gateway. Since this runs on a main client thread, running a time-consuming job will block other events from reaching the client, stopping the bot from functioning. Please start another thread for those jobs.

This method should be implemented by the inherited class.

Parameters
  • event – str indicating the type of this event. comes in ALL_CAPS.

  • obj – Corresponding type of object for the event. Possible types are: Channel/Guild/Member/Message/dict. Check DiscordGateway._event_parser method to see how things are handled.

class discordapi.handler.GeneratorEventHandler(client=None)

Bases: EventHandler

Inheritance diagram of discordapi.handler.GeneratorEventHandler

Handler to be used within terminal, or with a simple bot.

This handler provides a new way to handle events- by using a generator. By iterating through .event_generator method with for loop, You can receive events without declaring additional functions, which is suitable for simple usages.

event_queue

Queue object storing events.

handle(self, event, obj)

Handler function to handle the event.

This method handles the event fired by the Gateway. Since this runs on a main client thread, running a time-consuming job will block other events from reaching the client, stopping the bot from functioning. Please start another thread for those jobs.

This method should be implemented by the inherited class.

Parameters
  • event – str indicating the type of this event. comes in ALL_CAPS.

  • obj – Corresponding type of object for the event. Possible types are: Channel/Guild/Member/Message/dict. Check DiscordGateway._event_parser method to see how things are handled.

event_generator(self)

Generator yielding events.

Yielded data will be in a tuple of (event, obj)- where event is the type of the object and obj is the object itself. Same as .handle args.

This generator could be used like this:

for event, obj in handler.event_generator():
    print(f"{event}: {obj}")

or, in the interpreter: like this:

>>> gen = handler.event_generator()
>>> event, obj = next(gen)
>>> print(f"{event}: {obj}")
>>> # Rinse and repeat

Additionally, This generator silences KeyboardInterrupt exception.

class discordapi.handler.MethodEventHandler(client=None)

Bases: EventHandler

Inheritance diagram of discordapi.handler.MethodEventHandler

Handler to handle methods with defined methods.

This handler calls .on_{lowercased event type} method when event issues. e.g. self.on_message_create method gets called when MESSAGE_CREATE fires. If the corresponding method has not defined, It does nothing.

The arguments handlers will receive is the same as .handle method.

Since this runs on a main client thread, running a time-consuming job will block other events from reaching the client, stopping the bot from functioning. Please start another thread for those jobs.

No method other than .handle comes predefined, You have to either inherit this method or assign functions as attributes to use this.

handle(self, event, obj)

Handler function to handle the event.

This method handles the event fired by the Gateway. Since this runs on a main client thread, running a time-consuming job will block other events from reaching the client, stopping the bot from functioning. Please start another thread for those jobs.

This method should be implemented by the inherited class.

Parameters
  • event – str indicating the type of this event. comes in ALL_CAPS.

  • obj – Corresponding type of object for the event. Possible types are: Channel/Guild/Member/Message/dict. Check DiscordGateway._event_parser method to see how things are handled.

class discordapi.handler.DecoratorEventHandler(client=None)

Bases: EventHandler

Inheritance diagram of discordapi.handler.DecoratorEventHandler

Handler to assign functions per events with decorator.

It lets you assign individual handlers per events using decorator, similar to how other libraries such as Discord.py and Telethon works.

You can use @handler.on(“event_name”) to assign the handler.

function to be assigned should receive a single arguments- the object returned from the gateway. Handler is being provided in a second argument as a purpose of giving context.

Other than decorator, This handler behaves similar to MethodEventHandler.

handle(self, event, obj)

Handler function to handle the event.

This method handles the event fired by the Gateway. Since this runs on a main client thread, running a time-consuming job will block other events from reaching the client, stopping the bot from functioning. Please start another thread for those jobs.

This method should be implemented by the inherited class.

Parameters
  • event – str indicating the type of this event. comes in ALL_CAPS.

  • obj – Corresponding type of object for the event. Possible types are: Channel/Guild/Member/Message/dict. Check DiscordGateway._event_parser method to see how things are handled.

on(self, event)
class discordapi.handler.ThreadedMethodEventHandler(client=None)

Bases: MethodEventHandler

Inheritance diagram of discordapi.handler.ThreadedMethodEventHandler

Handler that starts thread automatically per every events.

Since handler runs on a main client thread, time-consuming actions will block other events from reaching, stopping the bot from functioning. this handler will counter that problem by starting a new thread for every events being fired.

This is a basic implementation with no limits or safety measures being placed whatsoever, and could be critical to performance. I recommend implementing your own handler with appropriate safety measures in place.

handle(self, event, obj)

Handler function to handle the event.

This method handles the event fired by the Gateway. Since this runs on a main client thread, running a time-consuming job will block other events from reaching the client, stopping the bot from functioning. Please start another thread for those jobs.

This method should be implemented by the inherited class.

Parameters
  • event – str indicating the type of this event. comes in ALL_CAPS.

  • obj – Corresponding type of object for the event. Possible types are: Channel/Guild/Member/Message/dict. Check DiscordGateway._event_parser method to see how things are handled.

class discordapi.handler.ThreadedDecoratorEventHandler(client=None)

Bases: DecoratorEventHandler

Inheritance diagram of discordapi.handler.ThreadedDecoratorEventHandler

Same as ThreadedMethodEventHandler, but decorator version.

Refer to ThreadMethodEventHandler for details, those cautions apply here too.

handle(self, event, obj)

Handler function to handle the event.

This method handles the event fired by the Gateway. Since this runs on a main client thread, running a time-consuming job will block other events from reaching the client, stopping the bot from functioning. Please start another thread for those jobs.

This method should be implemented by the inherited class.

Parameters
  • event – str indicating the type of this event. comes in ALL_CAPS.

  • obj – Corresponding type of object for the event. Possible types are: Channel/Guild/Member/Message/dict. Check DiscordGateway._event_parser method to see how things are handled.