You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserHelper.cs 3.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using Discord.API.Rest;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. using Model = Discord.API.User;
  6. using ImageModel = Discord.API.Image;
  7. using System.Linq;
  8. namespace Discord.Rest
  9. {
  10. internal static class UserHelper
  11. {
  12. public static async Task<Model> ModifyAsync(ISelfUser user, BaseDiscordClient client, Action<SelfUserProperties> func,
  13. RequestOptions options)
  14. {
  15. var args = new SelfUserProperties();
  16. func(args);
  17. var apiArgs = new API.Rest.ModifyCurrentUserParams
  18. {
  19. Avatar = args.Avatar.IsSpecified ? args.Avatar.Value?.ToModel() : Optional.Create<ImageModel?>(),
  20. Username = args.Username
  21. };
  22. if (!apiArgs.Avatar.IsSpecified && user.AvatarId != null)
  23. apiArgs.Avatar = new ImageModel(user.AvatarId);
  24. return await client.ApiClient.ModifySelfAsync(apiArgs, options).ConfigureAwait(false);
  25. }
  26. public static async Task<GuildUserProperties> ModifyAsync(IGuildUser user, BaseDiscordClient client, Action<GuildUserProperties> func,
  27. RequestOptions options)
  28. {
  29. var args = new GuildUserProperties();
  30. func(args);
  31. var apiArgs = new API.Rest.ModifyGuildMemberParams
  32. {
  33. Deaf = args.Deaf,
  34. Mute = args.Mute,
  35. Nickname = args.Nickname
  36. };
  37. if (args.Channel.IsSpecified)
  38. apiArgs.ChannelId = args.Channel.Value?.Id;
  39. else if (args.ChannelId.IsSpecified)
  40. apiArgs.ChannelId = args.ChannelId.Value;
  41. if (args.Roles.IsSpecified)
  42. apiArgs.RoleIds = args.Roles.Value.Select(x => x.Id).ToArray();
  43. else if (args.RoleIds.IsSpecified)
  44. apiArgs.RoleIds = args.RoleIds.Value.ToArray();
  45. /*
  46. * Ensure that the nick passed in the params of the request is not null.
  47. * string.Empty ("") is the only way to reset the user nick in the API,
  48. * a value of null does not. This is a workaround.
  49. */
  50. if (apiArgs.Nickname.IsSpecified && apiArgs.Nickname.Value == null)
  51. apiArgs.Nickname = new Optional<string>(string.Empty);
  52. await client.ApiClient.ModifyGuildMemberAsync(user.GuildId, user.Id, apiArgs, options).ConfigureAwait(false);
  53. return args;
  54. }
  55. public static async Task KickAsync(IGuildUser user, BaseDiscordClient client,
  56. string reason, RequestOptions options)
  57. {
  58. await client.ApiClient.RemoveGuildMemberAsync(user.GuildId, user.Id, reason, options).ConfigureAwait(false);
  59. }
  60. public static async Task<RestDMChannel> CreateDMChannelAsync(IUser user, BaseDiscordClient client,
  61. RequestOptions options)
  62. {
  63. var args = new CreateDMChannelParams(user.Id);
  64. return RestDMChannel.Create(client, await client.ApiClient.CreateDMChannelAsync(args, options).ConfigureAwait(false));
  65. }
  66. public static async Task AddRolesAsync(IGuildUser user, BaseDiscordClient client, IEnumerable<ulong> roleIds, RequestOptions options)
  67. {
  68. foreach (var roleId in roleIds)
  69. await client.ApiClient.AddRoleAsync(user.Guild.Id, user.Id, roleId, options).ConfigureAwait(false);
  70. }
  71. public static async Task RemoveRolesAsync(IGuildUser user, BaseDiscordClient client, IEnumerable<ulong> roleIds, RequestOptions options)
  72. {
  73. foreach (var roleId in roleIds)
  74. await client.ApiClient.RemoveRoleAsync(user.Guild.Id, user.Id, roleId, options).ConfigureAwait(false);
  75. }
  76. }
  77. }