module Discordrb

All discordrb functionality, to be extended by other files

This file uses code from Websocket::Client::Simple, licensed under the following license:

Copyright © 2013-2014 Sho Hashimoto

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish,

distribute, sublicense, and/or sell copies of the Software, and to

permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

Constants

ALL_INTENTS

All available intents

CHARACTER_LIMIT

The maximum length a Discord message can have

ColorRGB

A colour (red, green and blue values). Used for role colours. If you prefer the American spelling, the alias {ColorRGB} is also available.

DISCORD_EPOCH

The Unix timestamp Discord IDs are based on

INTENTS

Used to declare what events you wish to recieve from Discord. @see discord.com/developers/docs/topics/gateway#gateway-intents

LOGGER

The default debug logger used by discordrb.

LOG_TIMESTAMP_FORMAT

The format log timestamps should be in, in strftime format

NO_INTENTS

No intents

TIMESTAMP_STYLES

For creating timestamps with {timestamp} @see discord.com/developers/docs/reference#message-formatting-timestamp-styles

UNPRIVILEGED_INTENTS

All unprivileged intents @see discord.com/developers/docs/topics/gateway#privileged-intents

VERSION

The current version of discordrb.

Public Class Methods

id_compare(one_id, other) click to toggle source

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

# File lib/discordrb.rb, line 49
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) click to toggle source

Splits a message into chunks of 2000 characters. Attempts to split by lines if possible. @param msg [String] The message to split. @return [Array<String>] the message split into chunks

# File lib/discordrb.rb, line 71
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).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 the largest chunk without
  # cutting words apart, breaking on the nearest space within character limit, otherwise just return an array with one element
  ideal_ary = ideal.length > CHARACTER_LIMIT ? ideal.split(/(.{1,#{CHARACTER_LIMIT}}\b|.{1,#{CHARACTER_LIMIT}})/o).reject(&:empty?) : [ideal]

  # Slice off the ideal part and strip newlines
  rest = msg[ideal.length..].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
timestamp(time, style = nil) click to toggle source

@param time [Time, Integer] The time to create the timestamp from, or a unix timestamp integer. @param style [Symbol, String] One of the keys from {TIMESTAMP_STYLES} or a string with the style. @return [String] @example

Discordrb.timestamp(Time.now, :short_time)
# => "<t:1632146954:t>"
# File lib/discordrb.rb, line 113
def self.timestamp(time, style = nil)
  if style.nil?
    "<t:#{time.to_i}>"
  else
    "<t:#{time.to_i}:#{TIMESTAMP_STYLES[style] || style}>"
  end
end