Added EditUser(rolesMode) to allow setting, adding or removing roles
This commit is contained in:
@@ -218,16 +218,22 @@ namespace Discord
|
||||
return query;
|
||||
}
|
||||
|
||||
public Task EditUser(User user, bool? mute = null, bool? deaf = null, IEnumerable<Role> roles = null)
|
||||
public Task EditUser(User user, bool? mute = null, bool? deaf = null, IEnumerable<Role> roles = null, EditMode rolesMode = EditMode.Set)
|
||||
{
|
||||
if (user == null) throw new ArgumentNullException(nameof(user));
|
||||
if (user.IsPrivate) throw new InvalidOperationException("Unable to edit users in a private channel");
|
||||
CheckReady();
|
||||
|
||||
//Modify the roles collection and filter out the everyone role
|
||||
IEnumerable<long> roleIds = roles == null ? null : user.Roles
|
||||
.Modify(roles, rolesMode)
|
||||
.Where(x => !x.IsEveryone)
|
||||
.Select(x => x.Id);
|
||||
|
||||
var serverId = user.Server.Id;
|
||||
return _api.EditUser(serverId, user.Id,
|
||||
mute: mute, deaf: deaf,
|
||||
roleIds: roles.Select(x => x.Id).Where(x => x != serverId));
|
||||
roleIds: roleIds);
|
||||
}
|
||||
|
||||
public Task KickUser(User user)
|
||||
|
||||
@@ -1,35 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Discord
|
||||
{
|
||||
internal static class Extensions
|
||||
public enum EditMode : byte
|
||||
{
|
||||
public static async Task Timeout(this Task self, int milliseconds)
|
||||
Set,
|
||||
Add,
|
||||
Remove
|
||||
}
|
||||
|
||||
internal static class Extensions
|
||||
{
|
||||
public static async Task Timeout(this Task task, int milliseconds)
|
||||
{
|
||||
Task timeoutTask = Task.Delay(milliseconds);
|
||||
Task finishedTask = await Task.WhenAny(self, timeoutTask).ConfigureAwait(false);
|
||||
Task finishedTask = await Task.WhenAny(task, timeoutTask).ConfigureAwait(false);
|
||||
if (finishedTask == timeoutTask)
|
||||
throw new TimeoutException();
|
||||
else
|
||||
await self.ConfigureAwait(false);
|
||||
await task.ConfigureAwait(false);
|
||||
}
|
||||
public static async Task<T> Timeout<T>(this Task<T> self, int milliseconds)
|
||||
public static async Task<T> Timeout<T>(this Task<T> task, int milliseconds)
|
||||
{
|
||||
Task timeoutTask = Task.Delay(milliseconds);
|
||||
Task finishedTask = await Task.WhenAny(self, timeoutTask).ConfigureAwait(false);
|
||||
Task finishedTask = await Task.WhenAny(task, timeoutTask).ConfigureAwait(false);
|
||||
if (finishedTask == timeoutTask)
|
||||
throw new TimeoutException();
|
||||
else
|
||||
return await self.ConfigureAwait(false);
|
||||
return await task.ConfigureAwait(false);
|
||||
}
|
||||
public static async Task Timeout(this Task self, int milliseconds, CancellationTokenSource timeoutToken)
|
||||
public static async Task Timeout(this Task task, int milliseconds, CancellationTokenSource timeoutToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
timeoutToken.CancelAfter(milliseconds);
|
||||
await self.ConfigureAwait(false);
|
||||
await task.ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
@@ -38,12 +47,12 @@ namespace Discord
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public static async Task<T> Timeout<T>(this Task<T> self, int milliseconds, CancellationTokenSource timeoutToken)
|
||||
public static async Task<T> Timeout<T>(this Task<T> task, int milliseconds, CancellationTokenSource timeoutToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
timeoutToken.CancelAfter(milliseconds);
|
||||
return await self.ConfigureAwait(false);
|
||||
return await task.ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
@@ -64,5 +73,20 @@ namespace Discord
|
||||
try { await Task.Delay(-1, token).ConfigureAwait(false); }
|
||||
catch (OperationCanceledException) { } //Expected
|
||||
}
|
||||
}
|
||||
|
||||
public static IEnumerable<T> Modify<T>(this IEnumerable<T> original, IEnumerable<T> modified, EditMode mode)
|
||||
{
|
||||
if (original == null) return null;
|
||||
switch (mode)
|
||||
{
|
||||
case EditMode.Set:
|
||||
default:
|
||||
return modified;
|
||||
case EditMode.Add:
|
||||
return original.Concat(modified);
|
||||
case EditMode.Remove:
|
||||
return original.Except(modified);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user