Skip to content

Commit

Permalink
create roles with icons (#2792)
Browse files Browse the repository at this point in the history
  • Loading branch information
Misha-133 committed Nov 18, 2023
1 parent 52bc3b8 commit b45b774
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/Discord.Net.Core/Entities/Guilds/IGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -875,11 +875,13 @@ public interface IGuild : IDeletable, ISnowflakeEntity
/// <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="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>
/// A task that represents the asynchronous creation operation. The task result contains the newly created
/// role.
/// </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>
/// Adds a user to this guild.
Expand Down
19 changes: 16 additions & 3 deletions src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -488,18 +489,30 @@ public static async Task<RestBan> GetBanAsync(IGuild guild, BaseDiscordClient cl
#region Roles
/// <exception cref="ArgumentNullException"><paramref name="name"/> is <see langword="null" />.</exception>
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)
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<uint>(),
Color = color?.RawValue ?? Optional.Create<uint>(),
Hoist = isHoisted,
Mentionable = isMentionable,
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);
Expand Down
10 changes: 6 additions & 4 deletions src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -812,14 +812,16 @@ public RestRole GetRole(ulong id)
/// <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="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>
/// A task that represents the asynchronous creation operation. The task result contains the newly created
/// role.
/// </returns>
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);
return role;
}
Expand Down Expand Up @@ -1485,8 +1487,8 @@ IRole IGuild.GetRole(ulong id)
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);
/// <inheritdoc />
async Task<IRole> 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<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, icon, emoji).ConfigureAwait(false);

/// <inheritdoc />
async Task<IGuildUser> IGuild.AddGuildUserAsync(ulong userId, string accessToken, Action<AddGuildUserProperties> func, RequestOptions options)
Expand Down
10 changes: 6 additions & 4 deletions src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,14 +1084,16 @@ public SocketRole GetRole(ulong id)
/// <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="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>
/// <returns>
/// A task that represents the asynchronous creation operation. The task result contains the newly created
/// role.
/// </returns>
public Task<RestRole> 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);
Expand Down Expand Up @@ -2114,8 +2116,8 @@ IRole IGuild.GetRole(ulong id)
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);
/// <inheritdoc />
async Task<IRole> 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<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, icon, emoji).ConfigureAwait(false);

/// <inheritdoc />
async Task<IReadOnlyCollection<IGuildUser>> IGuild.GetUsersAsync(CacheMode mode, RequestOptions options)
Expand Down

0 comments on commit b45b774

Please sign in to comment.