create roles with icons (#2792)

This commit is contained in:
Mihail Gribkov
2023-11-18 23:35:51 +03:00
committed by GitHub
parent 52bc3b807e
commit b45b7743a5
4 changed files with 31 additions and 12 deletions

View File

@@ -875,11 +875,13 @@ namespace Discord
/// <param name="isHoisted">Whether the role is separated from others on the sidebar.</param> /// <param name="isHoisted">Whether the role is separated from others on the sidebar.</param>
/// <param name="isMentionable">Whether the role can be mentioned.</param> /// <param name="isMentionable">Whether the role can be mentioned.</param>
/// <param name="options">The options to be used when sending the request.</param> /// <param name="options">The options to be used when sending the request.</param>
/// <param name="icon">The icon for the role.</param>
/// <param name="emoji">The unicode emoji to be used as an icon for the role.</param>
/// <returns> /// <returns>
/// A task that represents the asynchronous creation operation. The task result contains the newly created /// A task that represents the asynchronous creation operation. The task result contains the newly created
/// role. /// role.
/// </returns> /// </returns>
Task<IRole> CreateRoleAsync(string name, GuildPermissions? permissions = null, Color? color = null, bool isHoisted = false, bool isMentionable = false, RequestOptions options = null); Task<IRole> CreateRoleAsync(string name, GuildPermissions? permissions = null, Color? color = null, bool isHoisted = false, bool isMentionable = false, RequestOptions options = null, Image? icon = null, Emoji emoji = null);
/// <summary> /// <summary>
/// Adds a user to this guild. /// Adds a user to this guild.

View File

@@ -4,6 +4,7 @@ using Discord.API.Rest;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Data;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -488,18 +489,30 @@ namespace Discord.Rest
#region Roles #region Roles
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null" />.</exception> /// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null" />.</exception>
public static async Task<RestRole> CreateRoleAsync(IGuild guild, BaseDiscordClient client, public static async Task<RestRole> CreateRoleAsync(IGuild guild, BaseDiscordClient client,
string name, GuildPermissions? permissions, Color? color, bool isHoisted, bool isMentionable, RequestOptions options) string name, GuildPermissions? permissions, Color? color, bool isHoisted, bool isMentionable, RequestOptions options, Image? icon, Emoji emoji)
{ {
if (name == null) if (name == null)
throw new ArgumentNullException(paramName: nameof(name)); throw new ArgumentNullException(paramName: nameof(name));
if (icon is not null || emoji is not null)
{
guild.Features.EnsureFeature(GuildFeature.RoleIcons);
if (icon is not null && emoji is not null)
{
throw new ArgumentException("Emoji and Icon properties cannot be present on a role at the same time.");
}
}
var createGuildRoleParams = new API.Rest.ModifyGuildRoleParams var createGuildRoleParams = new API.Rest.ModifyGuildRoleParams
{ {
Color = color?.RawValue ?? Optional.Create<uint>(), Color = color?.RawValue ?? Optional.Create<uint>(),
Hoist = isHoisted, Hoist = isHoisted,
Mentionable = isMentionable, Mentionable = isMentionable,
Name = name, Name = name,
Permissions = permissions?.RawValue.ToString() ?? Optional.Create<string>() Permissions = permissions?.RawValue.ToString() ?? Optional.Create<string>(),
Icon = icon?.ToModel(),
Emoji = emoji?.Name
}; };
var model = await client.ApiClient.CreateGuildRoleAsync(guild.Id, createGuildRoleParams, options).ConfigureAwait(false); var model = await client.ApiClient.CreateGuildRoleAsync(guild.Id, createGuildRoleParams, options).ConfigureAwait(false);

View File

@@ -812,14 +812,16 @@ namespace Discord.Rest
/// <param name="isHoisted">Whether the role is separated from others on the sidebar.</param> /// <param name="isHoisted">Whether the role is separated from others on the sidebar.</param>
/// <param name="options">The options to be used when sending the request.</param> /// <param name="options">The options to be used when sending the request.</param>
/// <param name="isMentionable">Whether the role can be mentioned.</param> /// <param name="isMentionable">Whether the role can be mentioned.</param>
/// <param name="icon">The icon for the role.</param>
/// <param name="emoji">The unicode emoji to be used as an icon for the role.</param>
/// <returns> /// <returns>
/// A task that represents the asynchronous creation operation. The task result contains the newly created /// A task that represents the asynchronous creation operation. The task result contains the newly created
/// role. /// role.
/// </returns> /// </returns>
public async Task<RestRole> CreateRoleAsync(string name, GuildPermissions? permissions = default(GuildPermissions?), Color? color = default(Color?), public async Task<RestRole> CreateRoleAsync(string name, GuildPermissions? permissions = default(GuildPermissions?), Color? color = default(Color?),
bool isHoisted = false, bool isMentionable = false, RequestOptions options = null) bool isHoisted = false, bool isMentionable = false, RequestOptions options = null, Image? icon = null, Emoji emoji = null)
{ {
var role = await GuildHelper.CreateRoleAsync(this, Discord, name, permissions, color, isHoisted, isMentionable, options).ConfigureAwait(false); var role = await GuildHelper.CreateRoleAsync(this, Discord, name, permissions, color, isHoisted, isMentionable, options, icon, emoji).ConfigureAwait(false);
_roles = _roles.Add(role.Id, role); _roles = _roles.Add(role.Id, role);
return role; return role;
} }
@@ -1485,8 +1487,8 @@ namespace Discord.Rest
async Task<IRole> IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options) async Task<IRole> IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options)
=> await CreateRoleAsync(name, permissions, color, isHoisted, false, options).ConfigureAwait(false); => await CreateRoleAsync(name, permissions, color, isHoisted, false, options).ConfigureAwait(false);
/// <inheritdoc /> /// <inheritdoc />
async Task<IRole> IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, bool isMentionable, RequestOptions options) async Task<IRole> IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, bool isMentionable, RequestOptions options, Image? icon, Emoji emoji)
=> await CreateRoleAsync(name, permissions, color, isHoisted, isMentionable, options).ConfigureAwait(false); => await CreateRoleAsync(name, permissions, color, isHoisted, isMentionable, options, icon, emoji).ConfigureAwait(false);
/// <inheritdoc /> /// <inheritdoc />
async Task<IGuildUser> IGuild.AddGuildUserAsync(ulong userId, string accessToken, Action<AddGuildUserProperties> func, RequestOptions options) async Task<IGuildUser> IGuild.AddGuildUserAsync(ulong userId, string accessToken, Action<AddGuildUserProperties> func, RequestOptions options)

View File

@@ -1084,14 +1084,16 @@ namespace Discord.WebSocket
/// <param name="isHoisted">Whether the role is separated from others on the sidebar.</param> /// <param name="isHoisted">Whether the role is separated from others on the sidebar.</param>
/// <param name="isMentionable">Whether the role can be mentioned.</param> /// <param name="isMentionable">Whether the role can be mentioned.</param>
/// <param name="options">The options to be used when sending the request.</param> /// <param name="options">The options to be used when sending the request.</param>
/// <param name="icon">The icon for the role.</param>
/// <param name="emoji">The unicode emoji to be used as an icon for the role.</param>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null"/>.</exception>
/// <returns> /// <returns>
/// A task that represents the asynchronous creation operation. The task result contains the newly created /// A task that represents the asynchronous creation operation. The task result contains the newly created
/// role. /// role.
/// </returns> /// </returns>
public Task<RestRole> CreateRoleAsync(string name, GuildPermissions? permissions = default(GuildPermissions?), Color? color = default(Color?), public Task<RestRole> CreateRoleAsync(string name, GuildPermissions? permissions = default(GuildPermissions?), Color? color = default(Color?),
bool isHoisted = false, bool isMentionable = false, RequestOptions options = null) bool isHoisted = false, bool isMentionable = false, RequestOptions options = null, Image? icon = null, Emoji emoji = null)
=> GuildHelper.CreateRoleAsync(this, Discord, name, permissions, color, isHoisted, isMentionable, options); => GuildHelper.CreateRoleAsync(this, Discord, name, permissions, color, isHoisted, isMentionable, options, icon, emoji);
internal SocketRole AddRole(RoleModel model) internal SocketRole AddRole(RoleModel model)
{ {
var role = SocketRole.Create(this, Discord.State, model); var role = SocketRole.Create(this, Discord.State, model);
@@ -2114,8 +2116,8 @@ namespace Discord.WebSocket
async Task<IRole> IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options) async Task<IRole> IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options)
=> await CreateRoleAsync(name, permissions, color, isHoisted, false, options).ConfigureAwait(false); => await CreateRoleAsync(name, permissions, color, isHoisted, false, options).ConfigureAwait(false);
/// <inheritdoc /> /// <inheritdoc />
async Task<IRole> IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, bool isMentionable, RequestOptions options) async Task<IRole> IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, bool isMentionable, RequestOptions options, Image? icon, Emoji emoji)
=> await CreateRoleAsync(name, permissions, color, isHoisted, isMentionable, options).ConfigureAwait(false); => await CreateRoleAsync(name, permissions, color, isHoisted, isMentionable, options, icon, emoji).ConfigureAwait(false);
/// <inheritdoc /> /// <inheritdoc />
async Task<IReadOnlyCollection<IGuildUser>> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options) async Task<IReadOnlyCollection<IGuildUser>> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options)