@@ -582,7 +582,8 @@ namespace Discord
}
}
//Auth
//Auth
public async Task<string> Connect(string token)
/// <summary> Connects to the Discord server with the provided token. </summary>
public async Task Connect(string token)
{
{
_isStopping.Reset();
_isStopping.Reset();
@@ -590,8 +591,9 @@ namespace Discord
await _webSocket.ConnectAsync(Endpoints.WebSocket_Hub, true);
await _webSocket.ConnectAsync(Endpoints.WebSocket_Hub, true);
_isReady = true;
_isReady = true;
return token;
}
}
/// <summary> Connects to the Discord server with the provided email and password. </summary>
/// <returns> Returns a token for future connections. </returns>
public async Task<string> Connect(string email, string password)
public async Task<string> Connect(string email, string password)
{
{
_isStopping.Reset();
_isStopping.Reset();
@@ -608,17 +610,22 @@ namespace Discord
_isReady = true;
_isReady = true;
return response.Token;
return response.Token;
}
}
/// <summary> Connects to the Discord server with the provided token, and will fall back to username and password. </summary>
/// <returns> Returns a token for future connections. </returns>
public async Task<string> Connect(string email, string password, string token)
public async Task<string> Connect(string email, string password, string token)
{
{
try
try
{
{
return await Connect(token);
await Connect(token);
return token;
}
}
catch (InvalidOperationException) //Bad Token
catch (InvalidOperationException) //Bad Token
{
{
return await Connect(email, password);
return await Connect(email, password);
}
}
}
}
/// <summary> Connects to the Discord server as an anonymous user with the provided username. </summary>
/// <returns> Returns a token for future connections. </returns>
public async Task<string> ConnectAnonymous(string username)
public async Task<string> ConnectAnonymous(string username)
{
{
_isStopping.Reset();
_isStopping.Reset();
@@ -635,6 +642,7 @@ namespace Discord
_isReady = true;
_isReady = true;
return response.Token;
return response.Token;
}
}
/// <summary> Disconnects from the Discord server, canceling any pending requests. </summary>
public async Task Disconnect()
public async Task Disconnect()
{
{
_isReady = false;
_isReady = false;
@@ -649,14 +657,17 @@ namespace Discord
}
}
//Servers
//Servers
/// <summary> Creates a new server with the provided name and region (see Regions). </summary>
public async Task<Server> CreateServer(string name, string region)
public async Task<Server> CreateServer(string name, string region)
{
{
CheckReady();
CheckReady();
var response = await DiscordAPI.CreateServer(name, region);
var response = await DiscordAPI.CreateServer(name, region);
return _servers.Update(response.Id, response);
return _servers.Update(response.Id, response);
}
}
/// <summary> Leaves the provided server, destroying it if you are the owner. </summary>
public Task<Server> LeaveServer(Server server)
public Task<Server> LeaveServer(Server server)
=> LeaveServer(server.Id);
=> LeaveServer(server.Id);
/// <summary> Leaves the provided server, destroying it if you are the owner. </summary>
public async Task<Server> LeaveServer(string serverId)
public async Task<Server> LeaveServer(string serverId)
{
{
CheckReady();
CheckReady();
@@ -669,25 +680,31 @@ namespace Discord
}
}
//Channels
//Channels
/// <summary> Creates a new channel with the provided name and type (see ChannelTypes). </summary>
public Task<Channel> CreateChannel(Server server, string name, string type)
public Task<Channel> CreateChannel(Server server, string name, string type)
=> CreateChannel(server.Id, name, type);
=> CreateChannel(server.Id, name, type);
public async Task<Channel> CreateChannel(string serverId, string name, string type)
/// <summary> Creates a new channel with the provided name and type (see ChannelTypes). </summary>
public async Task<Channel> CreateChannel(string serverId, string name, string type)
{
{
CheckReady();
CheckReady();
var response = await DiscordAPI.CreateChannel(serverId, name, type);
var response = await DiscordAPI.CreateChannel(serverId, name, type);
return _channels.Update(response.Id, serverId, response);
return _channels.Update(response.Id, serverId, response);
}
}
/// <summary> Creates a new private channel with the provided user. </summary>
public Task<Channel> CreatePMChannel(User user)
public Task<Channel> CreatePMChannel(User user)
=> CreatePMChannel(user.Id);
=> CreatePMChannel(user.Id);
public async Task<Channel> CreatePMChannel(string recipientId)
/// <summary> Creates a new private channel with the provided user. </summary>
public async Task<Channel> CreatePMChannel(string userId)
{
{
CheckReady();
CheckReady();
var response = await DiscordAPI.CreatePMChannel(UserId, recipient Id);
var response = await DiscordAPI.CreatePMChannel(UserId, user Id);
return _channels.Update(response.Id, response);
return _channels.Update(response.Id, response);
}
}
/// <summary> Destroys the provided channel. </summary>
public Task<Channel> DestroyChannel(Channel channel)
public Task<Channel> DestroyChannel(Channel channel)
=> DestroyChannel(channel.Id);
=> DestroyChannel(channel.Id);
public async Task<Channel> DestroyChannel(string channelId)
/// <summary> Destroys the provided channel. </summary>
public async Task<Channel> DestroyChannel(string channelId)
{
{
CheckReady();
CheckReady();
try
try
@@ -699,23 +716,31 @@ namespace Discord
}
}
//Bans
//Bans
/// <summary> Bans a user from the provided server. </summary>
public Task Ban(Server server, User user)
public Task Ban(Server server, User user)
=> Ban(server.Id, user.Id);
=> Ban(server.Id, user.Id);
/// <summary> Bans a user from the provided server. </summary>
public Task Ban(Server server, string userId)
public Task Ban(Server server, string userId)
=> Ban(server.Id, userId);
=> Ban(server.Id, userId);
/// <summary> Bans a user from the provided server. </summary>
public Task Ban(string server, User user)
public Task Ban(string server, User user)
=> Ban(server, user.Id);
=> Ban(server, user.Id);
/// <summary> Bans a user from the provided server. </summary>
public Task Ban(string serverId, string userId)
public Task Ban(string serverId, string userId)
{
{
CheckReady();
CheckReady();
return DiscordAPI.Ban(serverId, userId);
return DiscordAPI.Ban(serverId, userId);
}
}
/// <summary> Unbans a user from the provided server. </summary>
public Task Unban(Server server, User user)
public Task Unban(Server server, User user)
=> Unban(server.Id, user.Id);
=> Unban(server.Id, user.Id);
/// <summary> Unbans a user from the provided server. </summary>
public Task Unban(Server server, string userId)
public Task Unban(Server server, string userId)
=> Unban(server.Id, userId);
=> Unban(server.Id, userId);
/// <summary> Unbans a user from the provided server. </summary>
public Task Unban(string server, User user)
public Task Unban(string server, User user)
=> Unban(server, user.Id);
=> Unban(server, user.Id);
/// <summary> Unbans a user from the provided server. </summary>
public async Task Unban(string serverId, string userId)
public async Task Unban(string serverId, string userId)
{
{
CheckReady();
CheckReady();
@@ -727,15 +752,30 @@ namespace Discord
}
}
//Invites
//Invites
/// <summary> Creates a new invite to the default channel of the provided server. </summary>
/// <param name="maxAge"> Time (in seconds) until the invite expires. Set to 0 to never expire. </param>
/// <param name="isTemporary"> If true, a user accepting this invite will be kicked from the server after closing their client. </param>
/// <param name="hasXkcdPass"> If true, creates a human-readable link. Not supported if maxAge is set to 0. </param>
/// <param name="maxUses"> The max amount of times this invite may be used. </param>
public Task<Invite> CreateInvite(Server server, int maxAge, int maxUses, bool isTemporary, bool hasXkcdPass)
public Task<Invite> CreateInvite(Server server, int maxAge, int maxUses, bool isTemporary, bool hasXkcdPass)
{
{
return CreateInvite(server.DefaultChannelId, maxAge, maxUses, isTemporary, hasXkcdPass);
return CreateInvite(server.DefaultChannelId, maxAge, maxUses, isTemporary, hasXkcdPass);
}
}
/// <summary> Creates a new invite to the provided channel. </summary>
/// <param name="maxAge"> Time (in seconds) until the invite expires. Set to 0 to never expire. </param>
/// <param name="isTemporary"> If true, a user accepting this invite will be kicked from the server after closing their client. </param>
/// <param name="hasXkcdPass"> If true, creates a human-readable link. Not supported if maxAge is set to 0. </param>
/// <param name="maxUses"> The max amount of times this invite may be used. </param>
public Task<Invite> CreateInvite(Channel channel, int maxAge, int maxUses, bool isTemporary, bool hasXkcdPass)
public Task<Invite> CreateInvite(Channel channel, int maxAge, int maxUses, bool isTemporary, bool hasXkcdPass)
{
{
return CreateInvite(channel, maxAge, maxUses, isTemporary, hasXkcdPass);
return CreateInvite(channel, maxAge, maxUses, isTemporary, hasXkcdPass);
}
}
public async Task<Invite> CreateInvite(string channelId, int maxAge, int maxUses, bool isTemporary, bool hasXkcdPass)
/// <summary> Creates a new invite to the provided channel. </summary>
/// <param name="maxAge"> Time (in seconds) until the invite expires. Set to 0 to never expire. </param>
/// <param name="isTemporary"> If true, a user accepting this invite will be kicked from the server after closing their client. </param>
/// <param name="hasXkcdPass"> If true, creates a human-readable link. Not supported if maxAge is set to 0. </param>
/// <param name="maxUses"> The max amount of times this invite may be used. </param>
public async Task<Invite> CreateInvite(string channelId, int maxAge, int maxUses, bool isTemporary, bool hasXkcdPass)
{
{
CheckReady();
CheckReady();
var response = await DiscordAPI.CreateInvite(channelId, maxAge, maxUses, isTemporary, hasXkcdPass);
var response = await DiscordAPI.CreateInvite(channelId, maxAge, maxUses, isTemporary, hasXkcdPass);
@@ -754,7 +794,9 @@ namespace Discord
Uses = response.Uses
Uses = response.Uses
};
};
}
}
public async Task<Invite> GetInvite(string id)
/// <summary> Gets more info about the provided invite. </summary>
/// <remarks> Supported formats: inviteCode, xkcdCode, https://discord.gg/inviteCode, https://discord.gg/xkcdCode </remarks>
public async Task<Invite> GetInvite(string id)
{
{
CheckReady();
CheckReady();
var response = await DiscordAPI.GetInvite(id);
var response = await DiscordAPI.GetInvite(id);
@@ -765,11 +807,13 @@ namespace Discord
ServerId = response.Server.Id
ServerId = response.Server.Id
};
};
}
}
/// <summary> Accepts the provided invite. </summary>
public Task AcceptInvite(Invite invite)
public Task AcceptInvite(Invite invite)
{
{
CheckReady();
CheckReady();
return DiscordAPI.AcceptInvite(invite.Code);
return DiscordAPI.AcceptInvite(invite.Code);
}
}
/// <summary> Accepts the provided invite. </summary>
public async Task AcceptInvite(string id)
public async Task AcceptInvite(string id)
{
{
CheckReady();
CheckReady();
@@ -786,6 +830,7 @@ namespace Discord
var response = await DiscordAPI.GetInvite(id);
var response = await DiscordAPI.GetInvite(id);
await DiscordAPI.AcceptInvite(response.Code);
await DiscordAPI.AcceptInvite(response.Code);
}
}
/// <summary> Deletes the provided invite. </summary>
public async Task DeleteInvite(string id)
public async Task DeleteInvite(string id)
{
{
CheckReady();
CheckReady();
@@ -799,13 +844,19 @@ namespace Discord
}
}
//Chat
//Chat
/// <summary> Sends a message to the provided channel. </summary>
public Task<Message[]> SendMessage(Channel channel, string text)
public Task<Message[]> SendMessage(Channel channel, string text)
=> SendMessage(channel.Id, text, new string[0]);
=> SendMessage(channel.Id, text, new string[0]);
/// <summary> Sends a message to the provided channel. </summary>
public Task<Message[]> SendMessage(string channelId, string text)
public Task<Message[]> SendMessage(string channelId, string text)
=> SendMessage(channelId, text, new string[0]);
=> SendMessage(channelId, text, new string[0]);
/// <summary> Sends a message to the provided channel, mentioning certain users. </summary>
/// <remarks> While not required, it is recommended to include a mention reference in the text (see User.Mention). </remarks>
public Task<Message[]> SendMessage(Channel channel, string text, string[] mentions)
public Task<Message[]> SendMessage(Channel channel, string text, string[] mentions)
=> SendMessage(channel.Id, text, mentions);
=> SendMessage(channel.Id, text, mentions);
public async Task<Message[]> SendMessage(string channelId, string text, string[] mentions)
/// <summary> Sends a message to the provided channel, mentioning certain users. </summary>
/// <remarks> While not required, it is recommended to include a mention reference in the text (see User.Mention). </remarks>
public async Task<Message[]> SendMessage(string channelId, string text, string[] mentions)
{
{
CheckReady();
CheckReady();
@@ -829,16 +880,25 @@ namespace Discord
}
}
}
}
/// <summary> Edits a message the provided message. </summary>
public Task EditMessage(Message message, string text)
public Task EditMessage(Message message, string text)
=> EditMessage(message.ChannelId, message.Id, text, new string[0]);
=> EditMessage(message.ChannelId, message.Id, text, new string[0]);
/// <summary> Edits a message the provided message. </summary>
public Task EditMessage(Channel channel, string messageId, string text)
public Task EditMessage(Channel channel, string messageId, string text)
=> EditMessage(channel.Id, messageId, text, new string[0]);
=> EditMessage(channel.Id, messageId, text, new string[0]);
/// <summary> Edits a message the provided message. </summary>
public Task EditMessage(string channelId, string messageId, string text)
public Task EditMessage(string channelId, string messageId, string text)
=> EditMessage(channelId, messageId, text, new string[0]);
=> EditMessage(channelId, messageId, text, new string[0]);
/// <summary> Edits a message the provided message, mentioning certain users. </summary>
/// <remarks> While not required, it is recommended to include a mention reference in the text (see User.Mention). </remarks>
public Task EditMessage(Message message, string text, string[] mentions)
public Task EditMessage(Message message, string text, string[] mentions)
=> EditMessage(message.ChannelId, message.Id, text, mentions);
=> EditMessage(message.ChannelId, message.Id, text, mentions);
/// <summary> Edits a message the provided message, mentioning certain users. </summary>
/// <remarks> While not required, it is recommended to include a mention reference in the text (see User.Mention). </remarks>
public Task EditMessage(Channel channel, string messageId, string text, string[] mentions)
public Task EditMessage(Channel channel, string messageId, string text, string[] mentions)
=> EditMessage(channel.Id, messageId, text, mentions);
=> EditMessage(channel.Id, messageId, text, mentions);
/// <summary> Edits a message the provided message, mentioning certain users. </summary>
/// <remarks> While not required, it is recommended to include a mention reference in the text (see User.Mention). </remarks>
public async Task EditMessage(string channelId, string messageId, string text, string[] mentions)
public async Task EditMessage(string channelId, string messageId, string text, string[] mentions)
{
{
CheckReady();
CheckReady();
@@ -849,8 +909,10 @@ namespace Discord
_messages.Update(msg.Id, channelId, msg);
_messages.Update(msg.Id, channelId, msg);
}
}
/// <summary> Deletes the provided message. </summary>
public Task DeleteMessage(Message msg)
public Task DeleteMessage(Message msg)
=> DeleteMessage(msg.ChannelId, msg.Id);
=> DeleteMessage(msg.ChannelId, msg.Id);
/// <summary> Deletes the provided message. </summary>
public async Task<Message> DeleteMessage(string channelId, string msgId)
public async Task<Message> DeleteMessage(string channelId, string msgId)
{
{
try
try
@@ -863,12 +925,18 @@ namespace Discord
return null;
return null;
}
}
/// <summary> Sends a file to the provided channel. </summary>
public Task SendFile(Channel channel, string path)
public Task SendFile(Channel channel, string path)
=> SendFile(channel.Id, path);
=> SendFile(channel.Id, path);
/// <summary> Sends a file to the provided channel. </summary>
public Task SendFile(string channelId, string path)
public Task SendFile(string channelId, string path)
=> SendFile(channelId, File.OpenRead(path), Path.GetFileName(path));
=> SendFile(channelId, File.OpenRead(path), Path.GetFileName(path));
/// <summary> Reads a stream and sends it to the provided channel as a file. </summary>
/// <remarks> It is highly recommended that this stream be cached in memory or on disk, or the request may time out. </remarks>
public Task SendFile(Channel channel, Stream stream, string filename = null)
public Task SendFile(Channel channel, Stream stream, string filename = null)
=> SendFile(channel.Id, stream, filename);
=> SendFile(channel.Id, stream, filename);
/// <summary> Reads a stream and sends it to the provided channel as a file. </summary>
/// <remarks> It is highly recommended that this stream be cached in memory or on disk, or the request may time out. </remarks>
public Task SendFile(string channelId, Stream stream, string filename = null)
public Task SendFile(string channelId, Stream stream, string filename = null)
{
{
return DiscordAPI.SendFile(channelId, stream, filename);
return DiscordAPI.SendFile(channelId, stream, filename);
@@ -876,48 +944,64 @@ namespace Discord
//Voice
//Voice
/// <summary> Mutes a user on the provided server. </summary>
public Task Mute(Server server, User user)
public Task Mute(Server server, User user)
=> Mute(server.Id, user.Id);
=> Mute(server.Id, user.Id);
/// <summary> Mutes a user on the provided server. </summary>
public Task Mute(Server server, string userId)
public Task Mute(Server server, string userId)
=> Mute(server.Id, userId);
=> Mute(server.Id, userId);
/// <summary> Mutes a user on the provided server. </summary>
public Task Mute(string server, User user)
public Task Mute(string server, User user)
=> Mute(server, user.Id);
=> Mute(server, user.Id);
/// <summary> Mutes a user on the provided server. </summary>
public Task Mute(string serverId, string userId)
public Task Mute(string serverId, string userId)
{
{
CheckReady();
CheckReady();
return DiscordAPI.Mute(serverId, userId);
return DiscordAPI.Mute(serverId, userId);
}
}
/// <summary> Unmutes a user on the provided server. </summary>
public Task Unmute(Server server, User user)
public Task Unmute(Server server, User user)
=> Unmute(server.Id, user.Id);
=> Unmute(server.Id, user.Id);
/// <summary> Unmutes a user on the provided server. </summary>
public Task Unmute(Server server, string userId)
public Task Unmute(Server server, string userId)
=> Unmute(server.Id, userId);
=> Unmute(server.Id, userId);
/// <summary> Unmutes a user on the provided server. </summary>
public Task Unmute(string server, User user)
public Task Unmute(string server, User user)
=> Unmute(server, user.Id);
=> Unmute(server, user.Id);
/// <summary> Unmutes a user on the provided server. </summary>
public Task Unmute(string serverId, string userId)
public Task Unmute(string serverId, string userId)
{
{
CheckReady();
CheckReady();
return DiscordAPI.Unmute(serverId, userId);
return DiscordAPI.Unmute(serverId, userId);
}
}
/// <summary> Deafens a user on the provided server. </summary>
public Task Deafen(Server server, User user)
public Task Deafen(Server server, User user)
=> Deafen(server.Id, user.Id);
=> Deafen(server.Id, user.Id);
/// <summary> Deafens a user on the provided server. </summary>
public Task Deafen(Server server, string userId)
public Task Deafen(Server server, string userId)
=> Deafen(server.Id, userId);
=> Deafen(server.Id, userId);
/// <summary> Deafens a user on the provided server. </summary>
public Task Deafen(string server, User user)
public Task Deafen(string server, User user)
=> Deafen(server, user.Id);
=> Deafen(server, user.Id);
/// <summary> Deafens a user on the provided server. </summary>
public Task Deafen(string serverId, string userId)
public Task Deafen(string serverId, string userId)
{
{
CheckReady();
CheckReady();
return DiscordAPI.Deafen(serverId, userId);
return DiscordAPI.Deafen(serverId, userId);
}
}
/// <summary> Undeafens a user on the provided server. </summary>
public Task Undeafen(Server server, User user)
public Task Undeafen(Server server, User user)
=> Undeafen(server.Id, user.Id);
=> Undeafen(server.Id, user.Id);
/// <summary> Undeafens a user on the provided server. </summary>
public Task Undeafen(Server server, string userId)
public Task Undeafen(Server server, string userId)
=> Undeafen(server.Id, userId);
=> Undeafen(server.Id, userId);
/// <summary> Undeafens a user on the provided server. </summary>
public Task Undeafen(string server, User user)
public Task Undeafen(string server, User user)
=> Undeafen(server, user.Id);
=> Undeafen(server, user.Id);
/// <summary> Undeafens a user on the provided server. </summary>
public Task Undeafen(string serverId, string userId)
public Task Undeafen(string serverId, string userId)
{
{
CheckReady();
CheckReady();
@@ -925,18 +1009,21 @@ namespace Discord
}
}
//Profile
//Profile
/// <summary> Changes your username to newName. </summary>
public async Task ChangeUsername(string newName, string currentEmail, string currentPassword)
public async Task ChangeUsername(string newName, string currentEmail, string currentPassword)
{
{
CheckReady();
CheckReady();
var response = await DiscordAPI.ChangeUsername(newName, currentEmail, currentPassword);
var response = await DiscordAPI.ChangeUsername(newName, currentEmail, currentPassword);
_users.Update(response.Id, response);
_users.Update(response.Id, response);
}
}
/// <summary> Changes your email to newEmail. </summary>
public async Task ChangeEmail(string newEmail, string currentPassword)
public async Task ChangeEmail(string newEmail, string currentPassword)
{
{
CheckReady();
CheckReady();
var response = await DiscordAPI.ChangeEmail(newEmail, currentPassword);
var response = await DiscordAPI.ChangeEmail(newEmail, currentPassword);
_users.Update(response.Id, response);
_users.Update(response.Id, response);
}
}
/// <summary> Changes your password to newPassword. </summary>
public async Task ChangePassword(string newPassword, string currentEmail, string currentPassword)
public async Task ChangePassword(string newPassword, string currentEmail, string currentPassword)
{
{
CheckReady();
CheckReady();