Module: Discordrb

Defined in:
lib/discordrb.rb,
lib/discordrb/bot.rb,
lib/discordrb/data.rb,
lib/discordrb/await.rb,
lib/discordrb/cache.rb,
lib/discordrb/errors.rb,
lib/discordrb/logger.rb,
lib/discordrb/gateway.rb,
lib/discordrb/version.rb,
lib/discordrb/webhooks.rb,
lib/discordrb/container.rb,
lib/discordrb/permissions.rb,
lib/discordrb/websocket.rb

Overview

Discordrb and all its functionality, in this case only the version.

Defined Under Namespace

Modules: API, Cache, Commands, Errors, EventContainer, Events, IDObject, Light, MemberAttributes, Opcodes, PermissionCalculator, ServerAttributes, UserAttributes, Voice, Webhooks Classes: Application, Attachment, Await, Bot, Channel, ColourRGB, Embed, EmbedAuthor, EmbedProvider, EmbedThumbnail, Emoji, Gateway, GlobalEmoji, Integration, IntegrationAccount, Invite, InviteChannel, InviteServer, Logger, Member, Message, Permissions, Profile, Reaction, Recipient, Role, Server, Session, User, VoiceState, WebSocket

Constant Summary collapse

LOGGER =

The default debug logger used by discordrb.

Logger.new(ENV['DISCORDRB_FANCY_LOG'])
DISCORD_EPOCH =

The unix timestamp Discord IDs are based on

1_420_070_400_000
CHARACTER_LIMIT =

The maximum length a Discord message can have

2000
ColorRGB =

Alias for the class ColourRGB

ColourRGB
LOG_TIMESTAMP_FORMAT =

The format log timestamps should be in, in strftime format

'%Y-%m-%d %H:%M:%S.%L'.freeze
VERSION =

The current version of discordrb.

'3.2.0.1'.freeze

Class Method Summary collapse

Class Method Details

.id_compare(one_id, other) ⇒ Object

Compares two objects based on IDs - either the objects' IDs are equal, or one object is equal to the other's ID.



23
24
25
# File 'lib/discordrb/data.rb', line 23

def self.id_compare(one_id, other)
  other.respond_to?(:resolve_id) ? (one_id.resolve_id == other.resolve_id) : (one_id == other)
end

.split_message(msg) ⇒ Array<String>

Splits a message into chunks of 2000 characters. Attempts to split by lines if possible.

Parameters:

  • msg (String)

    The message to split.

Returns:

  • (Array<String>)

    the message split into chunks



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/discordrb/data.rb', line 33

def self.split_message(msg)
  # If the messages is empty, return an empty array
  return [] if msg.empty?

  # Split the message into lines
  lines = msg.lines

  # Turn the message into a "triangle" of consecutively longer slices, for example the array [1,2,3,4] would become
  # [
  #  [1],
  #  [1, 2],
  #  [1, 2, 3],
  #  [1, 2, 3, 4]
  # ]
  tri = [*0..(lines.length - 1)].map { |i| lines.combination(i + 1).first }

  # Join the individual elements together to get an array of strings with consecutively more lines
  joined = tri.map(&:join)

  # Find the largest element that is still below the character limit, or if none such element exists return the first
  ideal = joined.max_by { |e| e.length > CHARACTER_LIMIT ? -1 : e.length }

  # If it's still larger than the character limit (none was smaller than it) split it into slices with the length
  # being the character limit, otherwise just return an array with one element
  ideal_ary = ideal.length > CHARACTER_LIMIT ? ideal.chars.each_slice(CHARACTER_LIMIT).map(&:join) : [ideal]

  # Slice off the ideal part and strip newlines
  rest = msg[ideal.length..-1].strip

  # If none remains, return an empty array -> we're done
  return [] unless rest

  # Otherwise, call the method recursively to split the rest of the string and add it onto the ideal array
  ideal_ary + split_message(rest)
end