Skip to content

Commit

Permalink
Bug patch and docs (#33)
Browse files Browse the repository at this point in the history
* Document obtain function

* Fix not responding to first command used

* Bump ver
  • Loading branch information
xxxAnn authored Jan 28, 2022
1 parent dacd972 commit 97d55ae
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Dizkord"
uuid = "c8b112a7-7677-4d35-8651-7c5b5cba290e"
authors = ["Kyando <izawa.iori.tan@gmail.com>"]
version = "0.5.0"
version = "0.5.1"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
2 changes: 1 addition & 1 deletion src/Dizkord.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using OpenTrick
using Setfield
using TimeToLive

const DISCORD_JL_VERSION = v"0.5.0"
const DISCORD_JL_VERSION = v"0.5.1"
const API_VERSION = 9
const DISCORD_API = "https://discordapp.com/api"

Expand Down
2 changes: 1 addition & 1 deletion src/client/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ mutable struct Client
commands::Vector{ApplicationCommand} # Commands
guild_commands::Dict{Snowflake, Vector{ApplicationCommand}} # Guild commands
intents::Int # Intents value
handlers::Dict{Symbol, Vector{Handler}} # Handlers for each event
handlers::Dict{Symbol, Vector{Handler}} # Handlers for each event
hb_interval::Int # Milliseconds between heartbeats.
hb_seq::Nullable{Int} # Sequence value sent by Discord for resuming.
last_hb::DateTime # Last heartbeat send.
Expand Down
8 changes: 5 additions & 3 deletions src/client/handlers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,17 @@ function handle(c::Client, handlers::Vector{Handler}, data::Dict, t::Symbol)
isvalidcomponent = (h) -> return (!hasproperty(h, :custom_id) || (!ismissing(ctx.interaction.data.custom_id) && h.custom_id == ctx.interaction.data.custom_id))
isvalid = (h) -> return isvalidcommand(h) && isvalidcomponent(h)
for hh in handlers
isvalid(hh) && (runhandler(hh, ctx, t))
isvalid(hh) && (runhandler(c, hh, ctx, t))
end
end

"""
Runs a handler with given context
"""
function runhandler(h::Handler, ctx::Context, t::Symbol)
(hasproperty(h, :name) || hasproperty(h, :custom_id)) && @debug "Running handlers" Handler=h Event=t Time=now()
function runhandler(c::Client, h::Handler, ctx::Context, t::Symbol)
if hasproperty(h, :name) || hasproperty(h, :custom_id)
ack_interaction(c, ctx.interaction.id, ctx.interaction.token)
end
h.f(ctx)
end

Expand Down
8 changes: 7 additions & 1 deletion src/rest/crud/crud.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ create(c, Ban, guild, member; reason="baz")
function create end

"""
retrieve(c::Client, ::Type{T}, args...; kwargs...) -> Future{Response}
retrieve(c::Client, ::Type{T}, args...; kwargs...) -> Future{Response{T}}
Retrieve, get, list, etc.
Expand All @@ -46,7 +46,13 @@ retrieve(c, Invite, "abcdef")
"""
function retrieve end

"""
obtain(c::Client, ::Type{T}, args...; kwargs...) -> T
Equivalent to retrieve, but blocks and returns the object of type T
"""
obtain(args...; kwargs...) = fetch(retrieve(args...; kwargs...)).val

"""
update(c::Client, x::T, args...; kwargs...) -> Future{Response}
Expand Down
13 changes: 12 additions & 1 deletion src/rest/endpoints/interaction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,25 @@ function get_application_commands(c::Client, guild::Snowflake)
return Response{Vector{ApplicationCommand}}(c, :GET, "/applications/$appid/guilds/$guild/commands")
end

function create_followup_message(c::Client, int_id::Snowflake, int_token::String; kwargs...)
function respond_to_interaction(c::Client, int_id::Snowflake, int_token::String; kwargs...)
dict = Dict{Symbol, Any}(
:data => kwargs,
:type => 4,
)
return Response{Message}(c, :POST, "/interactions/$int_id/$int_token/callback"; body=dict)
end

function create_followup_message(c::Client, int_id::Snowflake, int_token::String; kwargs...)
appid = c.application_id
return Response(c, :POST, "/webhooks/$appid/$int_token)"; body=kwargs)
end

function ack_interaction(c::Client, int_id::Snowflake, int_token::String; kwargs...)
dict = Dict{Symbol, Any}(
:type => 5,
)
return Response(c, :POST, "/interactions/$int_id/$int_token/callback"; body=dict)
end
function bulk_overwrite_application_commands(c::Client, guild::Snowflake, cmds::Vector{ApplicationCommand})
appid = c.application_id
return Response{Vector{ApplicationCommand}}(c, :PUT, "/applications/$appid/guilds/$guild/commands"; body=cmds)
Expand Down
5 changes: 4 additions & 1 deletion src/rest/rest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ end

# HTTP response with body (maybe).
function Response{T}(c::Client, r::HTTP.Messages.Response) where T
if T == Nothing
return Response{Nothing}(r)
end
r.status == 204 && return Response{T}(nothing, true, r, nothing)
r.status >= 300 && return Response{T}(nothing, false, r, nothing)

Expand Down Expand Up @@ -126,7 +129,7 @@ function Response{T}(
http_r = HTTP.request(args...; status_exception=false)
@debug "Sent http request" Time=now()
if http_r.status - 200 >= 100
@warn "Got an unexpected response" Code=http_r.status
@warn "Got an unexpected response" Code=http_r.status Body=http_r Sent=args
end
http_r.status == 429 && return http_r
r = Response{T}(c, http_r)
Expand Down
2 changes: 1 addition & 1 deletion test/fulltest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ command!(client, TESTGUILD, "quit", "Ends the bot process!") do (ctx)
end

on_ready!(client) do (ctx)
@info "Successfully logged in as $(ctx.user.username)"
@info "Successfully logged in as $(ctx.user.username)" # obtain(client, User).username
end

start(client)

0 comments on commit 97d55ae

Please sign in to comment.