Class: LibDiscord::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/lib_discord/client.rb

Overview

This class is the entry point in utilizing lib_discord to access Discord’s API. Initialize a client and cache it for as long as you need to make requests to Discord.

require "lib_discord"
client = LibDiscord::Client.new("Bot my.amazing.token")

Instance Method Summary collapse

Constructor Details

#initialize(auth_header, base_url: "https://discord.com/api/v10", user_agent: "LibDiscord (#{LibDiscord::PROJECT_URL}, #{LibDiscord.version})", logger: nil) ⇒ self

Initialize a LibDiscord::Client for making requests to Discord’s RESTful web API.

Examples:

hook in your own logger and app-specific user-agent

client = LibDiscord::Client.new(
  "Bot my.token",
  user_agent: "MyApp (https://example.myapp.com, 1.2.3)",
  logger: my_logger
)

I don’t want any messages going to stdout.

client = LibDiscord::Client.new(
  "Bot my.token",
  logger: Logger.new("/dev/null")
)

Parameters:

  • auth_header (#to_s)

    Authorization header to be used in communication with Discord. In the format Bot <token> or Bearer <token>.

  • base_url (#to_s) (defaults to: "https://discord.com/api/v10")

    Base URL to use in requests to Discord.

  • user_agent (#to_s) (defaults to: "LibDiscord (#{LibDiscord::PROJECT_URL}, #{LibDiscord.version})")

    User-Agent header to use in requests to Discord.

  • logger (Logger) (defaults to: nil)

    Logger to use for output messages. If not provided, a basic INFO-level $stdout logger is created for internal use ( l = Logger.new($stdout); l.level = :info )

Raises:

  • (ConstraintError)

    if parameters for auth_header, base_url, or user_agent do not respond to #to_s.

  • (ConstraintError)

    if the parameter for logger is not an instance of Logger, or an instance that inherits from Logger.

See Also:



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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/lib_discord/client.rb', line 75

def initialize(
  auth_header,
  base_url: "https://discord.com/api/v10",
  user_agent: "LibDiscord (#{LibDiscord::PROJECT_URL}, #{LibDiscord.version})",
  logger: nil
)
  auth_header = Types::Coercible::String[auth_header]
  base_url = Types::Coercible::String[base_url]
  user_agent = Types::Coercible::String[user_agent]

  unless logger
    logger = Logger.new($stdout)
    logger.level = :info
  end

  logger = Types::Logger[logger]

  @container = Dry::Container.new

  @container.register("application") do
    Application.new(auth_header, base_url, user_agent, logger)
  end

  @container.register("application_command") do
    ApplicationCommand.new(auth_header, base_url, user_agent, logger)
  end

  @container.register("application_role_connection_metadata") do
    ApplicationRoleConnectionMetadata.new(
      auth_header,
      base_url,
      user_agent,
      logger
    )
  end

  @container.register("audit_log") do
    AuditLog.new(auth_header, base_url, user_agent, logger)
  end

  @container.register("auto_moderation") do
    AutoModeration.new(auth_header, base_url, user_agent, logger)
  end

  @container.register("channel") do
    Channel.new(auth_header, base_url, user_agent, logger)
  end

  @container.register("emoji") do
    Emoji.new(auth_header, base_url, user_agent, logger)
  end

  @container.register("guild") do
    Guild.new(auth_header, base_url, user_agent, logger)
  end

  @container.register("guild_scheduled_event") do
    GuildScheduledEvent.new(auth_header, base_url, user_agent, logger)
  end

  @container.register("guild_template") do
    GuildTemplate.new(auth_header, base_url, user_agent, logger)
  end

  @container.register("interaction") do
    Interaction.new(auth_header, base_url, user_agent, logger)
  end

  @container.register("invite") do
    Invite.new(auth_header, base_url, user_agent, logger)
  end

  @container.register("poll") do
    Poll.new(auth_header, base_url, user_agent, logger)
  end

  @container.register("stage_instance") do
    StageInstance.new(auth_header, base_url, user_agent, logger)
  end

  @container.register("sticker") do
    Sticker.new(auth_header, base_url, user_agent, logger)
  end

  @container.register("user") do
    User.new(auth_header, base_url, user_agent, logger)
  end

  @container.register("voice") do
    Voice.new(auth_header, base_url, user_agent, logger)
  end

  @container.register("webhook") do
    Webhook.new(auth_header, base_url, user_agent, logger)
  end
rescue Dry::Types::ConstraintError => e
  raise ConstraintError.new(e.result, e.input)
end

Instance Method Details

#[](resource) ⇒ Object

Get a Resource for making Discord API requests.

Examples:

client["application"]
# => #<LibDiscord::Application: ... >
client["not.a.valid.key"]
# => key not found "not.a.valid.key" (LibDiscord::KeyError)

Parameters:

  • resource (String)

    Resource key. see #keys for available values.

Returns:

  • Resource

Raises:

  • (KeyError)

    if no resource exists for a given key



189
190
191
192
193
# File 'lib/lib_discord/client.rb', line 189

def [](resource)
  @container[resource]
rescue Dry::Container::KeyError => e
  raise KeyError.new(e.message)
end

#keysArray<String>

Get available Resource keys.

Examples:

client.keys
# =>
# ["application",
#  "guild",
#  "channel",
#  ...]

Returns:

  • (Array<String>)

    a list of resources available



207
# File 'lib/lib_discord/client.rb', line 207

def keys = @container.keys