Nostrum.ConsumerGroup (nostrum v0.11.0-dev)

View Source

Registers consumers and handles event dispatch.

Summary

Functions

Stop monitoring the given reference.

Dispatch the given event(s) to all consumers.

Equivalent to ConsumerGroup.join(self()). See join/1.

Join the given process to the consumers.

Monitor the consumer group for changes.

Functions

child_spec(opts)

(since 0.7.0)

demonitor(ref)

(since 0.9.0)
@spec demonitor(reference()) :: :ok | false

Stop monitoring the given reference.

dispatch(event)

(since 0.7.0)
@spec dispatch([Nostrum.Consumer.event(), ...]) :: :ok
@spec dispatch(Nostrum.Consumer.event()) :: :ok

Dispatch the given event(s) to all consumers.

This is called by nostrum internally, you likely won't need to call this manually.

join()

(since 0.7.0)
@spec join() :: :ok

Equivalent to ConsumerGroup.join(self()). See join/1.

join(pid)

(since 0.7.0)
@spec join(pid()) :: :ok

Join the given process to the consumers.

If no process is given, joins the current process to the consumers. This can be used for subscribing to gateway events and awaiting them inline.

After the process has joined, it will receive any events sent by nostrum's gateway dispatch. These events are sent as messages {:event, t:Consumer.Event.t/0}. The given pid is automatically unsubscribed when it terminates.

Note that there is currently no filtering done. If the gateway sends a lot of messages and the event subscriber does not terminate swiftly, its message queue will keep growing.

Example

The following example illustrates how to use this to implement inline event awaiting:

defmodule MyBot.Command
  alias Nostrum.Api.Message
  alias Nostrum.ConsumerGroup
  alias Nostrum.Struct.Message
  alias Nostrum.Struct.User

  def command(%Message{author: %User{id: author_id}}) do
    Message.create(msg, "Reply 'y' in 5 seconds to confirm ordering a large burger menu.")
    ConsumerGroup.join()
    receive do
      {:event, {:MESSAGE_CREATE, %Message{author: %User{id: author_id}, content: "y"}, _}} ->
        Message.create(msg, "The large burger menu is coming.")
    after
      5_000 ->
        Message.create(msg, "Too slow!")
    end
  end
end

monitor()

(since 0.9.0)
@spec monitor() :: {reference(), [pid()]}

Monitor the consumer group for changes.

Return the initial state of the group on first call. Further updates are delivered as messages to the calling process, see :pg.monitor/2 for details. The returned reference/0 must be saved for later calls to demonitor/1.

start_link(opts)

(since 0.7.0)