Alchemy.Cogs.wait_for

You're seeing just the macro wait_for, go back to Alchemy.Cogs module for more information.
Link to this macro

wait_for(type, fun)

(macro)

Halts the current command until an event is received.

The event type is an item corresponding to the events in Alchemy.Events, i.e. on_message_edit -> Cogs.wait_for(:message_edit, ...). The fun is the function that gets called with the relevant event arguments; see Alchemy.Events for more info on what events have what arguments.

The :message event is a bit special, as it will specifically wait for a message not triggered by a bot, in that specific channel, unlike other events, which trigger generically across the entire bot.

The process will kill itself if it doesn't receive any such event for 20s.

Examples

Cogs.def color do
  Cogs.say "What's your favorite color?"
  Cogs.wait_for :message, fn msg ->
    Cogs.say "#{msg.content} is my favorite color too!"
  end
end
Cogs.def typing do
  Cogs.say "I'm waiting for someone to type.."
  Cogs.wait_for :typing, fn _,_,_ ->
    Cogs.say "Someone somewhere started typing..."
  end
Link to this macro

wait_for(type, condition, fun)

(macro)

Waits for a specific event satisfying a condition.

Same as wait_for/2, except this takes an extra condition that needs to be met for the waiting to handle to trigger.

Examples

Cogs.def foo do
  Cogs.say "Send me foo"
  Cogs.wait_for(:message, & &1.content == "foo", fn _msg ->
    Cogs.say "Nice foo man!"
  end)

Note that, if no event of the given type is received after 20s, the process will kill itself, it's possible that this will never get met, but no event satisfying the condition will ever arrive, essentially rendering the process a waste. To circumvent this, it might be smart to send a preemptive kill message:

self = self()
Task.start(fn ->
  Process.sleep(20_000)
  Process.exit(self, :kill)
)
Cogs.wait_for(:message, fn x -> false end, fn _msg ->
  Cogs.say "If you hear this, logic itself is falling apart!!!"
end)