Exposed RequestOptions

This commit is contained in:
RogueException
2016-10-06 02:44:41 -03:00
parent aa7d9ad414
commit f41df1f966
54 changed files with 793 additions and 766 deletions

View File

@@ -57,13 +57,16 @@ namespace Discord.Rest
roles.Add(roleIds[i]);
_roleIds = roles.ToImmutable();
}
public override async Task UpdateAsync()
=> Update(await UserHelper.GetAsync(this, Discord));
public Task ModifyAsync(Action<ModifyGuildMemberParams> func)
=> UserHelper.ModifyAsync(this, Discord, func);
public Task KickAsync()
=> UserHelper.KickAsync(this, Discord);
public override async Task UpdateAsync(RequestOptions options = null)
{
var model = await Discord.ApiClient.GetGuildMemberAsync(GuildId, Id, options);
Update(model);
}
public Task ModifyAsync(Action<ModifyGuildMemberParams> func, RequestOptions options = null)
=> UserHelper.ModifyAsync(this, Discord, func, options);
public Task KickAsync(RequestOptions options = null)
=> UserHelper.KickAsync(this, Discord, options);
public ChannelPermissions GetPermissions(IGuildChannel channel)
{

View File

@@ -35,11 +35,21 @@ namespace Discord.Rest
IsMfaEnabled = model.MfaEnabled.Value;
}
public override async Task UpdateAsync()
=> Update(await UserHelper.GetAsync(this, Discord));
public Task ModifyAsync(Action<ModifyCurrentUserParams> func)
=> UserHelper.ModifyAsync(this, Discord, func);
public override async Task UpdateAsync(RequestOptions options = null)
{
var model = await Discord.ApiClient.GetMyUserAsync(options);
if (model.Id != Id)
throw new InvalidOperationException("Unable to update this object using a different token.");
Update(model);
}
Task ISelfUser.ModifyStatusAsync(Action<ModifyPresenceParams> func) { throw new NotSupportedException(); }
public async Task ModifyAsync(Action<ModifyCurrentUserParams> func, RequestOptions options = null)
{
if (Id != Discord.CurrentUser.Id)
throw new InvalidOperationException("Unable to modify this object using a different token.");
await UserHelper.ModifyAsync(this, Discord, func, options);
}
Task ISelfUser.ModifyStatusAsync(Action<ModifyPresenceParams> func, RequestOptions options) { throw new NotSupportedException(); }
}
}

View File

@@ -39,20 +39,23 @@ namespace Discord.Rest
if (model.Username.IsSpecified)
Username = model.Username.Value;
}
public virtual async Task UpdateAsync()
=> Update(await UserHelper.GetAsync(this, Discord));
public Task<RestDMChannel> CreateDMChannelAsync()
=> UserHelper.CreateDMChannelAsync(this, Discord);
public virtual async Task UpdateAsync(RequestOptions options = null)
{
var model = await Discord.ApiClient.GetUserAsync(Id, options);
Update(model);
}
public Task<RestDMChannel> CreateDMChannelAsync(RequestOptions options = null)
=> UserHelper.CreateDMChannelAsync(this, Discord, options);
public override string ToString() => $"{Username}#{Discriminator}";
internal string DebuggerDisplay => $"{Username}#{Discriminator} ({Id}{(IsBot ? ", Bot" : "")})";
//IUser
Task<IDMChannel> IUser.GetDMChannelAsync(CacheMode mode)
Task<IDMChannel> IUser.GetDMChannelAsync(CacheMode mode, RequestOptions options)
=> Task.FromResult<IDMChannel>(null);
async Task<IDMChannel> IUser.CreateDMChannelAsync()
=> await CreateDMChannelAsync();
async Task<IDMChannel> IUser.CreateDMChannelAsync(RequestOptions options)
=> await CreateDMChannelAsync(options);
}
}

View File

@@ -1,53 +1,37 @@
using Discord.API.Rest;
using System;
using System.Threading.Tasks;
using MemberModel = Discord.API.GuildMember;
using Model = Discord.API.User;
namespace Discord.Rest
{
internal static class UserHelper
{
public static async Task<Model> GetAsync(IUser user, BaseDiscordClient client)
public static async Task ModifyAsync(ISelfUser user, BaseDiscordClient client, Action<ModifyCurrentUserParams> func,
RequestOptions options)
{
return await client.ApiClient.GetUserAsync(user.Id);
}
public static async Task<Model> GetAsync(ISelfUser user, BaseDiscordClient client)
{
var model = await client.ApiClient.GetMyUserAsync();
if (model.Id != user.Id)
throw new InvalidOperationException("Unable to update this object using a different token.");
return model;
}
public static async Task<MemberModel> GetAsync(IGuildUser user, BaseDiscordClient client)
{
return await client.ApiClient.GetGuildMemberAsync(user.GuildId, user.Id);
}
public static async Task ModifyAsync(ISelfUser user, BaseDiscordClient client, Action<ModifyCurrentUserParams> func)
{
if (user.Id != client.CurrentUser.Id)
throw new InvalidOperationException("Unable to modify this object using a different token.");
var args = new ModifyCurrentUserParams();
func(args);
await client.ApiClient.ModifySelfAsync(args);
await client.ApiClient.ModifySelfAsync(args, options);
}
public static async Task ModifyAsync(IGuildUser user, BaseDiscordClient client, Action<ModifyGuildMemberParams> func)
public static async Task ModifyAsync(IGuildUser user, BaseDiscordClient client, Action<ModifyGuildMemberParams> func,
RequestOptions options)
{
var args = new ModifyGuildMemberParams();
func(args);
await client.ApiClient.ModifyGuildMemberAsync(user.GuildId, user.Id, args);
await client.ApiClient.ModifyGuildMemberAsync(user.GuildId, user.Id, args, options);
}
public static async Task KickAsync(IGuildUser user, BaseDiscordClient client)
public static async Task KickAsync(IGuildUser user, BaseDiscordClient client,
RequestOptions options)
{
await client.ApiClient.RemoveGuildMemberAsync(user.GuildId, user.Id);
await client.ApiClient.RemoveGuildMemberAsync(user.GuildId, user.Id, options);
}
public static async Task<RestDMChannel> CreateDMChannelAsync(IUser user, BaseDiscordClient client)
public static async Task<RestDMChannel> CreateDMChannelAsync(IUser user, BaseDiscordClient client,
RequestOptions options)
{
var args = new CreateDMChannelParams(user.Id);
return RestDMChannel.Create(client, await client.ApiClient.CreateDMChannelAsync(args));
return RestDMChannel.Create(client, await client.ApiClient.CreateDMChannelAsync(args, options));
}
}
}