Lingo.Event (Lingo v0.4.2)

Copy Markdown View Source

The events Lingo.Bot.handle_event/2 receives: every one Discord sends, named as Discord names it, lowercased.

def handle_event(:message_create, %Lingo.Type.Message{content: "!ping"} = msg),
  do: Lingo.Api.Message.create(msg.channel_id, "Pong!")

def handle_event(:message_delete, %Lingo.Event.MessageDelete{old: %{content: content}}),
  do: Logger.info("deleted: #{content}")

def handle_event(:message_update, %{old: old, new: new}), do: ...

A payload takes one of three shapes:

  • a resource (Lingo.Type.Message, Lingo.Type.Channel, ...), the same struct Lingo.Api returns, when Discord sends one: creates, and deletes that carry the whole object, such as :channel_delete.
  • %{old: t | nil, new: t} for updates of a resource.

  • a struct of its own in Lingo.Event, named after Discord's event objects, for everything else: deletes that carry only ids, bans, reactions, typing and so on.

old

Updates, and deletes that carry only ids, include old: the resource as the cache held it just before the change. It's nil when it wasn't cached: the resource is disabled (nothing is cached by default, see Lingo.Cache), filtered out or evicted, or Lingo never saw it. So nil means unknown, never empty, and a handler that needs old should match nil too. Without a cache, new in :message_update holds only the fields Discord sent.

Unknown events

Events newer than Lingo arrive as :unknown, with a Lingo.Event.Unknown holding Discord's name and raw payload.

Lingo's own happenings (shards connecting and disconnecting, API rate limits) are not events but Lingo.Telemetry events; to wait until every shard is up, use Lingo.Gateway.await_ready/1.

Gateway events

EventPayload
:readyLingo.Event.Ready
:resumedLingo.Event.Resumed
:application_command_permissions_updateLingo.Type.CommandPermissions
:auto_moderation_rule_createLingo.Type.AutoModerationRule
:auto_moderation_rule_update%{old: old, new: new} of Lingo.Type.AutoModerationRule
:auto_moderation_rule_deleteLingo.Type.AutoModerationRule
:auto_moderation_action_executionLingo.Event.AutoModerationAction
:channel_createLingo.Type.Channel
:channel_update%{old: old, new: new} of Lingo.Type.Channel
:channel_deleteLingo.Type.Channel
:channel_infoLingo.Event.ChannelInfo
:channel_pins_updateLingo.Event.ChannelPinsUpdate
:thread_createLingo.Type.Channel
:thread_update%{old: old, new: new} of Lingo.Type.Channel
:thread_deleteLingo.Event.ThreadDelete
:thread_list_syncLingo.Event.ThreadListSync
:thread_member_updateLingo.Type.ThreadMember
:thread_members_updateLingo.Event.ThreadMembersUpdate
:entitlement_createLingo.Type.Entitlement
:entitlement_update%{old: old, new: new} of Lingo.Type.Entitlement
:entitlement_deleteLingo.Type.Entitlement
:guild_createLingo.Type.Guild
:guild_update%{old: old, new: new} of Lingo.Type.Guild
:guild_deleteLingo.Event.GuildDelete
:guild_audit_log_entry_createLingo.Type.AuditLogEntry
:guild_ban_addLingo.Event.GuildBan
:guild_ban_removeLingo.Event.GuildBan
:guild_emojis_updateLingo.Event.GuildEmojisUpdate
:guild_stickers_updateLingo.Event.GuildStickersUpdate
:guild_integrations_updateLingo.Event.GuildIntegrationsUpdate
:guild_member_addLingo.Type.Member
:guild_member_update%{old: old, new: new} of Lingo.Type.Member
:guild_member_removeLingo.Event.GuildMemberRemove
:guild_members_chunkLingo.Event.GuildMembersChunk
:guild_role_createLingo.Type.Role
:guild_role_update%{old: old, new: new} of Lingo.Type.Role
:guild_role_deleteLingo.Event.GuildRoleDelete
:guild_scheduled_event_createLingo.Type.ScheduledEvent
:guild_scheduled_event_update%{old: old, new: new} of Lingo.Type.ScheduledEvent
:guild_scheduled_event_deleteLingo.Type.ScheduledEvent
:guild_scheduled_event_user_addLingo.Event.ScheduledEventUser
:guild_scheduled_event_user_removeLingo.Event.ScheduledEventUser
:guild_soundboard_sound_createLingo.Type.SoundboardSound
:guild_soundboard_sound_update%{old: old, new: new} of Lingo.Type.SoundboardSound
:guild_soundboard_sound_deleteLingo.Event.SoundboardSoundDelete
:guild_soundboard_sounds_updateLingo.Event.SoundboardSounds
:soundboard_soundsLingo.Event.SoundboardSounds
:integration_createLingo.Type.Integration
:integration_update%{old: old, new: new} of Lingo.Type.Integration
:integration_deleteLingo.Event.IntegrationDelete
:interaction_createLingo.Type.Interaction
:invite_createLingo.Type.Invite
:invite_deleteLingo.Event.InviteDelete
:message_createLingo.Type.Message
:message_update%{old: old, new: new} of Lingo.Type.Message
:message_deleteLingo.Event.MessageDelete
:message_delete_bulkLingo.Event.MessageDeleteBulk
:message_reaction_addLingo.Event.Reaction
:message_reaction_removeLingo.Event.Reaction
:message_reaction_remove_allLingo.Event.ReactionRemoveAll
:message_reaction_remove_emojiLingo.Event.Reaction
:message_poll_vote_addLingo.Event.PollVote
:message_poll_vote_removeLingo.Event.PollVote
:presence_update%{old: old, new: new} of Lingo.Type.Presence
:rate_limitedLingo.Event.RateLimited
:stage_instance_createLingo.Type.StageInstance
:stage_instance_update%{old: old, new: new} of Lingo.Type.StageInstance
:stage_instance_deleteLingo.Type.StageInstance
:subscription_createLingo.Type.Subscription
:subscription_update%{old: old, new: new} of Lingo.Type.Subscription
:subscription_deleteLingo.Type.Subscription
:typing_startLingo.Event.TypingStart
:user_update%{old: old, new: new} of Lingo.Type.User
:voice_channel_effect_sendLingo.Event.VoiceChannelEffect
:voice_channel_status_updateLingo.Event.VoiceChannelStatus
:voice_channel_start_time_updateLingo.Event.VoiceChannelStartTime
:voice_state_update%{old: old, new: new} of Lingo.Type.VoiceState
:webhooks_updateLingo.Event.WebhooksUpdate

Webhook events

These come through Lingo.Endpoint's webhook events URL. Entitlement events come both ways: a bot with the gateway and webhook events on gets each one twice, so deduplicate by id or subscribe to one.

EventPayload
:application_authorizedLingo.Event.ApplicationAuthorized
:application_deauthorizedLingo.Event.ApplicationDeauthorized
:entitlement_createLingo.Type.Entitlement
:entitlement_update%{old: old, new: new} of Lingo.Type.Entitlement
:entitlement_deleteLingo.Type.Entitlement
:lobby_message_createLingo.Type.LobbyMessage
:lobby_message_update%{old: old, new: new} of Lingo.Type.LobbyMessage
:lobby_message_deleteLingo.Event.LobbyMessageDelete
:game_direct_message_createLingo.Type.Message
:game_direct_message_update%{old: old, new: new} of Lingo.Type.Message
:game_direct_message_deleteLingo.Type.Message

Summary

Types

An event's name.

An event's payload. The table above says which goes with which name.

An update's payload: the resource before (from the cache, or nil) and after.

Types

name()

@type name() ::
  :ready
  | :resumed
  | :application_command_permissions_update
  | :auto_moderation_rule_create
  | :auto_moderation_rule_update
  | :auto_moderation_rule_delete
  | :auto_moderation_action_execution
  | :channel_create
  | :channel_update
  | :channel_delete
  | :channel_info
  | :channel_pins_update
  | :thread_create
  | :thread_update
  | :thread_delete
  | :thread_list_sync
  | :thread_member_update
  | :thread_members_update
  | :entitlement_create
  | :entitlement_update
  | :entitlement_delete
  | :guild_create
  | :guild_update
  | :guild_delete
  | :guild_audit_log_entry_create
  | :guild_ban_add
  | :guild_ban_remove
  | :guild_emojis_update
  | :guild_stickers_update
  | :guild_integrations_update
  | :guild_member_add
  | :guild_member_update
  | :guild_member_remove
  | :guild_members_chunk
  | :guild_role_create
  | :guild_role_update
  | :guild_role_delete
  | :guild_scheduled_event_create
  | :guild_scheduled_event_update
  | :guild_scheduled_event_delete
  | :guild_scheduled_event_user_add
  | :guild_scheduled_event_user_remove
  | :guild_soundboard_sound_create
  | :guild_soundboard_sound_update
  | :guild_soundboard_sound_delete
  | :guild_soundboard_sounds_update
  | :soundboard_sounds
  | :integration_create
  | :integration_update
  | :integration_delete
  | :interaction_create
  | :invite_create
  | :invite_delete
  | :message_create
  | :message_update
  | :message_delete
  | :message_delete_bulk
  | :message_reaction_add
  | :message_reaction_remove
  | :message_reaction_remove_all
  | :message_reaction_remove_emoji
  | :message_poll_vote_add
  | :message_poll_vote_remove
  | :presence_update
  | :rate_limited
  | :stage_instance_create
  | :stage_instance_update
  | :stage_instance_delete
  | :subscription_create
  | :subscription_update
  | :subscription_delete
  | :typing_start
  | :user_update
  | :voice_channel_effect_send
  | :voice_channel_status_update
  | :voice_channel_start_time_update
  | :voice_state_update
  | :webhooks_update
  | :application_authorized
  | :application_deauthorized
  | :lobby_message_create
  | :lobby_message_update
  | :lobby_message_delete
  | :game_direct_message_create
  | :game_direct_message_update
  | :game_direct_message_delete
  | :unknown

An event's name.

payload()

@type payload() ::
  Lingo.Event.Ready.t()
  | Lingo.Event.Resumed.t()
  | Lingo.Type.CommandPermissions.t()
  | Lingo.Type.AutoModerationRule.t()
  | update(Lingo.Type.AutoModerationRule.t())
  | Lingo.Event.AutoModerationAction.t()
  | Lingo.Type.Channel.t()
  | update(Lingo.Type.Channel.t())
  | Lingo.Event.ChannelInfo.t()
  | Lingo.Event.ChannelPinsUpdate.t()
  | Lingo.Event.ThreadDelete.t()
  | Lingo.Event.ThreadListSync.t()
  | Lingo.Type.ThreadMember.t()
  | Lingo.Event.ThreadMembersUpdate.t()
  | Lingo.Type.Entitlement.t()
  | update(Lingo.Type.Entitlement.t())
  | Lingo.Type.Guild.t()
  | update(Lingo.Type.Guild.t())
  | Lingo.Event.GuildDelete.t()
  | Lingo.Type.AuditLogEntry.t()
  | Lingo.Event.GuildBan.t()
  | Lingo.Event.GuildEmojisUpdate.t()
  | Lingo.Event.GuildStickersUpdate.t()
  | Lingo.Event.GuildIntegrationsUpdate.t()
  | Lingo.Type.Member.t()
  | update(Lingo.Type.Member.t())
  | Lingo.Event.GuildMemberRemove.t()
  | Lingo.Event.GuildMembersChunk.t()
  | Lingo.Type.Role.t()
  | update(Lingo.Type.Role.t())
  | Lingo.Event.GuildRoleDelete.t()
  | Lingo.Type.ScheduledEvent.t()
  | update(Lingo.Type.ScheduledEvent.t())
  | Lingo.Event.ScheduledEventUser.t()
  | Lingo.Type.SoundboardSound.t()
  | update(Lingo.Type.SoundboardSound.t())
  | Lingo.Event.SoundboardSoundDelete.t()
  | Lingo.Event.SoundboardSounds.t()
  | Lingo.Type.Integration.t()
  | update(Lingo.Type.Integration.t())
  | Lingo.Event.IntegrationDelete.t()
  | Lingo.Type.Interaction.t()
  | Lingo.Type.Invite.t()
  | Lingo.Event.InviteDelete.t()
  | Lingo.Type.Message.t()
  | update(Lingo.Type.Message.t())
  | Lingo.Event.MessageDelete.t()
  | Lingo.Event.MessageDeleteBulk.t()
  | Lingo.Event.Reaction.t()
  | Lingo.Event.ReactionRemoveAll.t()
  | Lingo.Event.PollVote.t()
  | update(Lingo.Type.Presence.t())
  | Lingo.Event.RateLimited.t()
  | Lingo.Type.StageInstance.t()
  | update(Lingo.Type.StageInstance.t())
  | Lingo.Type.Subscription.t()
  | update(Lingo.Type.Subscription.t())
  | Lingo.Event.TypingStart.t()
  | update(Lingo.Type.User.t())
  | Lingo.Event.VoiceChannelEffect.t()
  | Lingo.Event.VoiceChannelStatus.t()
  | Lingo.Event.VoiceChannelStartTime.t()
  | update(Lingo.Type.VoiceState.t())
  | Lingo.Event.WebhooksUpdate.t()
  | Lingo.Event.ApplicationAuthorized.t()
  | Lingo.Event.ApplicationDeauthorized.t()
  | Lingo.Type.LobbyMessage.t()
  | update(Lingo.Type.LobbyMessage.t())
  | Lingo.Event.LobbyMessageDelete.t()
  | Lingo.Event.Unknown.t()

An event's payload. The table above says which goes with which name.

update(t)

@type update(t) :: %{old: t | nil, new: t}

An update's payload: the resource before (from the cache, or nil) and after.