Class: Discordrb::Events::MessageEvent

Inherits:
Event
  • Object
show all
Includes:
Respondable
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

Methods included from Respondable

#<<, #drain, #drain_into, #send_embed, #send_message, #send_temporary_message

Constructor Details

#initialize(message, bot) ⇒ MessageEvent


107
108
109
110
111
112
113
# File 'lib/discordrb/events/message.rb', line 107

def initialize(message, bot)
  @bot = bot
  @message = message
  @channel = message.channel
  @saved_message = ''
  @file = nil
end

Instance Attribute Details

#authorMember, User (readonly) Also known as: user

Returns who sent this message.

See Also:


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

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

#channelChannel (readonly)

Returns the channel in which this message was sent.

See Also:


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

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

#contentString (readonly) Also known as: text

Returns the message's content.

See Also:


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

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

#fileFile (readonly)


86
87
88
# File 'lib/discordrb/events/message.rb', line 86

def file
  @file
end

#messageMessage (readonly)


80
81
82
# File 'lib/discordrb/events/message.rb', line 80

def message
  @message
end

#saved_messageString (readonly)


83
84
85
# File 'lib/discordrb/events/message.rb', line 83

def saved_message
  @saved_message
end

#serverServer? (readonly)

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

See Also:


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

delegate :server, to: :channel

#timestampTime (readonly)

Returns the time at which the message was sent.

See Also:


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

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

Instance Method Details

#attach_file(file) ⇒ Object

Attaches a file to the message event and converts the message into a caption.

Raises:

  • (ArgumentError)

130
131
132
133
134
# File 'lib/discordrb/events/message.rb', line 130

def attach_file(file)
  raise ArgumentError, 'Argument is not a file!' unless file.is_a?(File)
  @file = file
  nil
end

#detach_fileObject

Detaches a file from the message event.


137
138
139
# File 'lib/discordrb/events/message.rb', line 137

def detach_file
  @file = nil
end

#from_bot?true, false


142
143
144
# File 'lib/discordrb/events/message.rb', line 142

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

#send_file(file, caption: nil) ⇒ Discordrb::Message

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

Examples:

Send a file from disk

event.send_file(File.open('rubytaco.png', 'r'))

123
124
125
# File 'lib/discordrb/events/message.rb', line 123

def send_file(file, caption: nil)
  @message.channel.send_file(file, caption: caption)
end

#voiceVoiceBot?

Utility method to get the voice bot for the current server


148
149
150
# File 'lib/discordrb/events/message.rb', line 148

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