Remedy.Consumer behaviour (Remedy v0.6.8) View Source
Consumer process for gateway event handling.
Consuming Dispatch Events
To handle events, Remedy uses a GenStage implementation.
Remedy defines the producer
and producer_consumer
in the GenStage design.
To consume the events you must create at least one consumer
process.
Remedy uses a ConsumerSupervisor to dispatch events, meaning your handlers will each be ran in their own seperate task.
The full list of dispatch events and their inner payload is described in the type specs within this module.
- Regular payloads are delivered after casting to their schema and ratified against the cache.
- Irregular payloads are classified as those which do not directly map to a standard discord object. They undergo extensive manipulation prior to updating the cache. They are described under the
DISPATCH
section of the documentation.
Example
It is recommended that you supervise your consumers. First we set up a supervisor module for our consumers.
#example_supervisor.ex
defmodule MyApp.ExampleSupervisor do
use Supervisor
def start_link(args) do
Supervisor.start_link(__MODULE__, args, name: __MODULE__)
end
@impl true
def init(_init_arg) do
children = [ExampleConsumer]
Supervisor.init(children, strategy: :one_for_one)
end
end
You can then set up your consumer module.
#example_consumer.ex
defmodule ExampleConsumer do
use Remedy.Consumer
alias Remedy.Api
def start_link do
Consumer.start_link(__MODULE__)
end
def handle_event({:MESSAGE_CREATE, %Message{content: content}, _ws_state}) do
case content do
"!sleep" ->
Api.create_message(msg.channel_id, "Going to sleep...")
Process.sleep(3000)
"!ping" ->
Api.create_message(msg.channel_id, "pyongyang!")
"!raise" ->
raise "No problems here!"
end
end
def handle_event(_event) do
:noop
end
end
Link to this section Summary
Types
Sent when a new channel is created.
Sent when a channel relevant to the current user is deleted.
Sent when a message is pinned or unpinned in a text channel.
Sent when a channel is updated.
Sent when a user is banned from a guild.
Sent when a user is unbanned from a guild.
This event can be sent in three different scenarios
Sent when a guild becomes or was already unavailable due to
Sent when a guild's emojis have been updated.
Sent when a guild integration is updated.
Sent when a new user joins a guild.
Sent when a used is removed from a guild.
Sent when a guild member is updated.
Sent in response to Guild Request Members.
Sent when a guild role is created.
Sent when a guild role is deleted.
Sent when a guild role is updated.
Sent when a guild is updated.
Sent when an integration is created.
Sent when an integration is deleted.
Sent when an integration is updated.
Sent when a user triggers an Application Command
Sent when a message is created.
Sent when a messgae is deleted.
Sent when multiple messages are deleted at once.
Sent when a user adds a reaction to a message.
Sent when a user removes a reaction from a message.
Sent when a user removes a reaction from a message.
Sent when a bot removes all instances of a given emoji from the reactions of a message.
Sent when a message is updated.
This event is sent when a user's presence or info, such as name or avatar, is updated.
Sent when a thread is created or when the user is added to a thread.
Sent when a thread relevant to the current user is deleted.
Sent when the current user gains access to a channel.
Sent when the thread member object for the current user is updated.
Sent when anyone is added to or removed from a thread.
Sent when a thread is updated.
Link to this section Types
Specs
channel_create() :: {:CHANNEL_CREATE, Remedy.Schema.Channel.t(), Remedy.Gateway.WSState.t()}
Sent when a new channel is created.
Specs
channel_delete() :: {:CHANNEL_DELETE, Remedy.Schema.Channel.t(), Remedy.Gateway.WSState.t()}
Sent when a channel relevant to the current user is deleted.
Specs
channel_pins_update() :: {:CHANNEL_UPDATE, Remedy.Schema.Channel.t(), Remedy.Gateway.WSState.t()}
Sent when a message is pinned or unpinned in a text channel.
This is not sent when a pinned message is deleted.
Specs
channel_update() :: {:CHANNEL_UPDATE, Remedy.Schema.Channel.t(), Remedy.Gateway.WSState.t()}
Sent when a channel is updated.
This is not sent when the field :last_message_id
is altered. To keep track of the :last_message_id
changes, you must listen for message_create/0
events.
Specs
event() :: channel_create() | channel_delete() | channel_pins_update() | channel_update() | guild_available() | guild_ban_add() | guild_ban_remove() | guild_create() | guild_delete() | guild_emojis_update() | guild_integrations_update() | guild_member_add() | guild_member_remove() | guild_member_update() | guild_members_chunk() | guild_role_create() | guild_role_delete() | guild_role_update() | guild_unavailable() | guild_update() | integration_create() | integration_update() | integration_delete() | interaction_create() | message_create() | message_delete_bulk() | message_delete() | message_reaction_add() | message_reaction_remove_all() | message_reaction_remove_emoji() | message_reaction_remove() | message_update() | presence_update() | ready() | thread_create() | thread_delete() | thread_list_sync() | thread_member_update() | thread_members_update() | thread_update() | typing_start() | user_update() | webhooks_update()
Specs
guild_ban_add() :: {:GUILD_BAN_ADD, Ban.t(), Remedy.Gateway.WSState.t()}
Sent when a user is banned from a guild.
Specs
guild_ban_remove() :: {:GUILD_BAN_REMOVE, Ban.t(), Remedy.Gateway.WSState.t()}
Sent when a user is unbanned from a guild.
Specs
guild_create() :: {:GUILD_CREATE, Remedy.Schema.Guild.t(), Remedy.Gateway.WSState.t()}
This event can be sent in three different scenarios:
- When a user is initially connecting, to lazily load and backfill information for all unavailable guilds sent in the Ready event. Guilds that are unavailable due to an outage will send a Guild Delete event.
- When a Guild becomes available again to the client.
- When the current user joins a new Guild.
The inner payload is a guild object, with all the extra fields specified.
Note: If your bot does not have the
:GUILD_PRESENCES
Gateway Intent, or if the guild has over 75k members, members and presences returned in this event will only contain your bot and users in voice channels.
Specs
guild_delete() :: {:GUILD_DELETE, Remedy.Schema.Guild.t(), Remedy.Gateway.WSState.t()}
Sent when a guild becomes or was already unavailable due to:
- An outage
- The user leaves or is removed from a guild.
The inner payload is an unavailable guild object. If the unavailable field is not set, the user was removed from the guild.
Specs
guild_emojis_update() :: {:GUILD_EMOJIS_UPDATE, Remedy.Schema.Guild.t(), Remedy.Gateway.WSState.t()}
Sent when a guild's emojis have been updated.
Specs
guild_integrations_update() :: {:GUILD_INTEGRATIONS_UPDATE, Remedy.Schema.Guild.t(), Remedy.Gateway.WSState.t()}
Sent when a guild integration is updated.
Specs
guild_member_add() :: {:GUILD_MEMBER_ADD, Remedy.Schema.Member.t(), Remedy.Gateway.WSState.t()}
Sent when a new user joins a guild.
Intents
:GUILD_MEMBERS
The inner payload is a guild member object with an extra guild_id.
Specs
guild_member_remove() :: {:GUILD_MEMBER_REMOVE, Remedy.Schema.Member.t(), Remedy.Gateway.WSState.t()}
Sent when a used is removed from a guild.
Intents
:GUILD_MEMBERS
Specs
guild_member_update() :: {:GUILD_MEMBER_UPDATE, Remedy.Schema.Member.t(), Remedy.Schema.Member.t(), Remedy.Gateway.WSState.t()}
Sent when a guild member is updated.
Intents
:GUILD_MEMBERS
This will also fire when the user object of a guild member changes.
Specs
guild_members_chunk() :: {:GUILD_MEMBERS_CHUNK, GuildMembersChunk.t(), Remedy.Gateway.WSState.t()}
Sent in response to Guild Request Members.
Note: While this event can be consumed if you so desire, it is kind of pointles, and used internally for the cache.
Specs
guild_role_create() :: {:GUILD_ROLE_CREATE, Role.t(), Remedy.Gateway.WSState.t()}
Sent when a guild role is created.
Specs
guild_role_delete() :: {:GUILD_ROLE_DELETE, Role.t(), Remedy.Gateway.WSState.t()}
Sent when a guild role is deleted.
Specs
guild_role_update() :: {:GUILD_ROLE_UPDATE, Role.t(), Remedy.Gateway.WSState.t()}
Sent when a guild role is updated.
Specs
guild_update() :: {:GUILD_UPDATE, Remedy.Schema.Guild.t(), Remedy.Gateway.WSState.t()}
Sent when a guild is updated.
The inner payload is a guild object.
Specs
integration_create() :: {:INTEGRATION_CREATE, Remedy.Schema.Integration.t(), Remedy.Gateway.WSState.t()}
Sent when an integration is created.
Specs
integration_delete() :: {:INTEGRATION_DELETE, Remedy.Schema.Integration.t(), Remedy.Gateway.WSState.t()}
Sent when an integration is deleted.
Specs
integration_update() :: {:INTEGRATION_UPDATE, Remedy.Schema.Integration.t(), Remedy.Gateway.WSState.t()}
Sent when an integration is updated.
Specs
interaction_create() :: {:INTERACTION_CREATE, Remedy.Schema.Interaction.t(), Remedy.Gateway.WSState.t()}
Sent when a user triggers an Application Command
Inner payload is an Interaction.
Specs
message_create() :: {:MESSAGE_CREATE, Remedy.Schema.Message.t(), Remedy.Gateway.WSState.t()}
Sent when a message is created.
The inner payload is a message object.
Specs
message_delete() :: {:MESSAGE_DELETE, Remedy.Schema.Message.t(), Remedy.Gateway.WSState.t()}
Sent when a messgae is deleted.
Specs
message_delete_bulk() :: {:MESSAGE_DELETE_BULK, Remedy.Schema.MessageDeleteBulk.t(), Remedy.Gateway.WSState.t()}
Sent when multiple messages are deleted at once.
Specs
message_reaction_add() :: {:MESSAGE_REACTION_ADD, Reaction.t(), Remedy.Gateway.WSState.t()}
Sent when a user adds a reaction to a message.
Specs
message_reaction_remove() :: {:MESSAGE_REACTION_REMOVE, MessageReactionRemove.t(), Remedy.Gateway.WSState.t()}
Sent when a user removes a reaction from a message.
Specs
message_reaction_remove_all() :: {:MESSAGE_REACTION_REMOVE_ALL, Remedy.Schema.MessageReactionRemoveAll.t(), Remedy.Gateway.WSState.t()}
Sent when a user removes a reaction from a message.
Specs
message_reaction_remove_emoji() :: {:MESSAGE_REACTION_REMOVE_EMOJI, Remedy.Schema.MessageReactionRemoveEmoji.t(), Remedy.Gateway.WSState.t()}
Sent when a bot removes all instances of a given emoji from the reactions of a message.
Specs
message_update() :: {:MESSAGE_UPDATE, Remedy.Schema.Message.t(), Remedy.Gateway.WSState.t()}
Sent when a message is updated.
Note: Unlike creates, message updates may contain only a subset of the full message object payload (but will always contain an id and channel_id).
Specs
presence_update() :: {:PRESENCE_UPDATE, Remedy.Schema.User.t(), Remedy.Gateway.WSState.t()}
This event is sent when a user's presence or info, such as name or avatar, is updated.
Intents
:GUILD_PRESENCES
Note: The user object within this event can be partial, the only field which must be sent is the id field, everything else is optional. Along with this limitation, no fields are required, and the types of the fields are not validated. Your client should expect any combination of fields and types within this event.
Specs
ready() :: {:READY, Remedy.Schema.Ready.t(), Remedy.Gateway.WSState.t()}
Specs
thread_create() :: {:THREAD_CREATE, Thread.t(), Remedy.Gateway.WSState.t()}
Sent when a thread is created or when the user is added to a thread.
When being added to an existing private thread, includes a thread member object.
Specs
thread_delete() :: {:THREAD_DELETE, Thread.t(), Remedy.Gateway.WSState.t()}
Sent when a thread relevant to the current user is deleted.
The inner payload is a subset of the channel object, containing just the id, guild_id, parent_id, and type fields.
Specs
thread_list_sync() :: {:THREAD_LIST_SYNC, Remedy.Schema.ThreadListSync.t(), Remedy.Gateway.WSState.t()}
Sent when the current user gains access to a channel.
Specs
thread_member_update() :: {:THREAD_MEMBER_UPDATE, Remedy.Schema.ThreadMember.t(), Remedy.Gateway.WSState.t()}
Sent when the thread member object for the current user is updated.
The inner payload is a thread member object. This event is documented for completeness, but unlikely to be used by most bots. For bots, this event largely is just a signal that you are a member of the thread. See the threads docs for more details.
Specs
thread_members_update() :: {:THREAD_MEMBERS_UPDATE, Remedy.Schema.ThreadMember.t(), Remedy.Gateway.WSState.t()}
Sent when anyone is added to or removed from a thread.
If the current user does not have the GUILD_MEMBERS
Gateway Intent, then this event will only be sent if the current user was added to or removed from the thread.
Specs
thread_update() :: {:THREAD_UPDATE, Thread.t(), Remedy.Gateway.WSState.t()}
Sent when a thread is updated.
The inner payload is a channel object. This is not sent when the field :last_message_id
is altered. To keep track of the :last_message_id
changes, you must listen for message_create/0
events.
Specs
typing_start() :: {:TYPING_START, Remedy.Schema.TypingStart.t(), Remedy.Gateway.WSState.t()}
Specs
user_update() :: {:USER_UPDATE, Remedy.Schema.User.t(), Remedy.Gateway.WSState.t()}
Specs
voice_state_update() :: {:VOICE_STATE_UPDATE, Remedy.Schema.VoiceState.t(), Remedy.Gateway.WSState.t()}
Specs
webhooks_update() :: {:WEBHOOKS_UPDATE, Remedy.Schema.WebhooksUpdate.t(), Remedy.Gateway.WSState.t()}