Class: Discordrb::Events::MessageEvent

Inherits:
Event
  • Object
show all
Defined in:
lib/discordrb/events/message.rb

Overview

Event raised when a text message is sent to a channel

Instance Attribute Summary collapse

Attributes inherited from Event

#bot

Instance Method Summary collapse

Constructor Details

#initialize(message, bot) ⇒ MessageEvent

Returns a new instance of MessageEvent.



33
34
35
36
37
# File 'lib/discordrb/events/message.rb', line 33

def initialize(message, bot)
  @bot = bot
  @message = message
  @saved_message = ''
end

Instance Attribute Details

#authorUser (readonly) Also known as: user

Returns who sent this message.

Returns:

  • (User)

    who sent this message.

See Also:



26
# File 'lib/discordrb/events/message.rb', line 26

delegate :author, :channel, :content, :timestamp, to: :message

#channelChannel (readonly)

Returns the channel in which this message was sent.

Returns:

  • (Channel)

    the channel in which this message was sent.

See Also:



26
# File 'lib/discordrb/events/message.rb', line 26

delegate :author, :channel, :content, :timestamp, to: :message

#contentString (readonly) Also known as: text

Returns the message's content.

Returns:

  • (String)

    the message's content.

See Also:



26
# File 'lib/discordrb/events/message.rb', line 26

delegate :author, :channel, :content, :timestamp, to: :message

#messageMessage (readonly)

Returns the message which triggered this event.

Returns:

  • (Message)

    the message which triggered this event.



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

def message
  @message
end

#saved_messageString (readonly)

Returns the message that has been saved by calls to #<< and will be sent to Discord upon completion.

Returns:

  • (String)

    the message that has been saved by calls to #<< and will be sent to Discord upon completion.



12
13
14
# File 'lib/discordrb/events/message.rb', line 12

def saved_message
  @saved_message
end

#serverServer? (readonly)

Returns the server where this message was sent, or nil if it was sent in PM.

Returns:

  • (Server, nil)

    the server where this message was sent, or nil if it was sent in PM.

See Also:



31
# File 'lib/discordrb/events/message.rb', line 31

delegate :server, to: :channel

#timestampTime (readonly)

Returns the time at which the message was sent.

Returns:

  • (Time)

    the time at which the message was sent.

See Also:



26
# File 'lib/discordrb/events/message.rb', line 26

delegate :author, :channel, :content, :timestamp, to: :message

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



62
63
64
65
# File 'lib/discordrb/events/message.rb', line 62

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

#from_bot?true, false

Returns whether or not this message was sent by the bot itself.

Returns:

  • (true, false)

    whether or not this message was sent by the bot itself



48
49
50
# File 'lib/discordrb/events/message.rb', line 48

def from_bot?
  @message.user.id == @bot.profile.id
end

#send_message(content) ⇒ 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

Returns:



43
44
45
# File 'lib/discordrb/events/message.rb', line 43

def send_message(content)
  @message.channel.send_message(content)
end

#voiceVoiceBot?

Utility method to get the voice bot for the current server

Returns:

  • (VoiceBot, nil)

    the voice bot connected to this message's server, or nil if there is none connected



54
55
56
# File 'lib/discordrb/events/message.rb', line 54

def voice
  @bot.voice(@message.channel.server.id)
end