using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Discord.API;
using Discord.Rest;
namespace Discord.WebSocket
{
///
/// Represents the base of a WebSocket-based Discord client.
///
public abstract partial class BaseSocketClient : BaseDiscordClient, IDiscordClient
{
protected readonly DiscordSocketConfig BaseConfig;
///
/// Gets the estimated round-trip latency, in milliseconds, to the gateway server.
///
///
/// An that represents the round-trip latency to the WebSocket server. Please
/// note that this value does not represent a "true" latency for operations such as sending a message.
///
public abstract int Latency { get; protected set; }
///
/// Gets the status for the logged-in user.
///
///
/// A status object that represents the user's online presence status.
///
public abstract UserStatus Status { get; protected set; }
///
/// Gets the activity for the logged-in user.
///
///
/// An activity object that represents the user's current activity.
///
public abstract IActivity Activity { get; protected set; }
///
/// Provides access to a REST-only client with a shared state from this client.
///
public abstract DiscordSocketRestClient Rest { get; }
internal new DiscordSocketApiClient ApiClient => base.ApiClient as DiscordSocketApiClient;
///
/// Gets the current logged-in user.
///
public new SocketSelfUser CurrentUser { get => base.CurrentUser as SocketSelfUser; protected set => base.CurrentUser = value; }
///
/// Gets a collection of guilds that the user is currently in.
///
///
/// A read-only collection of guilds that the current user is in.
///
public abstract IReadOnlyCollection Guilds { get; }
///
/// Gets a collection of private channels opened in this session.
///
///
/// This method will retrieve all private channels (including direct-message, group channel and such) that
/// are currently opened in this session.
///
/// This method will not return previously opened private channels outside of the current session! If
/// you have just started the client, this may return an empty collection.
///
///
///
/// A read-only collection of private channels that the user currently partakes in.
///
public abstract IReadOnlyCollection PrivateChannels { get; }
///
/// Gets a collection of available voice regions.
///
///
/// A read-only collection of voice regions that the user has access to.
///
public abstract IReadOnlyCollection VoiceRegions { get; }
internal BaseSocketClient(DiscordSocketConfig config, DiscordRestApiClient client)
: base(config, client) => BaseConfig = config;
private static DiscordSocketApiClient CreateApiClient(DiscordSocketConfig config)
=> new DiscordSocketApiClient(config.RestClientProvider, config.WebSocketProvider, DiscordRestConfig.UserAgent,
rateLimitPrecision: config.RateLimitPrecision,
useSystemClock: config.UseSystemClock);
///
/// Gets a Discord application information for the logged-in user.
///
///
/// This method reflects your application information you submitted when creating a Discord application via
/// the Developer Portal.
///
/// The options to be used when sending the request.
///
/// A task that represents the asynchronous get operation. The task result contains the application
/// information.
///
public abstract Task GetApplicationInfoAsync(RequestOptions options = null);
///
/// Gets a generic user.
///
/// The user snowflake ID.
///
/// This method gets the user present in the WebSocket cache with the given condition.
///
/// Sometimes a user may return null due to Discord not sending offline users in large guilds
/// (i.e. guild with 100+ members) actively. To download users on startup and to see more information
/// about this subject, see .
///
///
/// This method does not attempt to fetch users that the logged-in user does not have access to (i.e.
/// users who don't share mutual guild(s) with the current user). If you wish to get a user that you do
/// not have access to, consider using the REST implementation of
/// .
///
///
///
/// A generic WebSocket-based user; null when the user cannot be found.
///
public abstract SocketUser GetUser(ulong id);
///
/// Gets a user.
///
///
/// This method gets the user present in the WebSocket cache with the given condition.
///
/// Sometimes a user may return null due to Discord not sending offline users in large guilds
/// (i.e. guild with 100+ members) actively. To download users on startup and to see more information
/// about this subject, see .
///
///
/// This method does not attempt to fetch users that the logged-in user does not have access to (i.e.
/// users who don't share mutual guild(s) with the current user). If you wish to get a user that you do
/// not have access to, consider using the REST implementation of
/// .
///
///
/// The name of the user.
/// The discriminator value of the user.
///
/// A generic WebSocket-based user; null when the user cannot be found.
///
public abstract SocketUser GetUser(string username, string discriminator);
///
/// Gets a channel.
///
/// The snowflake identifier of the channel (e.g. `381889909113225237`).
///
/// A generic WebSocket-based channel object (voice, text, category, etc.) associated with the identifier;
/// null when the channel cannot be found.
///
public abstract SocketChannel GetChannel(ulong id);
///
/// Gets a guild.
///
/// The guild snowflake identifier.
///
/// A WebSocket-based guild associated with the snowflake identifier; null when the guild cannot be
/// found.
///
public abstract SocketGuild GetGuild(ulong id);
///
/// Gets a voice region.
///
/// The identifier of the voice region (e.g. eu-central ).
///
/// A REST-based voice region associated with the identifier; null if the voice region is not
/// found.
///
public abstract RestVoiceRegion GetVoiceRegion(string id);
///
public abstract Task StartAsync();
///
public abstract Task StopAsync();
///
/// Sets the current status of the user (e.g. Online, Do not Disturb).
///
/// The new status to be set.
///
/// A task that represents the asynchronous set operation.
///
public abstract Task SetStatusAsync(UserStatus status);
///
/// Sets the game of the user.
///
/// The name of the game.
/// If streaming, the URL of the stream. Must be a valid Twitch URL.
/// The type of the game.
///
/// A task that represents the asynchronous set operation.
///
public abstract Task SetGameAsync(string name, string streamUrl = null, ActivityType type = ActivityType.Playing);
///
/// Sets the of the logged-in user.
///
///
/// This method sets the of the user.
///
/// Discord will only accept setting of name and the type of activity.
///
///
/// Rich Presence cannot be set via this method or client. Rich Presence is strictly limited to RPC
/// clients only.
///
///
/// The activity to be set.
///
/// A task that represents the asynchronous set operation.
///
public abstract Task SetActivityAsync(IActivity activity);
///
/// Attempts to download users into the user cache for the selected guilds.
///
/// The guilds to download the members from.
///
/// A task that represents the asynchronous download operation.
///
public abstract Task DownloadUsersAsync(IEnumerable guilds);
///
/// Creates a guild for the logged-in user who is in less than 10 active guilds.
///
///
/// This method creates a new guild on behalf of the logged-in user.
///
/// Due to Discord's limitation, this method will only work for users that are in less than 10 guilds.
///
///
/// The name of the new guild.
/// The voice region to create the guild with.
/// The icon of the guild.
/// The options to be used when sending the request.
///
/// A task that represents the asynchronous creation operation. The task result contains the created guild.
///
public Task CreateGuildAsync(string name, IVoiceRegion region, Stream jpegIcon = null, RequestOptions options = null)
=> ClientHelper.CreateGuildAsync(this, name, region, jpegIcon, options ?? RequestOptions.Default);
///
/// Gets the connections that the user has set up.
///
/// The options to be used when sending the request.
///
/// A task that represents the asynchronous get operation. The task result contains a read-only collection of connections.
///
public Task> GetConnectionsAsync(RequestOptions options = null)
=> ClientHelper.GetConnectionsAsync(this, options ?? RequestOptions.Default);
///
/// Gets an invite.
///
/// The invitation identifier.
/// The options to be used when sending the request.
///
/// A task that represents the asynchronous get operation. The task result contains the invite information.
///
public Task GetInviteAsync(string inviteId, RequestOptions options = null)
=> ClientHelper.GetInviteAsync(this, inviteId, options ?? RequestOptions.Default);
// IDiscordClient
///
async Task IDiscordClient.GetApplicationInfoAsync(RequestOptions options)
=> await GetApplicationInfoAsync(options).ConfigureAwait(false);
///
Task IDiscordClient.GetChannelAsync(ulong id, CacheMode mode, RequestOptions options)
=> Task.FromResult(GetChannel(id));
///
Task> IDiscordClient.GetPrivateChannelsAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult>(PrivateChannels);
///
async Task> IDiscordClient.GetConnectionsAsync(RequestOptions options)
=> await GetConnectionsAsync(options).ConfigureAwait(false);
///
async Task IDiscordClient.GetInviteAsync(string inviteId, RequestOptions options)
=> await GetInviteAsync(inviteId, options).ConfigureAwait(false);
///
Task IDiscordClient.GetGuildAsync(ulong id, CacheMode mode, RequestOptions options)
=> Task.FromResult(GetGuild(id));
///
Task> IDiscordClient.GetGuildsAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult>(Guilds);
///
async Task IDiscordClient.CreateGuildAsync(string name, IVoiceRegion region, Stream jpegIcon, RequestOptions options)
=> await CreateGuildAsync(name, region, jpegIcon, options).ConfigureAwait(false);
///
Task IDiscordClient.GetUserAsync(ulong id, CacheMode mode, RequestOptions options)
=> Task.FromResult(GetUser(id));
///
Task IDiscordClient.GetUserAsync(string username, string discriminator, RequestOptions options)
=> Task.FromResult(GetUser(username, discriminator));
///
Task IDiscordClient.GetVoiceRegionAsync(string id, RequestOptions options)
=> Task.FromResult(GetVoiceRegion(id));
///
Task> IDiscordClient.GetVoiceRegionsAsync(RequestOptions options)
=> Task.FromResult>(VoiceRegions);
}
}