using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Threading.Tasks;
namespace Discord.Rest
{
public class DiscordRestClient : BaseDiscordClient, IDiscordClient
{
private RestApplication _applicationInfo;
public new RestSelfUser CurrentUser => base.CurrentUser as RestSelfUser;
public DiscordRestClient() : this(new DiscordRestConfig()) { }
public DiscordRestClient(DiscordRestConfig config) : base(config, CreateApiClient(config)) { }
private static API.DiscordRestApiClient CreateApiClient(DiscordRestConfig config)
=> new API.DiscordRestApiClient(config.RestClientProvider, DiscordRestConfig.UserAgent);
internal override void Dispose(bool disposing)
{
if (disposing)
ApiClient.Dispose();
}
internal override async Task OnLoginAsync(TokenType tokenType, string token)
{
var user = await ApiClient.GetMyUserAsync(new RequestOptions { RetryMode = RetryMode.AlwaysRetry }).ConfigureAwait(false);
ApiClient.CurrentUserId = user.Id;
base.CurrentUser = RestSelfUser.Create(this, user);
}
internal override Task OnLogoutAsync()
{
_applicationInfo = null;
return Task.Delay(0);
}
///
public async Task GetApplicationInfoAsync()
{
return _applicationInfo ?? (_applicationInfo = await ClientHelper.GetApplicationInfoAsync(this));
}
///
public Task GetChannelAsync(ulong id)
=> ClientHelper.GetChannelAsync(this, id);
///
public Task> GetPrivateChannelsAsync()
=> ClientHelper.GetPrivateChannelsAsync(this);
public Task> GetDMChannelsAsync()
=> ClientHelper.GetDMChannelsAsync(this);
public Task> GetGroupChannelsAsync()
=> ClientHelper.GetGroupChannelsAsync(this);
///
public Task> GetConnectionsAsync()
=> ClientHelper.GetConnectionsAsync(this);
///
public Task GetInviteAsync(string inviteId)
=> ClientHelper.GetInviteAsync(this, inviteId);
///
public Task GetGuildAsync(ulong id)
=> ClientHelper.GetGuildAsync(this, id);
///
public Task GetGuildEmbedAsync(ulong id)
=> ClientHelper.GetGuildEmbedAsync(this, id);
///
public Task> GetGuildSummariesAsync()
=> ClientHelper.GetGuildSummariesAsync(this);
///
public Task> GetGuildsAsync()
=> ClientHelper.GetGuildsAsync(this);
///
public Task CreateGuildAsync(string name, IVoiceRegion region, Stream jpegIcon = null)
=> ClientHelper.CreateGuildAsync(this, name, region, jpegIcon);
///
public Task GetUserAsync(ulong id)
=> ClientHelper.GetUserAsync(this, id);
///
public Task GetGuildUserAsync(ulong guildId, ulong id)
=> ClientHelper.GetGuildUserAsync(this, guildId, id);
///
public Task> GetVoiceRegionsAsync()
=> ClientHelper.GetVoiceRegionsAsync(this);
///
public Task GetVoiceRegionAsync(string id)
=> ClientHelper.GetVoiceRegionAsync(this, id);
//IDiscordClient
async Task IDiscordClient.GetApplicationInfoAsync()
=> await GetApplicationInfoAsync().ConfigureAwait(false);
async Task IDiscordClient.GetChannelAsync(ulong id, CacheMode mode)
{
if (mode == CacheMode.AllowDownload)
return await GetChannelAsync(id).ConfigureAwait(false);
else
return null;
}
async Task> IDiscordClient.GetPrivateChannelsAsync(CacheMode mode)
{
if (mode == CacheMode.AllowDownload)
return await GetPrivateChannelsAsync().ConfigureAwait(false);
else
return ImmutableArray.Create();
}
async Task> IDiscordClient.GetDMChannelsAsync(CacheMode mode)
{
if (mode == CacheMode.AllowDownload)
return await GetDMChannelsAsync().ConfigureAwait(false);
else
return ImmutableArray.Create();
}
async Task> IDiscordClient.GetGroupChannelsAsync(CacheMode mode)
{
if (mode == CacheMode.AllowDownload)
return await GetGroupChannelsAsync().ConfigureAwait(false);
else
return ImmutableArray.Create();
}
async Task> IDiscordClient.GetConnectionsAsync()
=> await GetConnectionsAsync().ConfigureAwait(false);
async Task IDiscordClient.GetInviteAsync(string inviteId)
=> await GetInviteAsync(inviteId).ConfigureAwait(false);
async Task IDiscordClient.GetGuildAsync(ulong id, CacheMode mode)
{
if (mode == CacheMode.AllowDownload)
return await GetGuildAsync(id).ConfigureAwait(false);
else
return null;
}
async Task> IDiscordClient.GetGuildsAsync(CacheMode mode)
{
if (mode == CacheMode.AllowDownload)
return await GetGuildsAsync().ConfigureAwait(false);
else
return ImmutableArray.Create();
}
async Task IDiscordClient.CreateGuildAsync(string name, IVoiceRegion region, Stream jpegIcon)
=> await CreateGuildAsync(name, region, jpegIcon).ConfigureAwait(false);
async Task IDiscordClient.GetUserAsync(ulong id, CacheMode mode)
{
if (mode == CacheMode.AllowDownload)
return await GetUserAsync(id).ConfigureAwait(false);
else
return null;
}
async Task> IDiscordClient.GetVoiceRegionsAsync()
=> await GetVoiceRegionsAsync().ConfigureAwait(false);
async Task IDiscordClient.GetVoiceRegionAsync(string id)
=> await GetVoiceRegionAsync(id).ConfigureAwait(false);
}
}