How do I emit custom discord.py events?

Viewed 2483

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)
1 Answers

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)

Related