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

Returns a new instance of MessageEvent.


113
114
115
116
117
118
119
120
121
# File 'lib/discordrb/events/message.rb', line 113

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

Instance Attribute Details

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

Returns who sent this message.

Returns:

See Also:


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

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:


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

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:


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

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

#fileFile (readonly)

Returns the file that has been saved by a call to #attach_file and will be sent to Discord upon completion.

Returns:

  • (File)

    the file that has been saved by a call to #attach_file and will be sent to Discord upon completion.


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

def file
  @file
end

#file_spoilertrue, false (readonly)

Returns Whether or not this file should appear as a spoiler. Set by #attach_file.

Returns:

  • (true, false)

    Whether or not this file should appear as a spoiler. Set by #attach_file


92
93
94
# File 'lib/discordrb/events/message.rb', line 92

def file_spoiler
  @file_spoiler
end

#filenameString (readonly)

Returns the filename set in #attach_file that will override the original filename when sent.

Returns:

  • (String)

    the filename set in #attach_file that will override the original filename when sent.


89
90
91
# File 'lib/discordrb/events/message.rb', line 89

def filename
  @filename
end

#messageMessage (readonly)

Returns the message which triggered this event.

Returns:

  • (Message)

    the message which triggered this event.


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

def message
  @message
end

#saved_messageString (readonly)

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

Returns:

  • (String)

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


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.

Returns:

  • (Server, nil)

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

See Also:


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

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:


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

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

Instance Method Details

#attach_file(file, filename: nil, spoiler: nil) ⇒ Object

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

Parameters:

  • file (File)

    The file to be attached

  • filename (String) (defaults to: nil)

    Overrides the filename of the uploaded file

  • spoiler (true, false) (defaults to: nil)

    Whether or not this file should appear as a spoiler.

Raises:

  • (ArgumentError)

142
143
144
145
146
147
148
149
# File 'lib/discordrb/events/message.rb', line 142

def attach_file(file, filename: nil, spoiler: nil)
  raise ArgumentError, 'Argument is not a file!' unless file.is_a?(File)

  @file = file
  @filename = filename
  @file_spoiler = spoiler
  nil
end

#detach_fileObject

Detaches a file from the message event.


152
153
154
155
156
# File 'lib/discordrb/events/message.rb', line 152

def detach_file
  @file = nil
  @filename = nil
  @file_spoiler = 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


159
160
161
# File 'lib/discordrb/events/message.rb', line 159

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

#send_file(file, caption: nil, filename: nil, spoiler: 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'))

Parameters:

  • file (File)

    The file to send to the channel

  • caption (String) (defaults to: nil)

    The caption attached to the file

  • filename (String) (defaults to: nil)

    Overrides the filename of the uploaded file

  • spoiler (true, false) (defaults to: nil)

    Whether or not this file should appear as a spoiler.

Returns:


133
134
135
# File 'lib/discordrb/events/message.rb', line 133

def send_file(file, caption: nil, filename: nil, spoiler: nil)
  @message.channel.send_file(file, caption: caption, filename: filename, spoiler: spoiler)
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


165
166
167
# File 'lib/discordrb/events/message.rb', line 165

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