Module: OnyxCord::Bot::Messaging
- Included in:
- OnyxCord::Bot
- Defined in:
- lib/onyxcord/core/bot/messaging.rb
Instance Method Summary collapse
-
#add_thread_member(channel, member) ⇒ Object
Add a member to a thread.
-
#join_thread(channel) ⇒ Object
Join a thread.
-
#leave_thread(channel) ⇒ Object
Leave a thread.
-
#parse_mention(mention, server = nil) ⇒ User, ...
Gets the user, channel, role or emoji from a string.
-
#parse_mentions(mentions, server = nil) ⇒ Array<User, Channel, Role, Emoji>
Gets the users, channels, roles and emoji from a string.
-
#prune_empty_groups ⇒ Object
Makes the bot leave any groups with no recipients remaining.
-
#remove_thread_member(channel, member) ⇒ Object
Remove a member from a thread.
-
#send_file(channel, file, caption: nil, tts: false, filename: nil, spoiler: nil) ⇒ Object
Sends a file to a channel.
-
#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.
-
#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.
Instance Method Details
#add_thread_member(channel, member) ⇒ Object
Add a member to a thread
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
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
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.
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.
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_groups ⇒ Object
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
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
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.
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.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/onyxcord/core/bot/messaging.rb', line 19 def (channel, content, tts = false, = nil, = nil, allowed_mentions = nil, = 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_id: .resolve_id } if .respond_to?(:resolve_id) = (.instance_of?(Array) ? .map(&:to_hash) : [&.to_hash]).compact flags = OnyxCord::MessageComponents.apply_v2_flag(flags, components) response = REST::Channel.(token, channel, content, tts, , nonce, , allowed_mentions&.to_hash, , 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.
46 47 48 49 50 51 52 53 54 |
# File 'lib/onyxcord/core/bot/messaging.rb', line 46 def (channel, content, timeout, tts = false, = nil, = nil, allowed_mentions = nil, = nil, components = nil, flags = 0, nonce = nil, enforce_nonce = false, poll = nil) Internal::AsyncRuntime.async do = (channel, content, tts, , , allowed_mentions, , components, flags, nonce, enforce_nonce, poll) Internal::AsyncRuntime.sleep(timeout) .delete end nil end |