Module: Discordrb::Events::Respondable

Included in:
MessageEvent, MessageIDEvent, ReactionEvent, ReactionRemoveAllEvent, TypingEvent
Defined in:
lib/discordrb/events/message.rb

Overview

Module to make sending messages easier with the presence of a text channel in an event

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#channelChannel (readonly)

Returns the channel in which this event occurred.

Returns:

  • (Channel)

    the channel in which this event occurred



10
11
12
# File 'lib/discordrb/events/message.rb', line 10

def channel
  @channel
end

Instance Method Details

#<<(message) ⇒ Object

Adds a string to be sent after the event has finished execution. Avoids problems with rate limiting because only one message is ever sent. If it is used multiple times, the strings will bunch up into one message (separated by newlines)

Parameters:

  • message (String)

    The message to send to the channel



58
59
60
61
62
# File 'lib/discordrb/events/message.rb', line 58

def <<(message)
  addition = "#{message}\n"
  @saved_message = @saved_message ? @saved_message + addition : addition
  nil
end

#drainObject

Drains the currently saved message, which clears it out, resulting in everything being saved before being thrown away and nothing being sent to the channel (unless there is something saved after this).

See Also:



67
68
69
70
# File 'lib/discordrb/events/message.rb', line 67

def drain
  @saved_message = ''
  nil
end

#drain_into(result) ⇒ String

Drains the currently saved message into a result string. This prepends it before that string, clears the saved message and returns the concatenation.

Parameters:

  • result (String)

    The result string to drain into.

Returns:

  • (String)

    a string formed by concatenating the saved message and the argument.



76
77
78
79
80
81
82
# File 'lib/discordrb/events/message.rb', line 76

def drain_into(result)
  return if result.is_a?(Discordrb::Message)

  result = (@saved_message.nil? ? '' : @saved_message.to_s) + (result.nil? ? '' : result.to_s)
  drain
  result
end

#send_embed(message = '', embed = nil, attachments = nil, tts = false, allowed_mentions = nil, message_reference = nil, components = nil) {|embed| ... } ⇒ Message

The same as #send_message, but yields a Webhooks::Embed for easy building of embedded content inside a block.

Parameters:

  • message (String) (defaults to: '')

    The message that should be sent along with the embed. If this is the empty string, only the embed will be shown.

  • embed (Discordrb::Webhooks::Embed, nil) (defaults to: nil)

    The embed to start the building process with, or nil if one should be created anew.

  • attachments (Array<File>) (defaults to: nil)

    Files that can be referenced in embeds via attachment://file.png

  • tts (true, false) (defaults to: false)

    Whether or not this message should be sent using Discord text-to-speech.

  • allowed_mentions (Hash, Discordrb::AllowedMentions, false, nil) (defaults to: nil)

    Mentions that are allowed to ping on this message. false disables all pings

  • message_reference (Message, String, Integer, nil) (defaults to: nil)

    The message, or message ID, to reply to if any.

  • components (View, Array<Hash>, nil) (defaults to: nil)

    A collection of components to attach to the message.

Yields:

  • (embed)

    Yields the embed to allow for easy building inside a block.

Yield Parameters:

Returns:

  • (Message)

    The resulting message.

See Also:



38
39
40
# File 'lib/discordrb/events/message.rb', line 38

def send_embed(message = '', embed = nil, attachments = nil, tts = false, allowed_mentions = nil, message_reference = nil, components = nil, &block)
  channel.send_embed(message, embed, attachments, tts, allowed_mentions, message_reference, components, &block)
end

#send_message(content, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil) ⇒ Discordrb::Message Also known as: send, respond

Sends a message to the channel this message was sent in, right now. It is usually preferable to use #<< instead because it avoids rate limiting problems

Parameters:

  • content (String)

    The message to send to the channel

  • tts (true, false) (defaults to: false)

    Whether or not this message should be sent using Discord text-to-speech.

  • embed (Hash, Discordrb::Webhooks::Embed, nil) (defaults to: nil)

    The rich embed to append to this message.

  • attachments (Array<File>) (defaults to: nil)

    Files that can be referenced in embeds via attachment://file.png

  • allowed_mentions (Hash, Discordrb::AllowedMentions, false, nil) (defaults to: nil)

    Mentions that are allowed to ping on this message. false disables all pings

  • message_reference (Message, String, Integer, nil) (defaults to: nil)

    The message, or message ID, to reply to if any.

  • components (View, Array<Hash>, nil) (defaults to: nil)

    A collection of components to attach to the message.

Returns:



22
23
24
# File 'lib/discordrb/events/message.rb', line 22

def send_message(content, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil)
  channel.send_message(content, tts, embed, attachments, allowed_mentions, message_reference, components)
end

#send_temporary_message(content, timeout, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, components = nil) ⇒ Object Also known as: send_temp

Sends a temporary message to the channel this message was sent in, right now.

Parameters:

  • content (String)

    The content to send. Should not be longer than 2000 characters or it will result in an error.

  • timeout (Float)

    The amount of time in seconds after which the message sent will be deleted.

  • tts (true, false) (defaults to: false)

    Whether or not this message should be sent using Discord text-to-speech.

  • embed (Hash, Discordrb::Webhooks::Embed, nil) (defaults to: nil)

    The rich embed to append to this message.

  • attachments (Array<File>) (defaults to: nil)

    Files that can be referenced in embeds via attachment://file.png

  • allowed_mentions (Hash, Discordrb::AllowedMentions, false, nil) (defaults to: nil)

    Mentions that are allowed to ping on this message. false disables all pings

  • components (View, Array<Hash>, nil) (defaults to: nil)

    A collection of components to attach to the message.



50
51
52
# File 'lib/discordrb/events/message.rb', line 50

def send_temporary_message(content, timeout, tts = false, embed = nil, attachments = nil, allowed_mentions = nil, components = nil)
  channel.send_temporary_message(content, timeout, tts, embed, attachments, allowed_mentions, components)
end