Module: OnyxCord::Bot::Messaging

Included in:
OnyxCord::Bot
Defined in:
lib/onyxcord/core/bot/messaging.rb

Instance Method Summary collapse

Instance Method Details

#add_thread_member(channel, member) ⇒ Object

Add a member to a thread

Parameters:



140
141
142
143
# File 'lib/onyxcord/core/bot/messaging.rb', line 140

def add_thread_member(channel, member)
  REST::Channel.add_thread_member(@token, channel.resolve_id, member.resolve_id)
  nil
end

#join_thread(channel) ⇒ Object

Join a thread

Parameters:



125
126
127
128
# File 'lib/onyxcord/core/bot/messaging.rb', line 125

def join_thread(channel)
  REST::Channel.join_thread(@token, channel.resolve_id)
  nil
end

#leave_thread(channel) ⇒ Object

Leave a thread

Parameters:



132
133
134
135
# File 'lib/onyxcord/core/bot/messaging.rb', line 132

def leave_thread(channel)
  REST::Channel.leave_thread(@token, channel.resolve_id)
  nil
end

#parse_mention(mention, server = nil) ⇒ User, ...

Gets the user, channel, role or emoji from a string.

Parameters:

  • mention (String)

    The mention, which should look like <@12314873129>, <#123456789>, <@&123456789> or <:name:126328:>.

  • server (Server, nil) (defaults to: nil)

    The server of the associated mention. (recommended for role parsing, to speed things up)

Returns:

  • (User, Channel, Role, Emoji)

    The user, channel, role or emoji identified by the mention, or nil if none exists.



119
120
121
# File 'lib/onyxcord/core/bot/messaging.rb', line 119

def parse_mention(mention, server = nil)
  parse_mentions(mention, server).first
end

#parse_mentions(mentions, server = nil) ⇒ Array<User, Channel, Role, Emoji>

Gets the users, channels, roles and emoji from a string.

Parameters:

  • mentions (String)

    The mentions, which should look like <@12314873129>, <#123456789>, <@&123456789> or <:name:126328:>.

  • server (Server, nil) (defaults to: nil)

    The server of the associated mentions. (recommended for role parsing, to speed things up)

Returns:

  • (Array<User, Channel, Role, Emoji>)

    The array of users, channels, roles and emoji identified by the mentions, or nil if none exists.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/onyxcord/core/bot/messaging.rb', line 85

def parse_mentions(mentions, server = nil)
  array_to_return = []
  # While possible mentions may be in message
  while mentions.include?('<') && mentions.include?('>')
    # Removing all content before the next possible mention
    mentions = mentions.split('<', 2)[1]
    # Locate the first valid mention enclosed in `<...>`, otherwise advance to the next open `<`
    next unless mentions.split('>', 2).first.length < mentions.split('<', 2).first.length

    # Store the possible mention value to be validated with RegEx
    mention = mentions.split('>', 2).first
    if /@!?(?<id>\d+)/ =~ mention
      array_to_return << user(id) unless user(id).nil?
    elsif /#(?<id>\d+)/ =~ mention
      array_to_return << channel(id, server) unless channel(id, server).nil?
    elsif /@&(?<id>\d+)/ =~ mention
      if server
        array_to_return << server.role(id) unless server.role(id).nil?
      else
        @servers.each_value do |element|
          array_to_return << element.role(id) unless element.role(id).nil?
        end
      end
    elsif /(?<animated>^a|^${0}):(?<name>\w+):(?<id>\d+)/ =~ mention
      array_to_return << (emoji(id) || Emoji.new({ 'animated' => animated != '', 'name' => name, 'id' => id }, self, nil))
    end
  end
  array_to_return
end

#prune_empty_groupsObject

Makes the bot leave any groups with no recipients remaining



154
155
156
157
158
# File 'lib/onyxcord/core/bot/messaging.rb', line 154

def prune_empty_groups
  @channels.each_value do |channel|
    channel.leave_group if channel.group? && channel.recipients.empty?
  end
end

#remove_thread_member(channel, member) ⇒ Object

Remove a member from a thread

Parameters:



148
149
150
151
# File 'lib/onyxcord/core/bot/messaging.rb', line 148

def remove_thread_member(channel, member)
  REST::Channel.remove_thread_member(@token, channel.resolve_id, member.resolve_id)
  nil
end

#send_file(channel, file, caption: nil, tts: false, filename: nil, spoiler: nil) ⇒ Object

Note:

This executes in a blocking way, so if you're sending long files, be wary of delays.

Sends a file to a channel. If it is an image, it will automatically be embedded.

Examples:

Send a file from disk

bot.send_file(83281822225530880, File.open('rubytaco.png', 'r'))

Parameters:

  • channel (Channel, String, Integer)

    The channel, or its ID, to send something to.

  • file (File)

    The file that should be sent.

  • caption (string) (defaults to: nil)

    The caption for the file.

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

    Whether or not this file's caption should be sent using Discord text-to-speech.

  • 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.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/onyxcord/core/bot/messaging.rb', line 66

def send_file(channel, file, caption: nil, tts: false, filename: nil, spoiler: nil)
  if file.respond_to?(:read)
    if spoiler
      filename ||= File.basename(file.path)
      filename = "SPOILER_#{filename}" unless filename.start_with? 'SPOILER_'
    end
    file.define_singleton_method(:original_filename) { filename } if filename
    file.define_singleton_method(:path) { filename } if filename
  end

  channel = channel.resolve_id
  response = REST::Channel.upload_file(token, channel, file, caption: caption, tts: tts)
  Message.new(OnyxCord::Internal::JSON.parse(response), self)
end

#send_message(channel, content, tts = false, embeds = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = 0, nonce = nil, enforce_nonce = false, poll = nil) ⇒ Message

Sends a text message to a channel given its ID and the message's content.

Parameters:

  • channel (Channel, String, Integer)

    The channel, or its ID, to send something to.

  • content (String)

    The text that should be sent as a message. It is limited to 2000 characters (Discord imposed).

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

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

  • embeds (Hash, OnyxCord::Webhooks::Embed, Array<Hash>, Array<OnyxCord::Webhooks::Embed> nil) (defaults to: nil)

    The rich embed(s) to append to this message.

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

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

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

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

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

    Interaction components to associate with this message.

  • flags (Integer) (defaults to: 0)

    Flags for this message. Currently only SUPPRESS_EMBEDS (1 << 2), SUPPRESS_NOTIFICATIONS (1 << 12), and IS_COMPONENTS_V2 (1 << 15) can be set.

  • nonce (String, nil) (defaults to: nil)

    A optional nonce in order to verify that a message was sent. Maximum of twenty-five characters.

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

    Whether the nonce should be enforced and used for message de-duplication.

  • poll (Hash, Poll::Builder, Poll, nil) (defaults to: nil)

    The poll that should be attached to this message.

Returns:

  • (Message)

    The message that was sent.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/onyxcord/core/bot/messaging.rb', line 19

def send_message(channel, content, tts = false, embeds = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = 0, nonce = nil, enforce_nonce = false, poll = nil)
  channel = channel.resolve_id
  debug("Sending message to #{channel} with content '#{content}'")
  allowed_mentions = { parse: [] } if allowed_mentions == false
  message_reference = { message_id: message_reference.resolve_id } if message_reference.respond_to?(:resolve_id)
  embeds = (embeds.instance_of?(Array) ? embeds.map(&:to_hash) : [embeds&.to_hash]).compact
  flags = OnyxCord::MessageComponents.apply_v2_flag(flags, components)

  response = REST::Channel.create_message(token, channel, content, tts, embeds, nonce, attachments, allowed_mentions&.to_hash, message_reference, components, flags, enforce_nonce, poll&.to_h)
  Message.new(OnyxCord::Internal::JSON.parse(response), self)
end

#send_temporary_message(channel, content, timeout, tts = false, embeds = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = 0, nonce = nil, enforce_nonce = false, poll = nil) ⇒ Object

Sends a text message to a channel given its ID and the message's content, then deletes it after the specified timeout in seconds.

Parameters:

  • channel (Channel, String, Integer)

    The channel, or its ID, to send something to.

  • content (String)

    The text that should be sent as a message. It is limited to 2000 characters (Discord imposed).

  • 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.

  • embeds (Hash, OnyxCord::Webhooks::Embed, Array<Hash>, Array<OnyxCord::Webhooks::Embed> nil) (defaults to: nil)

    The rich embed(s) 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, OnyxCord::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>) (defaults to: nil)

    Interaction components to associate with this message.

  • flags (Integer) (defaults to: 0)

    Flags for this message. Currently only SUPPRESS_EMBEDS (1 << 2), SUPPRESS_NOTIFICATIONS (1 << 12), and IS_COMPONENTS_V2 (1 << 15) can be set.

  • nonce (String, nil) (defaults to: nil)

    A optional nonce in order to verify that a message was sent. Maximum of twenty-five characters.

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

    Whether the nonce should be enforced and used for message de-duplication.

  • poll (Hash, Poll::Builder, Poll, nil) (defaults to: nil)

    The poll that should be attached to this message.



46
47
48
49
50
51
52
53
54
# File 'lib/onyxcord/core/bot/messaging.rb', line 46

def send_temporary_message(channel, content, timeout, tts = false, embeds = nil, attachments = nil, allowed_mentions = nil, message_reference = nil, components = nil, flags = 0, nonce = nil, enforce_nonce = false, poll = nil)
  Internal::AsyncRuntime.async do
    message = send_message(channel, content, tts, embeds, attachments, allowed_mentions, message_reference, components, flags, nonce, enforce_nonce, poll)
    Internal::AsyncRuntime.sleep(timeout)
    message.delete
  end

  nil
end