Skip to content

Commit

Permalink
Implement new REST API endpoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nihlus committed Dec 18, 2020
1 parent e5efcad commit 508ace9
Show file tree
Hide file tree
Showing 10 changed files with 585 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public interface IDiscordRestApplicationAPI
Task<IRetrieveRestEntityResult<IReadOnlyList<IApplicationCommand>>> GetGlobalApplicationCommandsAsync
(
Snowflake applicationID,
CancellationToken ct
CancellationToken ct = default
);

/// <summary>
Expand All @@ -66,7 +66,7 @@ Task<ICreateRestEntityResult<IApplicationCommand>> CreateGlobalApplicationComman
string name,
string description,
Optional<IReadOnlyList<IApplicationCommandOption>> options,
CancellationToken ct
CancellationToken ct = default
);

/// <summary>
Expand All @@ -87,7 +87,7 @@ Task<IModifyRestEntityResult<IApplicationCommand>> EditGlobalApplicationCommandA
Optional<string> name,
Optional<string> description,
Optional<IReadOnlyList<IApplicationCommandOption>> options,
CancellationToken ct
CancellationToken ct = default
);

/// <summary>
Expand All @@ -101,7 +101,7 @@ Task<IDeleteRestEntityResult> DeleteGlobalApplicationCommandAsync
(
Snowflake applicationID,
Snowflake commandID,
CancellationToken ct
CancellationToken ct = default
);

/// <summary>
Expand All @@ -115,7 +115,7 @@ Task<IRetrieveRestEntityResult<IReadOnlyList<IApplicationCommand>>> GetGuildAppl
(
Snowflake applicationID,
Snowflake guildID,
CancellationToken ct
CancellationToken ct = default
);

/// <summary>
Expand All @@ -138,7 +138,7 @@ Task<ICreateRestEntityResult<IApplicationCommand>> CreateGuildApplicationCommand
string name,
string description,
Optional<IReadOnlyList<IApplicationCommandOption>> options,
CancellationToken ct
CancellationToken ct = default
);

/// <summary>
Expand All @@ -161,7 +161,7 @@ Task<IModifyRestEntityResult<IApplicationCommand>> EditGuildApplicationCommandAs
Optional<string> name,
Optional<string> description,
Optional<IReadOnlyList<IApplicationCommandOption>> options,
CancellationToken ct
CancellationToken ct = default
);

/// <summary>
Expand All @@ -177,7 +177,7 @@ Task<IDeleteRestEntityResult> DeleteGuildApplicationCommandAsync
Snowflake applicationID,
Snowflake guildID,
Snowflake commandID,
CancellationToken ct
CancellationToken ct = default
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Task<ICreateRestEntityResult<IInteractionResponse>> CreateInteractionResponseAsy
Snowflake interactionID,
string interactionToken,
IInteractionResponse response,
CancellationToken ct
CancellationToken ct = default
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ Task<IDeleteRestEntityResult> DeleteOriginalInteractionResponseAsync
(
Snowflake applicationID,
string token,
CancellationToken ct
CancellationToken ct = default
);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=api_005Cgateway_005Cevents_005Cchannels/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=api_005Cgateway_005Cevents_005Cconnectingresuming/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=api_005Cgateway_005Cevents_005Cguilds/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=api_005Cgateway_005Cevents_005Cinteractions/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=api_005Cgateway_005Cevents_005Cinvites/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=api_005Cgateway_005Cevents_005Cmessages/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=api_005Cgateway_005Cevents_005Cpresence/@EntryIndexedValue">True</s:Boolean>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
//
// DiscordRestApplicationAPI.cs
//
// Author:
// Jarl Gullberg <[email protected]>
//
// Copyright (c) 2017 Jarl Gullberg
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

using System.Collections.Generic;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Abstractions.Rest;
using Remora.Discord.API.Abstractions.Results;
using Remora.Discord.Core;
using Remora.Discord.Rest.Extensions;
using Remora.Discord.Rest.Results;

namespace Remora.Discord.Rest.API
{
/// <inheritdoc />
public class DiscordRestApplicationAPI : IDiscordRestApplicationAPI
{
private readonly DiscordHttpClient _discordHttpClient;
private readonly JsonSerializerOptions _jsonOptions;

/// <summary>
/// Initializes a new instance of the <see cref="DiscordRestApplicationAPI"/> class.
/// </summary>
/// <param name="discordHttpClient">The Discord HTTP client.</param>
/// <param name="jsonOptions">The json options.</param>
public DiscordRestApplicationAPI
(
DiscordHttpClient discordHttpClient,
IOptions<JsonSerializerOptions> jsonOptions
)
{
_discordHttpClient = discordHttpClient;
_jsonOptions = jsonOptions.Value;
}

/// <inheritdoc />
public Task<IRetrieveRestEntityResult<IReadOnlyList<IApplicationCommand>>> GetGlobalApplicationCommandsAsync
(
Snowflake applicationID,
CancellationToken ct
)
{
return _discordHttpClient.GetAsync<IReadOnlyList<IApplicationCommand>>
(
$"applications/{applicationID}/commands",
ct: ct
);
}

/// <inheritdoc />
public async Task<ICreateRestEntityResult<IApplicationCommand>> CreateGlobalApplicationCommandAsync
(
Snowflake applicationID,
string name,
string description,
Optional<IReadOnlyList<IApplicationCommandOption>> options,
CancellationToken ct
)
{
if (name.Length is < 3 or > 32)
{
return CreateRestEntityResult<IApplicationCommand>.FromError
(
"The name must be between 3 and 32 characters."
);
}

if (description.Length is < 1 or > 100)
{
return CreateRestEntityResult<IApplicationCommand>.FromError
(
"The description must be between 1 and 100 characters."
);
}

return await _discordHttpClient.PostAsync<IApplicationCommand>
(
$"applications/{applicationID}/commands",
b => b.WithJson
(
json =>
{
json.WriteString("name", name);
json.WriteString("description", description);
json.Write("options", options, _jsonOptions);
}
),
ct: ct
);
}

/// <inheritdoc />
public async Task<IModifyRestEntityResult<IApplicationCommand>> EditGlobalApplicationCommandAsync
(
Snowflake applicationID,
Snowflake commandID,
Optional<string> name,
Optional<string> description,
Optional<IReadOnlyList<IApplicationCommandOption>> options,
CancellationToken ct
)
{
if (name.HasValue && name.Value!.Length is < 3 or > 32)
{
return ModifyRestEntityResult<IApplicationCommand>.FromError
(
"The name must be between 3 and 32 characters."
);
}

if (description.HasValue && description.Value!.Length is < 1 or > 100)
{
return ModifyRestEntityResult<IApplicationCommand>.FromError
(
"The description must be between 1 and 100 characters."
);
}

return await _discordHttpClient.PatchAsync<IApplicationCommand>
(
$"applications/{applicationID}/commands/{commandID}",
b => b.WithJson
(
json =>
{
json.Write("name", name, _jsonOptions);
json.Write("description", description, _jsonOptions);
json.Write("options", options, _jsonOptions);
}
),
ct: ct
);
}

/// <inheritdoc />
public Task<IDeleteRestEntityResult> DeleteGlobalApplicationCommandAsync
(
Snowflake applicationID,
Snowflake commandID,
CancellationToken ct
)
{
return _discordHttpClient.DeleteAsync($"applications/{applicationID}/commands/{commandID}", ct: ct);
}

/// <inheritdoc />
public Task<IRetrieveRestEntityResult<IReadOnlyList<IApplicationCommand>>> GetGuildApplicationCommandsAsync
(
Snowflake applicationID,
Snowflake guildID,
CancellationToken ct
)
{
return _discordHttpClient.GetAsync<IReadOnlyList<IApplicationCommand>>
(
$"applications/{applicationID}/guilds/{guildID}/commands",
ct: ct
);
}

/// <inheritdoc />
public async Task<ICreateRestEntityResult<IApplicationCommand>> CreateGuildApplicationCommandAsync
(
Snowflake applicationID,
Snowflake guildID,
string name,
string description,
Optional<IReadOnlyList<IApplicationCommandOption>> options,
CancellationToken ct
)
{
if (name.Length is < 3 or > 32)
{
return CreateRestEntityResult<IApplicationCommand>.FromError
(
"The name must be between 3 and 32 characters."
);
}

if (description.Length is < 1 or > 100)
{
return CreateRestEntityResult<IApplicationCommand>.FromError
(
"The description must be between 1 and 100 characters."
);
}

return await _discordHttpClient.PostAsync<IApplicationCommand>
(
$"applications/{applicationID}/guilds/{guildID}/commands",
b => b.WithJson
(
json =>
{
json.WriteString("name", name);
json.WriteString("description", description);
json.Write("options", options, _jsonOptions);
}
),
ct: ct
);
}

/// <inheritdoc />
public async Task<IModifyRestEntityResult<IApplicationCommand>> EditGuildApplicationCommandAsync
(
Snowflake applicationID,
Snowflake guildID,
Snowflake commandID,
Optional<string> name,
Optional<string> description,
Optional<IReadOnlyList<IApplicationCommandOption>> options,
CancellationToken ct
)
{
if (name.HasValue && name.Value!.Length is < 3 or > 32)
{
return ModifyRestEntityResult<IApplicationCommand>.FromError
(
"The name must be between 3 and 32 characters."
);
}

if (description.HasValue && description.Value!.Length is < 1 or > 100)
{
return ModifyRestEntityResult<IApplicationCommand>.FromError
(
"The description must be between 1 and 100 characters."
);
}

return await _discordHttpClient.PatchAsync<IApplicationCommand>
(
$"applications/{applicationID}/guilds/{guildID}/commands/{commandID}",
b => b.WithJson
(
json =>
{
json.Write("name", name, _jsonOptions);
json.Write("description", description, _jsonOptions);
json.Write("options", options, _jsonOptions);
}
),
ct: ct
);
}

/// <inheritdoc />
public Task<IDeleteRestEntityResult> DeleteGuildApplicationCommandAsync
(
Snowflake applicationID,
Snowflake guildID,
Snowflake commandID,
CancellationToken ct
)
{
return _discordHttpClient.DeleteAsync
(
$"applications/{applicationID}/guilds/{guildID}/commands/{commandID}",
ct: ct
);
}
}
}
Loading

0 comments on commit 508ace9

Please sign in to comment.