class Discordrb::Events::MessageEvent

Event raised when a text message is sent to a channel

Attributes

file[R]

@return [File] the file that has been saved by a call to {#attach_file} and will be sent to Discord upon completion.

file_spoiler[R]

@return [true, false] Whether or not this file should appear as a spoiler. Set by {#attach_file}

filename[R]

@return [String] the filename set in {#attach_file} that will override the original filename when sent.

message[R]

@return [Message] the message which triggered this event.

saved_message[R]

@return [String] the message that has been saved by calls to {#<<} and will be sent to Discord upon completion.

Public Class Methods

new(message, bot) click to toggle source
# File lib/discordrb/events/message.rb, line 127
def initialize(message, bot)
  @bot = bot
  @message = message
  @channel = message.channel
  @saved_message = ''
  @file = nil
  @filename = nil
  @file_spoiler = nil
end

Public Instance Methods

attach_file(file, filename: nil, spoiler: nil) click to toggle source

Attaches a file to the message event and converts the message into a caption. @param file [File] The file to be attached @param filename [String] Overrides the filename of the uploaded file @param spoiler [true, false] Whether or not this file should appear as a spoiler.

# File lib/discordrb/events/message.rb, line 156
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_file() click to toggle source

Detaches a file from the message event.

# File lib/discordrb/events/message.rb, line 166
def detach_file
  @file = nil
  @filename = nil
  @file_spoiler = nil
end
from_bot?() click to toggle source

@return [true, false] whether or not this message was sent by the bot itself

# File lib/discordrb/events/message.rb, line 173
def from_bot?
  @message.user.id == @bot.profile.id
end
send_file(file, caption: nil, filename: nil, spoiler: nil) click to toggle source

Sends file with a caption to the channel this message was sent in, right now. It is usually preferable to use {#<<} and {#attach_file} instead because it avoids rate limiting problems @param file [File] The file to send to the channel @param caption [String] The caption attached to the file @param filename [String] Overrides the filename of the uploaded file @param spoiler [true, false] Whether or not this file should appear as a spoiler. @return [Discordrb::Message] the message that was sent @example Send a file from disk

event.send_file(File.open('rubytaco.png', 'r'))
# File lib/discordrb/events/message.rb, line 147
def send_file(file, caption: nil, filename: nil, spoiler: nil)
  @message.channel.send_file(file, caption: caption, filename: filename, spoiler: spoiler)
end
voice() click to toggle source

Utility method to get the voice bot for the current server @return [VoiceBot, nil] the voice bot connected to this message’s server, or nil if there is none connected

# File lib/discordrb/events/message.rb, line 179
def voice
  @bot.voice(@message.channel.server.id)
end