* Request headers (#2394) * add support for per-request headers * remove unnecessary usings * Revert "remove unnecessary usings" This reverts commit 8d674fe4faf985b117f143fae3877a1698170ad2. * remove nullable strings from RequestOptions * Add Localization Support to Interaction Service (#2211) * add json and resx localization managers * add utils class for getting command paths * update json regex to make langage code optional * remove IServiceProvider from ILocalizationManager method params * replace the command path method in command map * add localization fields to rest and websocket application command entity implementations * move deconstruct extensions method to extensions folder * add withLocalizations parameter to rest methods * fix build error * add rest conversions to interaction service * add localization to the rest methods * add inline docs * fix implementation bugs * add missing inline docs * inline docs correction (Name/Description Localized properties) * add choice localization * fix conflicts * fix conflicts * add missing command props fields to ToApplicationCommandProps methods * add locale parameter to Get*ApplicationCommandsAsync methods for fetching localized command names/descriptions * Apply suggestions from code review Co-authored-by: Armano den Boef <68127614+Rozen4334@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Armano den Boef <68127614+Rozen4334@users.noreply.github.com> * Update src/Discord.Net.Core/Entities/Guilds/IGuild.cs Co-authored-by: Armano den Boef <68127614+Rozen4334@users.noreply.github.com> * add inline docs to LocalizationTarget * fix upstream merge errors * fix command parsing for context command names with space char * fix command parsing for context command names with space char * fix failed to generate buket id * fix get guild commands endpoint * update rexs localization manager to use single-file pattern * Upstream Merge Localization Branch (#2434) * fix ci/cd error (#2428) * Fix role icon & emoji assignment. (#2416) * Fix IGuild.GetBansAsync() (#2424) fix the problem of not being able to get more than 1000 bans * [DOCS] Add a note about `DontAutoRegisterAttribute` (#2430) * add a note about `DontAutoRegisterAttribute` * Remove "to to" and add punctuation Co-authored-by: MrCakeSlayer <13650699+MrCakeSlayer@users.noreply.github.com> * fix: Missing Fact attribute in ColorTests (#2425) * feat: Embed comparison (#2347) * Fix broken code snippet in dependency injection docs (#2420) * Fixed markdown formatting to show code snippet * Fixed constructor injection code snippet pointer * Added support for lottie stickers (#2359) Co-authored-by: Armano den Boef <68127614+Rozen4334@users.noreply.github.com> Co-authored-by: BokuNoPasya <49203428+1NieR@users.noreply.github.com> Co-authored-by: Misha133 <61027276+Misha-133@users.noreply.github.com> Co-authored-by: MrCakeSlayer <13650699+MrCakeSlayer@users.noreply.github.com> Co-authored-by: Ge <gehongyan1996@126.com> Co-authored-by: Charlie U <52503242+cpurules@users.noreply.github.com> Co-authored-by: Kuba_Z2 <77853483+KubaZ2@users.noreply.github.com> * remove unnecassary fields from ResxLocalizationManager * update int framework guides * remove space character tokenization from ResxLocalizationManager Co-authored-by: Armano den Boef <68127614+Rozen4334@users.noreply.github.com> Co-authored-by: BokuNoPasya <49203428+1NieR@users.noreply.github.com> Co-authored-by: Misha133 <61027276+Misha-133@users.noreply.github.com> Co-authored-by: MrCakeSlayer <13650699+MrCakeSlayer@users.noreply.github.com> Co-authored-by: Ge <gehongyan1996@126.com> Co-authored-by: Charlie U <52503242+cpurules@users.noreply.github.com> Co-authored-by: Kuba_Z2 <77853483+KubaZ2@users.noreply.github.com>
269 lines
13 KiB
C#
269 lines
13 KiB
C#
using System;
|
|
using Discord.API.Rest;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Immutable;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Discord.Rest
|
|
{
|
|
internal static class ClientHelper
|
|
{
|
|
#region Applications
|
|
public static async Task<RestApplication> GetApplicationInfoAsync(BaseDiscordClient client, RequestOptions options)
|
|
{
|
|
var model = await client.ApiClient.GetMyApplicationAsync(options).ConfigureAwait(false);
|
|
return RestApplication.Create(client, model);
|
|
}
|
|
|
|
public static async Task<RestChannel> GetChannelAsync(BaseDiscordClient client,
|
|
ulong id, RequestOptions options)
|
|
{
|
|
var model = await client.ApiClient.GetChannelAsync(id, options).ConfigureAwait(false);
|
|
if (model != null)
|
|
return RestChannel.Create(client, model);
|
|
return null;
|
|
}
|
|
/// <exception cref="InvalidOperationException">Unexpected channel type.</exception>
|
|
public static async Task<IReadOnlyCollection<IRestPrivateChannel>> GetPrivateChannelsAsync(BaseDiscordClient client, RequestOptions options)
|
|
{
|
|
var models = await client.ApiClient.GetMyPrivateChannelsAsync(options).ConfigureAwait(false);
|
|
return models.Select(x => RestChannel.CreatePrivate(client, x)).ToImmutableArray();
|
|
}
|
|
public static async Task<IReadOnlyCollection<RestDMChannel>> GetDMChannelsAsync(BaseDiscordClient client, RequestOptions options)
|
|
{
|
|
var models = await client.ApiClient.GetMyPrivateChannelsAsync(options).ConfigureAwait(false);
|
|
return models
|
|
.Where(x => x.Type == ChannelType.DM)
|
|
.Select(x => RestDMChannel.Create(client, x)).ToImmutableArray();
|
|
}
|
|
public static async Task<IReadOnlyCollection<RestGroupChannel>> GetGroupChannelsAsync(BaseDiscordClient client, RequestOptions options)
|
|
{
|
|
var models = await client.ApiClient.GetMyPrivateChannelsAsync(options).ConfigureAwait(false);
|
|
return models
|
|
.Where(x => x.Type == ChannelType.Group)
|
|
.Select(x => RestGroupChannel.Create(client, x)).ToImmutableArray();
|
|
}
|
|
|
|
public static async Task<IReadOnlyCollection<RestConnection>> GetConnectionsAsync(BaseDiscordClient client, RequestOptions options)
|
|
{
|
|
var models = await client.ApiClient.GetMyConnectionsAsync(options).ConfigureAwait(false);
|
|
return models.Select(model => RestConnection.Create(client, model)).ToImmutableArray();
|
|
}
|
|
|
|
public static async Task<RestInviteMetadata> GetInviteAsync(BaseDiscordClient client,
|
|
string inviteId, RequestOptions options)
|
|
{
|
|
var model = await client.ApiClient.GetInviteAsync(inviteId, options).ConfigureAwait(false);
|
|
if (model != null)
|
|
return RestInviteMetadata.Create(client, null, null, model);
|
|
return null;
|
|
}
|
|
|
|
public static async Task<RestGuild> GetGuildAsync(BaseDiscordClient client,
|
|
ulong id, bool withCounts, RequestOptions options)
|
|
{
|
|
var model = await client.ApiClient.GetGuildAsync(id, withCounts, options).ConfigureAwait(false);
|
|
if (model != null)
|
|
return RestGuild.Create(client, model);
|
|
return null;
|
|
}
|
|
public static async Task<RestGuildWidget?> GetGuildWidgetAsync(BaseDiscordClient client,
|
|
ulong id, RequestOptions options)
|
|
{
|
|
var model = await client.ApiClient.GetGuildWidgetAsync(id, options).ConfigureAwait(false);
|
|
if (model != null)
|
|
return RestGuildWidget.Create(model);
|
|
return null;
|
|
}
|
|
public static IAsyncEnumerable<IReadOnlyCollection<RestUserGuild>> GetGuildSummariesAsync(BaseDiscordClient client,
|
|
ulong? fromGuildId, int? limit, RequestOptions options)
|
|
{
|
|
return new PagedAsyncEnumerable<RestUserGuild>(
|
|
DiscordConfig.MaxGuildsPerBatch,
|
|
async (info, ct) =>
|
|
{
|
|
var args = new GetGuildSummariesParams
|
|
{
|
|
Limit = info.PageSize
|
|
};
|
|
if (info.Position != null)
|
|
args.AfterGuildId = info.Position.Value;
|
|
var models = await client.ApiClient.GetMyGuildsAsync(args, options).ConfigureAwait(false);
|
|
return models
|
|
.Select(x => RestUserGuild.Create(client, x))
|
|
.ToImmutableArray();
|
|
},
|
|
nextPage: (info, lastPage) =>
|
|
{
|
|
if (lastPage.Count != DiscordConfig.MaxMessagesPerBatch)
|
|
return false;
|
|
info.Position = lastPage.Max(x => x.Id);
|
|
return true;
|
|
},
|
|
start: fromGuildId,
|
|
count: limit
|
|
);
|
|
}
|
|
public static async Task<IReadOnlyCollection<RestGuild>> GetGuildsAsync(BaseDiscordClient client, bool withCounts, RequestOptions options)
|
|
{
|
|
var summaryModels = await GetGuildSummariesAsync(client, null, null, options).FlattenAsync().ConfigureAwait(false);
|
|
var guilds = ImmutableArray.CreateBuilder<RestGuild>();
|
|
foreach (var summaryModel in summaryModels)
|
|
{
|
|
var guildModel = await client.ApiClient.GetGuildAsync(summaryModel.Id, withCounts).ConfigureAwait(false);
|
|
if (guildModel != null)
|
|
guilds.Add(RestGuild.Create(client, guildModel));
|
|
}
|
|
return guilds.ToImmutable();
|
|
}
|
|
public static async Task<RestGuild> CreateGuildAsync(BaseDiscordClient client,
|
|
string name, IVoiceRegion region, Stream jpegIcon, RequestOptions options)
|
|
{
|
|
var args = new CreateGuildParams(name, region.Id);
|
|
if (jpegIcon != null)
|
|
args.Icon = new API.Image(jpegIcon);
|
|
|
|
var model = await client.ApiClient.CreateGuildAsync(args, options).ConfigureAwait(false);
|
|
return RestGuild.Create(client, model);
|
|
}
|
|
|
|
public static async Task<RestUser> GetUserAsync(BaseDiscordClient client,
|
|
ulong id, RequestOptions options)
|
|
{
|
|
var model = await client.ApiClient.GetUserAsync(id, options).ConfigureAwait(false);
|
|
if (model != null)
|
|
return RestUser.Create(client, model);
|
|
return null;
|
|
}
|
|
public static async Task<RestGuildUser> GetGuildUserAsync(BaseDiscordClient client,
|
|
ulong guildId, ulong id, RequestOptions options)
|
|
{
|
|
var guild = await GetGuildAsync(client, guildId, false, options).ConfigureAwait(false);
|
|
if (guild == null)
|
|
return null;
|
|
|
|
var model = await client.ApiClient.GetGuildMemberAsync(guildId, id, options).ConfigureAwait(false);
|
|
if (model != null)
|
|
return RestGuildUser.Create(client, guild, model);
|
|
|
|
return null;
|
|
}
|
|
|
|
public static async Task<RestWebhook> GetWebhookAsync(BaseDiscordClient client, ulong id, RequestOptions options)
|
|
{
|
|
var model = await client.ApiClient.GetWebhookAsync(id).ConfigureAwait(false);
|
|
if (model != null)
|
|
return RestWebhook.Create(client, (IGuild)null, model);
|
|
return null;
|
|
}
|
|
|
|
public static async Task<IReadOnlyCollection<RestVoiceRegion>> GetVoiceRegionsAsync(BaseDiscordClient client, RequestOptions options)
|
|
{
|
|
var models = await client.ApiClient.GetVoiceRegionsAsync(options).ConfigureAwait(false);
|
|
return models.Select(x => RestVoiceRegion.Create(client, x)).ToImmutableArray();
|
|
}
|
|
public static async Task<RestVoiceRegion> GetVoiceRegionAsync(BaseDiscordClient client,
|
|
string id, RequestOptions options)
|
|
{
|
|
var models = await client.ApiClient.GetVoiceRegionsAsync(options).ConfigureAwait(false);
|
|
return models.Select(x => RestVoiceRegion.Create(client, x)).FirstOrDefault(x => x.Id == id);
|
|
}
|
|
|
|
public static async Task<int> GetRecommendShardCountAsync(BaseDiscordClient client, RequestOptions options)
|
|
{
|
|
var response = await client.ApiClient.GetBotGatewayAsync(options).ConfigureAwait(false);
|
|
return response.Shards;
|
|
}
|
|
|
|
public static async Task<BotGateway> GetBotGatewayAsync(BaseDiscordClient client, RequestOptions options)
|
|
{
|
|
var response = await client.ApiClient.GetBotGatewayAsync(options).ConfigureAwait(false);
|
|
return new BotGateway
|
|
{
|
|
Url = response.Url,
|
|
Shards = response.Shards,
|
|
SessionStartLimit = new SessionStartLimit
|
|
{
|
|
Total = response.SessionStartLimit.Total,
|
|
Remaining = response.SessionStartLimit.Remaining,
|
|
ResetAfter = response.SessionStartLimit.ResetAfter,
|
|
MaxConcurrency = response.SessionStartLimit.MaxConcurrency
|
|
}
|
|
};
|
|
}
|
|
|
|
public static async Task<IReadOnlyCollection<RestGlobalCommand>> GetGlobalApplicationCommandsAsync(BaseDiscordClient client, bool withLocalizations = false,
|
|
string locale = null, RequestOptions options = null)
|
|
{
|
|
var response = await client.ApiClient.GetGlobalApplicationCommandsAsync(withLocalizations, locale, options).ConfigureAwait(false);
|
|
|
|
if (!response.Any())
|
|
return Array.Empty<RestGlobalCommand>();
|
|
|
|
return response.Select(x => RestGlobalCommand.Create(client, x)).ToArray();
|
|
}
|
|
public static async Task<RestGlobalCommand> GetGlobalApplicationCommandAsync(BaseDiscordClient client, ulong id,
|
|
RequestOptions options = null)
|
|
{
|
|
var model = await client.ApiClient.GetGlobalApplicationCommandAsync(id, options);
|
|
|
|
return model != null ? RestGlobalCommand.Create(client, model) : null;
|
|
}
|
|
|
|
public static async Task<IReadOnlyCollection<RestGuildCommand>> GetGuildApplicationCommandsAsync(BaseDiscordClient client, ulong guildId, bool withLocalizations = false,
|
|
string locale = null, RequestOptions options = null)
|
|
{
|
|
var response = await client.ApiClient.GetGuildApplicationCommandsAsync(guildId, withLocalizations, locale, options).ConfigureAwait(false);
|
|
|
|
if (!response.Any())
|
|
return ImmutableArray.Create<RestGuildCommand>();
|
|
|
|
return response.Select(x => RestGuildCommand.Create(client, x, guildId)).ToImmutableArray();
|
|
}
|
|
public static async Task<RestGuildCommand> GetGuildApplicationCommandAsync(BaseDiscordClient client, ulong id, ulong guildId,
|
|
RequestOptions options = null)
|
|
{
|
|
var model = await client.ApiClient.GetGuildApplicationCommandAsync(guildId, id, options);
|
|
|
|
return model != null ? RestGuildCommand.Create(client, model, guildId) : null;
|
|
}
|
|
public static async Task<RestGuildCommand> CreateGuildApplicationCommandAsync(BaseDiscordClient client, ulong guildId, ApplicationCommandProperties properties,
|
|
RequestOptions options = null)
|
|
{
|
|
var model = await InteractionHelper.CreateGuildCommandAsync(client, guildId, properties, options);
|
|
|
|
return RestGuildCommand.Create(client, model, guildId);
|
|
}
|
|
public static async Task<RestGlobalCommand> CreateGlobalApplicationCommandAsync(BaseDiscordClient client, ApplicationCommandProperties properties,
|
|
RequestOptions options = null)
|
|
{
|
|
var model = await InteractionHelper.CreateGlobalCommandAsync(client, properties, options);
|
|
|
|
return RestGlobalCommand.Create(client, model);
|
|
}
|
|
public static async Task<IReadOnlyCollection<RestGlobalCommand>> BulkOverwriteGlobalApplicationCommandAsync(BaseDiscordClient client, ApplicationCommandProperties[] properties,
|
|
RequestOptions options = null)
|
|
{
|
|
var models = await InteractionHelper.BulkOverwriteGlobalCommandsAsync(client, properties, options);
|
|
|
|
return models.Select(x => RestGlobalCommand.Create(client, x)).ToImmutableArray();
|
|
}
|
|
public static async Task<IReadOnlyCollection<RestGuildCommand>> BulkOverwriteGuildApplicationCommandAsync(BaseDiscordClient client, ulong guildId,
|
|
ApplicationCommandProperties[] properties, RequestOptions options = null)
|
|
{
|
|
var models = await InteractionHelper.BulkOverwriteGuildCommandsAsync(client, guildId, properties, options);
|
|
|
|
return models.Select(x => RestGuildCommand.Create(client, x, guildId)).ToImmutableArray();
|
|
}
|
|
|
|
public static Task AddRoleAsync(BaseDiscordClient client, ulong guildId, ulong userId, ulong roleId, RequestOptions options = null)
|
|
=> client.ApiClient.AddRoleAsync(guildId, userId, roleId, options);
|
|
|
|
public static Task RemoveRoleAsync(BaseDiscordClient client, ulong guildId, ulong userId, ulong roleId, RequestOptions options = null)
|
|
=> client.ApiClient.RemoveRoleAsync(guildId, userId, roleId, options);
|
|
#endregion
|
|
}
|
|
}
|