From b45b7743a5d59065c672932ad1dbf95dac460e5e Mon Sep 17 00:00:00 2001
From: Mihail Gribkov <61027276+Misha-133@users.noreply.github.com>
Date: Sat, 18 Nov 2023 23:35:51 +0300
Subject: [PATCH] create roles with icons (#2792)
---
.../Entities/Guilds/IGuild.cs | 4 +++-
.../Entities/Guilds/GuildHelper.cs | 19 ++++++++++++++++---
.../Entities/Guilds/RestGuild.cs | 10 ++++++----
.../Entities/Guilds/SocketGuild.cs | 10 ++++++----
4 files changed, 31 insertions(+), 12 deletions(-)
diff --git a/src/Discord.Net.Core/Entities/Guilds/IGuild.cs b/src/Discord.Net.Core/Entities/Guilds/IGuild.cs
index dfde4d82..086a92d3 100644
--- a/src/Discord.Net.Core/Entities/Guilds/IGuild.cs
+++ b/src/Discord.Net.Core/Entities/Guilds/IGuild.cs
@@ -875,11 +875,13 @@ namespace Discord
/// Whether the role is separated from others on the sidebar.
/// Whether the role can be mentioned.
/// The options to be used when sending the request.
+ /// The icon for the role.
+ /// The unicode emoji to be used as an icon for the role.
///
/// A task that represents the asynchronous creation operation. The task result contains the newly created
/// role.
///
- Task CreateRoleAsync(string name, GuildPermissions? permissions = null, Color? color = null, bool isHoisted = false, bool isMentionable = false, RequestOptions options = null);
+ Task CreateRoleAsync(string name, GuildPermissions? permissions = null, Color? color = null, bool isHoisted = false, bool isMentionable = false, RequestOptions options = null, Image? icon = null, Emoji emoji = null);
///
/// Adds a user to this guild.
diff --git a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
index d837b8b9..b88ef5ad 100644
--- a/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
+++ b/src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
@@ -4,6 +4,7 @@ using Discord.API.Rest;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
+using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
@@ -488,18 +489,30 @@ namespace Discord.Rest
#region Roles
/// is .
public static async Task 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)
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
{
- Color = color?.RawValue ?? Optional.Create(),
+ Color = color?.RawValue ?? Optional.Create(),
Hoist = isHoisted,
Mentionable = isMentionable,
Name = name,
- Permissions = permissions?.RawValue.ToString() ?? Optional.Create()
+ Permissions = permissions?.RawValue.ToString() ?? Optional.Create(),
+ Icon = icon?.ToModel(),
+ Emoji = emoji?.Name
};
var model = await client.ApiClient.CreateGuildRoleAsync(guild.Id, createGuildRoleParams, options).ConfigureAwait(false);
diff --git a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
index 880e97dc..82a0fe80 100644
--- a/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
+++ b/src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
@@ -812,14 +812,16 @@ namespace Discord.Rest
/// Whether the role is separated from others on the sidebar.
/// The options to be used when sending the request.
/// Whether the role can be mentioned.
+ /// The icon for the role.
+ /// The unicode emoji to be used as an icon for the role.
///
/// A task that represents the asynchronous creation operation. The task result contains the newly created
/// role.
///
public async Task 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);
return role;
}
@@ -1485,8 +1487,8 @@ namespace Discord.Rest
async Task IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options)
=> await CreateRoleAsync(name, permissions, color, isHoisted, false, options).ConfigureAwait(false);
///
- async Task IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, bool isMentionable, RequestOptions options)
- => await CreateRoleAsync(name, permissions, color, isHoisted, isMentionable, options).ConfigureAwait(false);
+ async Task 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, icon, emoji).ConfigureAwait(false);
///
async Task IGuild.AddGuildUserAsync(ulong userId, string accessToken, Action func, RequestOptions options)
diff --git a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs
index 8e814571..53c5c93e 100644
--- a/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs
+++ b/src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs
@@ -1084,14 +1084,16 @@ namespace Discord.WebSocket
/// Whether the role is separated from others on the sidebar.
/// Whether the role can be mentioned.
/// The options to be used when sending the request.
+ /// The icon for the role.
+ /// The unicode emoji to be used as an icon for the role.
/// is .
///
/// A task that represents the asynchronous creation operation. The task result contains the newly created
/// role.
///
public Task CreateRoleAsync(string name, GuildPermissions? permissions = default(GuildPermissions?), Color? color = default(Color?),
- bool isHoisted = false, bool isMentionable = false, RequestOptions options = null)
- => GuildHelper.CreateRoleAsync(this, Discord, name, permissions, color, isHoisted, isMentionable, options);
+ 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, icon, emoji);
internal SocketRole AddRole(RoleModel model)
{
var role = SocketRole.Create(this, Discord.State, model);
@@ -2114,8 +2116,8 @@ namespace Discord.WebSocket
async Task IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, RequestOptions options)
=> await CreateRoleAsync(name, permissions, color, isHoisted, false, options).ConfigureAwait(false);
///
- async Task IGuild.CreateRoleAsync(string name, GuildPermissions? permissions, Color? color, bool isHoisted, bool isMentionable, RequestOptions options)
- => await CreateRoleAsync(name, permissions, color, isHoisted, isMentionable, options).ConfigureAwait(false);
+ async Task 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, icon, emoji).ConfigureAwait(false);
///
async Task> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options)