Changed GetPMChannel to GetOrCreatePMChannel, added SendPrivateMessage, hid User.PrivateChannel

This commit is contained in:
Brandon Smith
2015-08-30 16:46:52 -03:00
parent 0fcf1367de
commit 66d8eab986
3 changed files with 36 additions and 24 deletions

View File

@@ -57,9 +57,6 @@ namespace Discord
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)
=> CreatePMChannel(user?.Id);
/// <summary> Creates a new private channel with the provided user. </summary> /// <summary> Creates a new private channel with the provided user. </summary>
public async Task<Channel> CreatePMChannel(string userId) public async Task<Channel> CreatePMChannel(string userId)
{ {
@@ -278,6 +275,21 @@ namespace Discord
} }
return result; return result;
} }
/// <summary> Sends a private message to the provided channel. </summary>
public async Task<Message[]> SendPrivateMessage(User user, string text)
=> await SendMessage(await GetOrCreatePMChannel(user), text, new string[0]);
/// <summary> Sends a private message to the provided channel. </summary>
public async Task<Message[]> SendPrivateMessage(string userId, string text)
=> await SendMessage(await GetOrCreatePMChannel(userId), text, new string[0]);
/*/// <summary> Sends a private message to the provided user, 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[]> SendPrivateMessage(User user, string text, string[] mentions)
=> SendMessage(await GetOrCreatePMChannel(user), text, mentions);
/// <summary> Sends a private message to the provided user, 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[]> SendPrivateMessage(string userId, string text, string[] mentions)
=> SendMessage(await GetOrCreatePMChannel(userId), text, mentions);*/
/// <summary> Edits a message the provided message. </summary> /// <summary> Edits a message the provided message. </summary>
public Task EditMessage(Message message, string text) public Task EditMessage(Message message, string text)

View File

@@ -286,7 +286,8 @@ namespace Discord
} }
/// <summary> Returns the user with the specified id, or null if none was found. </summary> /// <summary> Returns the user with the specified id, or null if none was found. </summary>
public User GetUser(string id) => _users[id]; public User GetUser(string id)
=> _users[id];
/// <summary> Returns the user with the specified name and discriminator, or null if none was found. </summary> /// <summary> Returns the user with the specified name and discriminator, or null if none was found. </summary>
/// <remarks> Name formats supported: Name and @Name. Search is case-insensitive. </remarks> /// <remarks> Name formats supported: Name and @Name. Search is case-insensitive. </remarks>
public User GetUser(string name, string discriminator) public User GetUser(string name, string discriminator)
@@ -369,7 +370,8 @@ namespace Discord
} }
/// <summary> Returns the server with the specified id, or null if none was found. </summary> /// <summary> Returns the server with the specified id, or null if none was found. </summary>
public Server GetServer(string id) => _servers[id]; public Server GetServer(string id)
=> _servers[id];
/// <summary> Returns all servers with the specified name. </summary> /// <summary> Returns all servers with the specified name. </summary>
/// <remarks> Search is case-insensitive. </remarks> /// <remarks> Search is case-insensitive. </remarks>
public IEnumerable<Server> FindServers(string name) public IEnumerable<Server> FindServers(string name)
@@ -382,25 +384,21 @@ namespace Discord
/// <summary> Returns the channel with the specified id, or null if none was found. </summary> /// <summary> Returns the channel with the specified id, or null if none was found. </summary>
public Channel GetChannel(string id) => _channels[id]; public Channel GetChannel(string id) => _channels[id];
/// <summary> Returns a private channel with the provided user. </summary> /// <summary> Returns the private channel with the provided user, creating one if it does not currently exist. </summary>
public Task<Channel> GetPMChannel(string userId, bool createIfNotExists = false) public Task<Channel> GetOrCreatePMChannel(string userId)
=> GetPMChannel(_users[userId], createIfNotExists); => GetOrCreatePMChannel(_users[userId]);
/// <summary> Returns a private channel with the provided user. </summary> /// <summary> Returns the private channel with the provided user, creating one if it does not currently exist. </summary>
public async Task<Channel> GetPMChannel(User user, bool createIfNotExists = false) public async Task<Channel> GetOrCreatePMChannel(User user)
{ {
if (user == null) CheckReady();
{ if (user == null) throw new ArgumentNullException(nameof(user));
if (createIfNotExists)
throw new ArgumentNullException(nameof(user));
else
return null;
}
var channel = user.PrivateChannel; var channel = user.PrivateChannel;
if (channel == null && createIfNotExists) if (channel != null)
await CreatePMChannel(user); return channel;
return channel; return await CreatePMChannel(user?.Id);
} }
/// <summary> Returns all channels with the specified server and name. </summary> /// <summary> Returns all channels with the specified server and name. </summary>
/// <remarks> Name formats supported: Name and #Name. Search is case-insensitive. </remarks> /// <remarks> Name formats supported: Name and #Name. Search is case-insensitive. </remarks>
public IEnumerable<Channel> FindChannels(Server server, string name) public IEnumerable<Channel> FindChannels(Server server, string name)
@@ -426,7 +424,8 @@ namespace Discord
} }
/// <summary> Returns the role with the specified id, or null if none was found. </summary> /// <summary> Returns the role with the specified id, or null if none was found. </summary>
public Role GetRole(string id) => _roles[id]; public Role GetRole(string id)
=> _roles[id];
/// <summary> Returns all roles with the specified server and name. </summary> /// <summary> Returns all roles with the specified server and name. </summary>
/// <remarks> Name formats supported: Name and @Name. Search is case-insensitive. </remarks> /// <remarks> Name formats supported: Name and @Name. Search is case-insensitive. </remarks>
public IEnumerable<Role> FindRoles(Server server, string name) public IEnumerable<Role> FindRoles(Server server, string name)
@@ -452,6 +451,7 @@ namespace Discord
} }
/// <summary> Returns the message with the specified id, or null if none was found. </summary> /// <summary> Returns the message with the specified id, or null if none was found. </summary>
public Message GetMessage(string id) => _messages[id]; public Message GetMessage(string id)
=> _messages[id];
} }
} }

View File

@@ -31,8 +31,8 @@ namespace Discord
/// <summary> Returns the string "&lt;@Id&gt;" to be used as a shortcut when including mentions in text. </summary> /// <summary> Returns the string "&lt;@Id&gt;" to be used as a shortcut when including mentions in text. </summary>
public string Mention => $"<@{Id}>"; public string Mention => $"<@{Id}>";
public string PrivateChannelId { get; internal set; } internal string PrivateChannelId { get; internal set; }
public Channel PrivateChannel => _client.GetChannel(PrivateChannelId); internal Channel PrivateChannel => _client.GetChannel(PrivateChannelId);
//TODO: Add voice //TODO: Add voice
/// <summary> Returns the time this user last sent a message. </summary> /// <summary> Returns the time this user last sent a message. </summary>