Module: Discordrb

Defined in:
lib/discordrb.rb,
lib/discordrb/bot.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/data/role.rb,
lib/discordrb/data/user.rb,
lib/discordrb/id_object.rb,
lib/discordrb/paginator.rb,
lib/discordrb/colour_rgb.rb,
lib/discordrb/data/embed.rb,
lib/discordrb/data/emoji.rb,
lib/discordrb/data/invite.rb,
lib/discordrb/data/member.rb,
lib/discordrb/data/server.rb,
lib/discordrb/permissions.rb,
lib/discordrb/data/channel.rb,
lib/discordrb/data/message.rb,
lib/discordrb/data/profile.rb,
lib/discordrb/data/webhook.rb,
lib/discordrb/data/activity.rb,
lib/discordrb/data/reaction.rb,
lib/discordrb/data/component.rb,
lib/discordrb/data/overwrite.rb,
lib/discordrb/data/recipient.rb,
lib/discordrb/data/attachment.rb,
lib/discordrb/data/audit_logs.rb,
lib/discordrb/allowed_mentions.rb,
lib/discordrb/data/application.rb,
lib/discordrb/data/integration.rb,
lib/discordrb/data/interaction.rb,
lib/discordrb/data/voice_state.rb,
lib/discordrb/webhooks/version.rb,
lib/discordrb/data/voice_region.rb,
lib/discordrb/websocket.rb

Overview

Webhook support for discordrb

Defined Under Namespace

Modules: API, Cache, Commands, Components, Errors, EventContainer, Events, IDObject, Interactions, Light, MemberAttributes, Opcodes, PermissionCalculator, ServerAttributes, UserAttributes, Voice, Webhooks Classes: Activity, ActivitySet, AllowedMentions, Application, ApplicationCommand, Attachment, AuditLogs, Await, Bot, Channel, ColourRGB, Embed, EmbedAuthor, EmbedField, EmbedFooter, EmbedImage, EmbedProvider, EmbedThumbnail, EmbedVideo, Emoji, Gateway, Integration, IntegrationAccount, IntegrationApplication, Interaction, Invite, InviteChannel, InviteServer, Logger, Member, Message, Overwrite, Paginator, Permissions, Profile, Reaction, Recipient, Role, Server, ServerBan, Session, User, VoiceRegion, VoiceState, WebSocket, Webhook

Constant Summary collapse

LOGGER =

The default debug logger used by discordrb.

Logger.new(ENV.fetch('DISCORDRB_FANCY_LOG', false))
DISCORD_EPOCH =

The Unix timestamp Discord IDs are based on

1_420_070_400_000
INTENTS =

Used to declare what events you wish to recieve from Discord.

{
  servers: 1 << 0,
  server_members: 1 << 1,
  server_bans: 1 << 2,
  server_emojis: 1 << 3,
  server_integrations: 1 << 4,
  server_webhooks: 1 << 5,
  server_invites: 1 << 6,
  server_voice_states: 1 << 7,
  server_presences: 1 << 8,
  server_messages: 1 << 9,
  server_message_reactions: 1 << 10,
  server_message_typing: 1 << 11,
  direct_messages: 1 << 12,
  direct_message_reactions: 1 << 13,
  direct_message_typing: 1 << 14
}.freeze
ALL_INTENTS =

All available intents

INTENTS.values.reduce(&:|)
UNPRIVILEGED_INTENTS =

All unprivileged intents

ALL_INTENTS & ~(INTENTS[:server_members] | INTENTS[:server_presences])
NO_INTENTS =

No intents

0
CHARACTER_LIMIT =

The maximum length a Discord message can have

2000
TIMESTAMP_STYLES =

For creating timestamps with timestamp

{
  short_time: 't', # 16:20
  long_time: 'T', # 16:20:30
  short_date: 'd', # 20/04/2021
  long_date: 'D', # 20 April 2021
  short_datetime: 'f', # 20 April 2021 16:20
  long_datetime: 'F', # Tuesday, 20 April 2021 16:20
  relative: 'R' # 2 months ago
}.freeze
LOG_TIMESTAMP_FORMAT =

The format log timestamps should be in, in strftime format

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

The current version of discordrb.

'3.5.0'
ColorRGB =

Alias for the class ColourRGB

ColourRGB

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.



49
50
51
# 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) ⇒ 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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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) ⇒ String

Examples:

Discordrb.timestamp(Time.now, :short_time)
# => "<t:1632146954:t>"

Parameters:

  • time (Time, Integer)

    The time to create the timestamp from, or a unix timestamp integer.

  • style (Symbol, String) (defaults to: nil)

    One of the keys from TIMESTAMP_STYLES or a string with the style.

Returns:



113
114
115
116
117
118
119
# 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