Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add application and application commands support #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/async/discord/application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require_relative "representation"
require_relative "application_commands"

module Async
module Discord
# Represents an application.
class Application < Representation
# The unique identifier for this application.
def id
self.value[:id]
end

# The name of this application.
def name
self.value[:name]
end

# The global commands for this application.
def commands
path = @resource.path.gsub("@me", id) + "/commands"
ApplicationCommands.new(@resource.with(path: path))
end
end
end
end
45 changes: 45 additions & 0 deletions lib/async/discord/application_commands.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

module Async
module Discord
# Represents an application command.
class ApplicationCommand < Representation
# The unique identifier for this application command.
def id
self.value[:id]
end

# The name of this application command.
def name
self.value[:name]
end

# Update this application command.
def update(payload)
self.class.patch(@resource, payload)
end

# Delete this application command.
def delete
self.class.delete(@resource)
end
end

# Represents a collection of application commands.
class ApplicationCommands < Representation
# Create a new application command.
def create(payload)
ApplicationCommand.post(@resource, payload)
end

# Enumerate over each command.
def each(&block)
return to_enum unless block_given?

self.value.each do |value|
yield ApplicationCommand.new(@resource.with(path: value[:id]), value: value)
end
end
end
end
end
27 changes: 19 additions & 8 deletions lib/async/discord/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,63 @@

require_relative "guilds"
require_relative "gateway"
require_relative "application"

module Async
module Discord
# A client for interacting with Discord.
class Client < Async::REST::Resource
# The default endpoint for Discord.
ENDPOINT = Async::HTTP::Endpoint.parse("https://discord.com/api/v10/")

# The default user agent for this client.
USER_AGENT = "#{self.name} (https://github.com/socketry/async-discord, v#{Async::Discord::VERSION})"

# Authenticate the client, either with a bot or bearer token.
#
# @parameter bot [String] The bot token.
# @parameter bearer [String] The bearer token.
# @returns [Client] a new client with the given authentication.
def authenticated(bot: nil, bearer: nil)
headers = {}

headers["user-agent"] ||= USER_AGENT

if bot
headers["authorization"] = "Bot #{bot}"
elsif bearer
headers["authorization"] = "Bearer #{bearer}"
else
raise ArgumentError, "You must provide either a bot or bearer token!"
end

return self.with(headers: headers)
end

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

# @returns [Gateway] the gateway for the bot.
def gateway
Gateway.new(self.with(path: "gateway/bot"))
end

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

# @returns [Application] the application.
def application(id)
Application.new(self.with(path: "applications/#{id}"))
end

# @returns [Application] the application which is currently authenticated.
def current_application
application("@me")
end
end
end
end
13 changes: 11 additions & 2 deletions test/async/discord/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@

describe Async::Discord::Client do
include Async::Discord::ClientContext

with "#guilds" do
it "can list guilds" do
guilds = client.guilds

expect(guilds).to be_a(Async::Discord::Guilds)
expect(guilds).not.to be(:empty?)
end
end

with "#current_application" do
it "can get the current application" do
application = client.current_application

expect(application).to be_a(Async::Discord::Application)
expect(application.id).to be_kind_of(Integer)
end
end
end