Skip to content

Commit 42c50a7

Browse files
committed
Documentation.
1 parent 538a871 commit 42c50a7

File tree

6 files changed

+87
-2
lines changed

6 files changed

+87
-2
lines changed

Diff for: lib/async/discord.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
# Released under the MIT License.
4+
# Copyright, 2024, by Samuel Williams.
5+
6+
require_relative "discord/version"
7+
require_relative "discord/client"
8+
9+
# @namespace
10+
module Async
11+
# @namespace
12+
module Discord
13+
end
14+
end
15+

Diff for: lib/async/discord/channels.rb

+20
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77

88
module Async
99
module Discord
10+
# Represents a message in a channel.
1011
class Message < Representation
1112
end
1213

14+
# Represents a channel in a guild.
1315
class Channel < Representation
16+
# Send a message to this channel.
17+
#
18+
# @parameter content [String] The content of the message.
1419
def send_message(content)
1520
payload = {
1621
content: content
@@ -19,20 +24,32 @@ def send_message(content)
1924
Message.post(@resource.with(path: "messages"), payload)
2025
end
2126

27+
# The unique identifier for this channel.
2228
def id
2329
self.value[:id]
2430
end
2531

32+
# Whether this channel is a text channel.
33+
#
34+
# @returns [Boolean] if this channel is a text channel.
2635
def text?
2736
self.value[:type] == 0
2837
end
2938

39+
# Whether this channel is a voice channel.
40+
#
41+
# @returns [Boolean] if this channel is a voice channel.
3042
def voice?
3143
self.value[:type] == 2
3244
end
3345
end
3446

47+
# Represents a collection of channels.
3548
class Channels < Representation
49+
# Enumerate over each channel.
50+
#
51+
# @yields {|channel| ...}
52+
# @parameter channel [Channel] The channel.
3653
def each(&block)
3754
return to_enum unless block_given?
3855

@@ -43,6 +60,9 @@ def each(&block)
4360
end
4461
end
4562

63+
# Convert this collection to an array.
64+
#
65+
# @returns [Array(Channel)] an array of channels.
4666
def to_a
4767
each.to_a
4868
end

Diff for: lib/async/discord/client.rb

+12
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,19 @@
1010

1111
module Async
1212
module Discord
13+
# A client for interacting with Discord.
1314
class Client < Async::REST::Resource
15+
# The default endpoint for Discord.
1416
ENDPOINT = Async::HTTP::Endpoint.parse("https://discord.com/api/v10/")
17+
18+
# The default user agent for this client.
1519
USER_AGENT = "#{self.name} (https://github.com/socketry/async-discord, v#{Async::Discord::VERSION})"
1620

21+
# Authenticate the client, either with a bot or bearer token.
22+
#
23+
# @parameter bot [String] The bot token.
24+
# @parameter bearer [String] The bearer token.
25+
# @returns [Client] a new client with the given authentication.
1726
def authenticated(bot: nil, bearer: nil)
1827
headers = {}
1928

@@ -30,14 +39,17 @@ def authenticated(bot: nil, bearer: nil)
3039
return self.with(headers: headers)
3140
end
3241

42+
# @returns [Guilds] a collection of guilds the bot is a member of.
3343
def guilds
3444
Guilds.new(self.with(path: "users/@me/guilds"))
3545
end
3646

47+
# @returns [Gateway] the gateway for the bot.
3748
def gateway
3849
Gateway.new(self.with(path: "gateway/bot"))
3950
end
4051

52+
# @returns [Channel] a channel by its unique identifier.
4153
def channel(id)
4254
Channel.new(self.with(path: "channels/#{id}"))
4355
end

Diff for: lib/async/discord/gateway.rb

+25-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
module Async
1111
module Discord
12+
# Represents a gateway connection to Discord, which can be used to send and receive messages via a WebSocket connection.
1213
class GatewayConnection < Async::WebSocket::Connection
1314
# Gateway Opcodes:
1415
DISPATCH = 0
@@ -24,7 +25,7 @@ class GatewayConnection < Async::WebSocket::Connection
2425
HEARTBEAT_ACK = 11
2526
REQUEST_SOUNDBOARD_SOUNDS = 31
2627

27-
# Gateway Error Codes
28+
# Gateway Error Codes.
2829
ERROR_CODES = {
2930
4000 => "UNKNOWN_ERROR",
3031
4001 => "UNKNOWN_OPCODE",
@@ -42,7 +43,7 @@ class GatewayConnection < Async::WebSocket::Connection
4243
4014 => "DISALLOWED_INTENT"
4344
}
4445

45-
# Guild Intents:
46+
# Guild Intents.
4647
module Intent
4748
GUILDS = 1 << 0
4849
GUILD_MEMBERS = 1 << 1
@@ -67,27 +68,32 @@ module Intent
6768
DIRECT_MESSAGE_POLLS = 1 << 25
6869
end
6970

71+
# Default intent for a bot.
7072
DEFAULT_INTENT = Intent::GUILDS | Intent::GUILD_MESSAGES | Intent::DIRECT_MESSAGES
7173

74+
# Default properties for a bot.
7275
DEFAULT_PROPERTIES = {
7376
os: RUBY_PLATFORM,
7477
browser: Async::Discord.name,
7578
device: Async::Discord.name,
7679
}
7780

81+
# Default presence for a bot.
7882
DEFAULT_PRESENCE = {
7983
status: "online",
8084
afk: false,
8185
activities: [],
8286
}
8387

88+
# Initialize the gateway connection.
8489
def initialize(...)
8590
super
8691

8792
@heartbeat_task = nil
8893
@sequence = nil
8994
end
9095

96+
# Close the gateway connection, including the heartbeat task.
9197
def close(...)
9298
if heartbeat_task = @heartbeat_task
9399
@heartbeat_task = nil
@@ -97,6 +103,9 @@ def close(...)
97103
super
98104
end
99105

106+
# Identify the bot with the given identity.
107+
#
108+
# @returns [Hash] the payload from the READY event.
100109
def identify(**identity)
101110
while message = self.read
102111
payload = message.parse
@@ -136,6 +145,10 @@ def identify(**identity)
136145
end
137146
end
138147

148+
# Listen for events from the gateway.
149+
#
150+
# @yields {|payload| ...}
151+
# @parameter payload [Hash] The parsed payload.
139152
def listen
140153
while message = self.read
141154
payload = message.parse
@@ -158,6 +171,7 @@ def listen
158171

159172
private
160173

174+
# Run a heartbeat task at the given interval.
161175
def run_heartbeat(duration_ms)
162176
duration = duration_ms / 1000.0
163177
Console.debug(self, "Running heartbeat every #{duration} seconds.")
@@ -177,19 +191,28 @@ def run_heartbeat(duration_ms)
177191
end
178192
end
179193

194+
# Represents a gateway for the bot.
180195
class Gateway < Representation
196+
# The URL of the gateway, used for connecting to the WebSocket server.
181197
def url
182198
self.value[:url]
183199
end
184200

201+
# The number of shards to use.
185202
def shards
186203
self.value[:shards]
187204
end
188205

206+
189207
def session_start_limit
190208
self.value[:session_start_limit]
191209
end
192210

211+
# Connect to the gateway, yielding the connection.
212+
#
213+
# @yields {|connection| ...} if a block is given.
214+
# @parameter connection [GatewayConnection] The connection to the gateway.
215+
# @returns [GatewayConnection] the connection to the gateway.
193216
def connect(shard: nil, &block)
194217
endpoint = Async::HTTP::Endpoint.parse(self.url, alpn_protocols: Async::HTTP::Protocol::HTTP11.names)
195218

Diff for: lib/async/discord/guilds.rb

+13
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,26 @@
88

99
module Async
1010
module Discord
11+
# Represents a guild in Discord.
1112
class Guild < Representation
13+
# @returns [Channels] a collection of channels in this guild.
1214
def channels
1315
Channels.new(@resource.with(path: "channels"))
1416
end
1517

18+
# The unique identifier for this guild.
1619
def id
1720
self.value[:id]
1821
end
1922
end
2023

24+
# Represents a collection of guilds.
2125
class Guilds < Representation
26+
# Enumerate over each guild.
27+
#
28+
# @yields {|guild| ...} if a block is given.
29+
# @parameter guild [Guild] The guild.
30+
# @returns [Enumerator] if no block is given.
2231
def each(&block)
2332
return to_enum unless block_given?
2433

@@ -29,10 +38,14 @@ def each(&block)
2938
end
3039
end
3140

41+
# Convert the collection to an array.
42+
#
43+
# @returns [Array(Guild)] the collection as an array.
3244
def to_a
3345
each.to_a
3446
end
3547

48+
# @returns [Boolean] if the collection is empty.
3649
def empty?
3750
self.value.empty?
3851
end

Diff for: lib/async/discord/representation.rb

+2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88

99
module Async
1010
module Discord
11+
# The default wrapper for Discord.
1112
class Wrapper < Async::REST::Wrapper::JSON
1213
end
1314

15+
# The default representation for Discord.
1416
class Representation < Async::REST::Representation[Wrapper]
1517
end
1618
end

0 commit comments

Comments
 (0)