Skip to content

Commit

Permalink
Merge pull request #23 from Humans-of-Julia/Options
Browse files Browse the repository at this point in the history
Add helper functions to work with options
  • Loading branch information
xxxAnn authored Nov 4, 2021
2 parents d0b3c97 + a30c3b2 commit 7c9a09a
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 13 deletions.
2 changes: 2 additions & 0 deletions docs/src/helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ set_game
@fetchval
@deferred_fetch
@deferred_fetchval
ops
extops
```
1 change: 1 addition & 0 deletions src/Dizkord.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const Optional{T} = Union{T, Missing}
const Nullable{T} = Union{T, Nothing}
const OptionalNullable{T} = Union{T, Missing, Nothing}
const StringOrChar = Union{AbstractString, AbstractChar}
const MaybeAny = Union{T, Missing} where T<:Any

# Constant functions.
donothing(args...; kwargs...) = nothing
Expand Down
2 changes: 1 addition & 1 deletion src/client/handlers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function command!(f::Function, c::Client, name::AbstractString, description::Abs
add_handler!(c, OnInteractionCreate(f; name=name))
add_command!(c; name=name, description=description, kwargs...)
end
function command!(f::Function, c::Client, g::Int64, name::AbstractString, description::AbstractString; kwargs...)
function command!(f::Function, c::Client, g::Number, name::AbstractString, description::AbstractString; kwargs...)
add_handler!(c, OnInteractionCreate(f; name=name))
add_command!(c, Snowflake(g); name=name, description=description, kwargs...)
end
Expand Down
10 changes: 8 additions & 2 deletions src/types/interaction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,24 @@ struct ApplicationCommandChoice
end
@boilerplate ApplicationCommandChoice :constructors :docs :lower :merge

# TODO: Custom type gen for `value` field of ApplicationCommandOption.
"""
Application Command Option.
More details [here](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-option-structure).
"""
struct ApplicationCommandOption
value::MaybeAny
type::Optional{OptionType}
name::Optional{String}
type::OptionType
value::Optional{Any}
description::Optional{String}
required::Optional{Bool}
min_value::Optional{Number}
max_value::Optional{Number}
autocomplete::Optional{Bool}
choices::Optional{Vector{ApplicationCommandChoice}}
options::Optional{Vector{ApplicationCommandOption}}
channel_types::Optional{Vector{ChannelTypes}}
focused::Optional{Bool}
end
@boilerplate ApplicationCommandOption :constructors :docs :lower :merge

Expand Down
1 change: 1 addition & 0 deletions src/types/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ field(k::QuoteNode, ::Type{T}) where T = :($T(kwargs[$k]))
field(k::QuoteNode, ::Type{Vector{Snowflake}}) = :(snowflake.(kwargs[$k]))
field(k::QuoteNode, ::Type{Vector{DateTime}}) = :(datetime.(kwargs[$k]))
field(k::QuoteNode, ::Type{Vector{T}}) where T = :($T.(kwargs[$k]))
field(k::QuoteNode, ::Type{MaybeAny}) = :(haskey(kwargs, $k) ? kwargs[$k] : missing)
function field(k::QuoteNode, ::Type{T}) where T <: Enum
return :(kwargs[$k] isa Integer ? $T(Int(kwargs[$k])) :
kwargs[$k] isa $T ? kwargs[$k] : $T(kwargs[$k]))
Expand Down
22 changes: 22 additions & 0 deletions src/utils/helpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export PERM_NONE,
heartbeat_ping,
upload_file,
set_game,
opt,
extops,
@fetch,
@fetchval,
@deferred_fetch,
Expand Down Expand Up @@ -575,3 +577,23 @@ function deferfn!(ex, fns::Tuple, deferred::Symbol)
append!(ex.args, repls)
return ex
end

"""
opt(; kwargs...)
Helper function that is equivalent to calling `ApplicationCommandOption(; type=3, kwargs...)`
"""
opt(; kwargs...) = ApplicationCommandOption(; type=3, kwargs...)
"""
opt(ctx::Context)
Helper function that is equivalent to calling `extops(ctx.interaction.data.options)`
"""
opt(ctx::Context) = extops(ctx.interaction.data.options)
"""
extops(ops::Vector)
Creates a Dict of `option name` -> `option value` for the given vector of [`ApplicationCommandOption`](@ref).
If the option is of `Subcommand` type, creates a dict for all its subcommands.
"""
extops(ops::Vector) = Dict([(op.name, Int(op.type) < 3 ? extops(op.options) : op.value) for op in ops])
17 changes: 7 additions & 10 deletions test/fulltest.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,27 @@ client = Client(
intents(GUILDS, GUILD_MESSAGES)
)

TESTGUILD = ENV["TESTGUILD"] isa Number ? ENV["TESTGUILD"] : parse(Int, ENV["TESTGUILD"])

on_message!(client) do (ctx)
if ctx.message.author.id != me(client).id
Dizkord.reply(client, ctx, content="<@$(ctx.message.author.id)>, $(ctx.message.content) TEST")
end
end

command!(client, 776251117616234506, "boom", "Go boom!") do (ctx)
command!(client, TESTGUILD, "boom", "Go boom!") do (ctx)
Dizkord.reply(client, ctx, content="<@$(ctx.interaction.member.user.id)> blew up!")
end

command!(client, 776251117616234506, "bam", "Go bam!") do (ctx)
command!(client, TESTGUILD, "bam", "Go bam!") do (ctx)
Dizkord.reply(client, ctx, content="<@$(ctx.interaction.member.user.id)> slapped themselves!")
end

command!(client, 776251117616234506, "quit", "Ends the bot process!") do (ctx)
Dizkord.reply(client, ctx, content="Shutting down the bot")
close(client)
end

command!(client, 776251117616234506, "crack", "Go crack!") do (ctx)
Dizkord.reply(client, ctx, content="<@$(ctx.interaction.member.user.id)> hit a pole")
command!(client, TESTGUILD, "water", "Water the plants!"; options=[opt(name="someop", description="Dosum")]) do (ctx)
Dizkord.reply(client, ctx, content="<@$(ctx.interaction.member.user.id)> watered the plant so much they grew taller than them!\n$(opt(ctx))")
end

command!(client, 776251117616234506, "quit", "Ends the bot process!") do (ctx)
command!(client, TESTGUILD, "quit", "Ends the bot process!") do (ctx)
Dizkord.reply(client, ctx, content="Shutting down the bot")
close(client)
end
Expand Down

0 comments on commit 7c9a09a

Please sign in to comment.