Browse Source

Update building_block_listeners.md

pull/56/head
Jari (LotU) 8 years ago
committed by GitHub
parent
commit
cc8c43434c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 31
      docs/bot_tutorial/building_block_listeners.md

31
docs/bot_tutorial/building_block_listeners.md

@ -1,8 +1,8 @@
# Listeners # Listeners
Listeners are a way to execute custom actions when a certain Discord event happens. For example, on message creation, when a member joins or leaves a guild, or when someone changes user or nickname. Listeners are a way to execute custom actions when a certain Discord event happens. For example, on message creation, when a member joins or leaves a guild, or when someone changes their username or nickname.
## Listeners in disco ## Listeners in disco
Listeners are easy to use and implement in Disco. First of all, we'll create a [plugin](https://b1naryth1ef.github.io/disco/bot_tutorial/building_block_plugins.html) like so: Listeners are easy to use and implement in Disco. First of all, we'll create a [Plugin](https://b1naryth1ef.github.io/disco/bot_tutorial/building_block_plugins.html) like so:
```py ```py
class MyPlugin(Plugin): class MyPlugin(Plugin):
``` ```
@ -14,21 +14,38 @@ def on_event_name(self, event):
``` ```
Change the `'EventName'` in the `.listen()` method to the event name you want to listen to, and give the `on_event_name` method a more descriptive name. Change the `'EventName'` in the `.listen()` method to the event name you want to listen to, and give the `on_event_name` method a more descriptive name.
Now, on to adding the first listener to our plugin. This listener will listen for a new message and will reply with the exact same message every time.
The listener will listen for a new message and will reply with the exact same message every time.
```py ```py
@Plugin.listen('MesageCreate') @Plugin.listen('MesageCreate')
def on_message_create(self, event): def on_message_create(self, event):
event.reply(event.message.content) event.reply(event.message.content)
``` ```
Let's create a nother listener, this time one that listens for a member that's added to the guild, when this happens, it will send a welcome message in a welcome channel. For this example, we'll asume the ID for that channel's ID is stored in a constant named `WELCOME_CHANNEL`: Let's create another listener, this time one that listens for a member that's added to the guild, when this happens, it will send a welcome message in a welcome channel:
```py ```py
WELCOME_CHANNEL = 381890676654080001
@Plugin.listen('GuildMemberAdd') @Plugin.listen('GuildMemberAdd')
def on_member_add(self, event): def on_member_add(self, event):
self.bot.client.api.channels_messages_create( self.bot.client.state.channels.get(WELCOME_CHANNEL).send_message(
WELCOME_CHANNEL,
'Welcome to the server {}'.format(event.member.user.mention()) 'Welcome to the server {}'.format(event.member.user.mention())
) )
``` ```
A list of all Discord events supported by disco can be found [here](https://b1naryth1ef.github.io/disco/api/disco_gateway_events.html) including event attributes and functions you can use on the event property. A list of all Discord events supported by disco can be found [here](https://b1naryth1ef.github.io/disco/api/disco_gateway_events.html) including event attributes and functions you can use on the event property.
These are all the listeners we created in this tutorial:
```py
class MyPlugin(Plugin):
@Plugin.listen('MesageCreate')
def on_message_create(self, event):
event.reply(event.message.content)
WELCOME_CHANNEL = 381890676654080001
@Plugin.listen('GuildMemberAdd')
def on_member_add(self, event):
self.bot.client.state.channels.get(WELCOME_CHANNEL).send_message(
'Welcome to the server {}'.format(event.member.user.mention())
)
```

Loading…
Cancel
Save