diff --git a/Project.toml b/Project.toml index e790f91..a512ce9 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Dizkord" uuid = "c8b112a7-7677-4d35-8651-7c5b5cba290e" authors = ["Kyando "] -version = "0.5.0" +version = "0.5.1" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/src/Dizkord.jl b/src/Dizkord.jl index 05ef4cc..3e7d163 100644 --- a/src/Dizkord.jl +++ b/src/Dizkord.jl @@ -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" diff --git a/src/client/client.jl b/src/client/client.jl index ff34e8b..d13a7b5 100644 --- a/src/client/client.jl +++ b/src/client/client.jl @@ -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. diff --git a/src/client/handlers.jl b/src/client/handlers.jl index 907123f..007975e 100644 --- a/src/client/handlers.jl +++ b/src/client/handlers.jl @@ -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 diff --git a/src/rest/crud/crud.jl b/src/rest/crud/crud.jl index a8625b2..9f4b294 100644 --- a/src/rest/crud/crud.jl +++ b/src/rest/crud/crud.jl @@ -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. @@ -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} diff --git a/src/rest/endpoints/interaction.jl b/src/rest/endpoints/interaction.jl index 155de31..cf009c9 100644 --- a/src/rest/endpoints/interaction.jl +++ b/src/rest/endpoints/interaction.jl @@ -15,7 +15,7 @@ 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, @@ -23,6 +23,17 @@ function create_followup_message(c::Client, int_id::Snowflake, int_token::String 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) diff --git a/src/rest/rest.jl b/src/rest/rest.jl index 0bc3627..0b74453 100644 --- a/src/rest/rest.jl +++ b/src/rest/rest.jl @@ -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) @@ -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) diff --git a/test/fulltest.jl b/test/fulltest.jl index 4725344..673c00d 100644 --- a/test/fulltest.jl +++ b/test/fulltest.jl @@ -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)