Module: OnyxCord

Defined in:
lib/onyxcord.rb,
lib/onyxcord/bot.rb,
lib/onyxcord/await.rb,
lib/onyxcord/cogs/bot.rb,
lib/onyxcord/cogs/cog.rb,
lib/onyxcord/cogs/cog.rb,
lib/onyxcord/webhooks.rb,
lib/onyxcord/container.rb,
lib/onyxcord/core/errors.rb,
lib/onyxcord/core/logger.rb,
lib/onyxcord/models/call.rb,
lib/onyxcord/models/poll.rb,
lib/onyxcord/models/role.rb,
lib/onyxcord/models/team.rb,
lib/onyxcord/models/user.rb,
lib/onyxcord/core/version.rb,
lib/onyxcord/interactions.rb,
lib/onyxcord/models/embed.rb,
lib/onyxcord/models/emoji.rb,
lib/onyxcord/cache/manager.rb,
lib/onyxcord/cogs/cog_meta.rb,
lib/onyxcord/internal/http.rb,
lib/onyxcord/internal/json.rb,
lib/onyxcord/models/invite.rb,
lib/onyxcord/models/member.rb,
lib/onyxcord/models/server.rb,
lib/onyxcord/core/bot/oauth.rb,
lib/onyxcord/core/bot/voice.rb,
lib/onyxcord/gateway/client.rb,
lib/onyxcord/models/channel.rb,
lib/onyxcord/models/message.rb,
lib/onyxcord/models/profile.rb,
lib/onyxcord/models/webhook.rb,
lib/onyxcord/cogs/extensions.rb,
lib/onyxcord/core/bot/awaits.rb,
lib/onyxcord/internal/upload.rb,
lib/onyxcord/models/activity.rb,
lib/onyxcord/models/reaction.rb,
lib/onyxcord/models/snapshot.rb,
lib/onyxcord/utils/id_object.rb,
lib/onyxcord/utils/paginator.rb,
lib/onyxcord/utils/timestamp.rb,
lib/onyxcord/core/bot/invites.rb,
lib/onyxcord/core/bot/runtime.rb,
lib/onyxcord/models/component.rb,
lib/onyxcord/models/overwrite.rb,
lib/onyxcord/models/recipient.rb,
lib/onyxcord/models/timestamp.rb,
lib/onyxcord/utils/colour_rgb.rb,
lib/onyxcord/webhooks/version.rb,
lib/onyxcord/core/bot/presence.rb,
lib/onyxcord/models/attachment.rb,
lib/onyxcord/models/audit_logs.rb,
lib/onyxcord/utils/permissions.rb,
lib/onyxcord/core/bot/messaging.rb,
lib/onyxcord/core/configuration.rb,
lib/onyxcord/internal/event_bus.rb,
lib/onyxcord/internal/websocket.rb,
lib/onyxcord/models/application.rb,
lib/onyxcord/models/channel_tag.rb,
lib/onyxcord/models/integration.rb,
lib/onyxcord/models/interaction.rb,
lib/onyxcord/models/voice_state.rb,
lib/onyxcord/events/handlers/raw.rb,
lib/onyxcord/interactions/option.rb,
lib/onyxcord/models/collectibles.rb,
lib/onyxcord/models/voice_region.rb,
lib/onyxcord/application_commands.rb,
lib/onyxcord/cache/stores/gateway.rb,
lib/onyxcord/events/handlers/core.rb,
lib/onyxcord/interactions/command.rb,
lib/onyxcord/interactions/context.rb,
lib/onyxcord/events/handlers/voice.rb,
lib/onyxcord/interactions/registry.rb,
lib/onyxcord/models/install_params.rb,
lib/onyxcord/models/primary_server.rb,
lib/onyxcord/models/server_preview.rb,
lib/onyxcord/events/handlers/guilds.rb,
lib/onyxcord/internal/async_runtime.rb,
lib/onyxcord/models/scheduled_event.rb,
lib/onyxcord/utils/allowed_mentions.rb,
lib/onyxcord/internal/event_executor.rb,
lib/onyxcord/models/message_activity.rb,
lib/onyxcord/events/handlers/channels.rb,
lib/onyxcord/events/handlers/lifetime.rb,
lib/onyxcord/events/handlers/messages.rb,
lib/onyxcord/events/handlers/presence.rb,
lib/onyxcord/internal/gateway/opcodes.rb,
lib/onyxcord/internal/gateway/session.rb,
lib/onyxcord/internal/message_payload.rb,
lib/onyxcord/models/avatar_decoration.rb,
lib/onyxcord/models/role_subscription.rb,
lib/onyxcord/utils/message_components.rb,
lib/onyxcord/events/handlers/reactions.rb,
lib/onyxcord/internal/rate_limiter/rest.rb,
lib/onyxcord/application_commands/option.rb,
lib/onyxcord/models/role_connection_data.rb,
lib/onyxcord/application_commands/command.rb,
lib/onyxcord/application_commands/context.rb,
lib/onyxcord/events/handlers/interactions.rb,
lib/onyxcord/application_commands/registry.rb,
lib/onyxcord/core/bot/application_commands.rb,
lib/onyxcord/interactions/internal/message.rb,
lib/onyxcord/internal/rate_limiter/gateway.rb,
lib/onyxcord/interactions/internal/metadata.rb,
lib/onyxcord/events/handlers/scheduled_events.rb,
lib/onyxcord/internal/rate_limiter/async_rest.rb,
lib/onyxcord/interactions/internal/option_builder.rb,
lib/onyxcord/interactions/internal/permission_builder.rb,
lib/onyxcord/interactions/internal/application_command.rb

Overview

Top-level namespace for the OnyxCord Discord framework.

Defined Under Namespace

Modules: ApplicationCommands, Cache, CogMeta, Commands, Components, Errors, EventContainer, Events, Gateway, IDObject, Interactions, Internal, Light, MemberAttributes, MessageComponents, MessagePayload, PermissionCalculator, REST, ServerAttributes, UserAttributes, Voice, Webhooks Classes: Activity, ActivitySet, AllowedMentions, Application, ApplicationCommand, Attachment, AuditLogs, AvatarDecoration, Await, Bot, BulkBan, Call, Channel, ChannelTag, Cog, Collectibles, ColourRGB, Configuration, Embed, EmbedAuthor, EmbedField, EmbedFooter, EmbedImage, EmbedProvider, EmbedThumbnail, EmbedVideo, Emoji, InstallParams, Integration, IntegrationAccount, IntegrationApplication, Interaction, Invite, InviteChannel, InviteServer, Logger, Member, Message, MessageActivity, Overwrite, Paginator, Permissions, Poll, PrimaryServer, Profile, Reaction, Recipient, Role, RoleConnectionMetadata, RoleSubscriptionData, ScheduledEvent, SearchedMessages, Server, ServerBan, ServerPreview, Snapshot, Team, TimestampMarkdown, Upload, User, VoiceRegion, VoiceState, Webhook

Constant Summary collapse

LOGGER =

The default debug logger used by onyxcord.

Logger.new(ENV.fetch('ONYXCORD_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,
  message_content: 1 << 15,
  server_scheduled_events: 1 << 16,
  server_message_polls: 1 << 24,
  direct_message_polls: 1 << 25
}.freeze
INTENT_ALIASES =
{
  guilds: :servers,
  guild_members: :server_members,
  guild_bans: :server_bans,
  guild_emojis: :server_emojis,
  guild_integrations: :server_integrations,
  guild_webhooks: :server_webhooks,
  guild_invites: :server_invites,
  guild_voice_states: :server_voice_states,
  guild_presences: :server_presences,
  guild_messages: :server_messages,
  guild_message_reactions: :server_message_reactions,
  guild_message_typing: :server_message_typing,
  guild_scheduled_events: :server_scheduled_events,
  guild_message_polls: :server_message_polls
}.freeze
MINIMAL_INTENTS =
INTENTS.values_at(:servers, :server_messages, :direct_messages).reduce(&:|)
ALL_INTENTS =

All available intents

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

All unprivileged intents

ALL_INTENTS & ~(INTENTS[:server_members] | INTENTS[:server_presences] | INTENTS[:message_content])
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
  simple_datetime: 's', # 20/04/2021, 16:20
  medium_datetime: 'S' # 20/04/2021, 16:20:30
}.freeze
LOG_TIMESTAMP_FORMAT =

The format log timestamps should be in, in strftime format

'%Y-%m-%d %H:%M:%S.%L'
VERSION =
'4.0.0.beta.5'
ColorRGB =

Alias for the class ColourRGB

ColourRGB

Class Method Summary collapse

Class Method Details

.configurationObject



168
169
170
# File 'lib/onyxcord/core/configuration.rb', line 168

def self.configuration
  Configuration.current
end

.configure(&block) ⇒ Object



172
173
174
# File 'lib/onyxcord/core/configuration.rb', line 172

def self.configure(&block)
  Configuration.configure(&block)
end

.encode64(file) ⇒ String

A utility method to base64 encode a file like object using its mime type.

Parameters:

  • file (File, #read)

    A file like object that responds to #read.

Returns:

  • (String)

    The file object encoded as base64 image data.

Raises:

  • (ArgumentError)


150
151
152
153
154
155
156
157
158
# File 'lib/onyxcord.rb', line 150

def self.encode64(file)
  path_method = %i[original_filename path local_path].find { |method| file.respond_to?(method) }

  raise ArgumentError, 'File object must respond to original_filename, path, or local path.' unless path_method
  raise ArgumentError, 'File object must respond to read.' unless file.respond_to?(:read)

  mime_type = MIME::Types.type_for(file.__send__(path_method)).first&.to_s || 'image/jpeg'
  "data:#{mime_type};base64,#{Base64.encode64(file.read).strip}"
end

.id_compare?(one_id, other) ⇒ Boolean

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

Returns:

  • (Boolean)


73
74
75
# File 'lib/onyxcord.rb', line 73

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

.reset_configuration!Object



176
177
178
# File 'lib/onyxcord/core/configuration.rb', line 176

def self.reset_configuration!
  Configuration.reset!
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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/onyxcord.rb', line 100

def self.split_message(msg)
  return [] if msg.empty?

  chunks = []
  current = +''

  msg.lines.each do |line|
    if line.length > CHARACTER_LIMIT
      chunks << current unless current.empty?
      current = +''
      rest = line
      until rest.empty?
        break chunks << rest if rest.length <= CHARACTER_LIMIT

        chunk = rest[0, CHARACTER_LIMIT]
        split_at = chunk.rindex(' ')
        split_at = split_at ? split_at + 1 : CHARACTER_LIMIT
        chunks << rest[0, split_at]
        rest = rest[split_at..]
      end
    elsif current.length + line.length <= CHARACTER_LIMIT
      current << line
    else
      chunks << current
      current = line.dup
    end
  end

  chunks << current unless current.empty?
  chunks[-1] = chunks[-1].delete_suffix("\n") if chunks[-1]
  chunks
end

.timestamp(time, style = nil) ⇒ String

Examples:

OnyxCord.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:



139
140
141
142
143
144
145
# File 'lib/onyxcord.rb', line 139

def self.timestamp(time, style = nil)
  if style.nil?
    "<t:#{time.to_i}>"
  else
    "<t:#{time.to_i}:#{TIMESTAMP_STYLES[style] || style}>"
  end
end