CircuitSacul
CircuitSacul

Reputation: 1840

How do I emit custom discord.py events?

In discord.py, I am able to use even listeners like this:

@bot.event
async def on_event_name(*args, **kwargs):
    # do stuff

I have seen some libraries that appear to emit custom events, ones that aren't included in discord.py be default. I'm wondering how this is done? I'm looking for something like this:

# bot.py:
@bot.event
async def my_event_name(data):
    #do stuff

# somewhere else
await bot.emit('my_event_name', data)

Upvotes: 2

Views: 4381

Answers (1)

Just for fun
Just for fun

Reputation: 4225

You can make your own events using Bot.dispatch

Here is an example:

bot.dispatch("my_event", a, b, c)

is heard by

a, b, c = await bot.wait_for("my_event")

and

@bot.event
async def on_my_event(a, b, c):
  # ...

(don't forget to write the "on_" at the beginning of your function)

Upvotes: 6

Related Questions