/// <summary> /// Builds and adds role to server. IF the operation fails, an exception will be thrown /// with the error message. /// </summary> public Discord.Rest.RestRole Build() { bool hexColorUsed = false; bool positionedByIndex = false; if (altColor != null) { hexColorUsed = true; } if (pos != -1) { positionedByIndex = true; } else if (upperRole != "" || lowerRole != "") { this.pos = GetIndexForRelativePos(); positionedByIndex = true; } try { GeneratePermRawValue(); } catch (Exception e) { throw e; } GuildPermissions perms = new GuildPermissions(permVal); Discord.Rest.RestRole role = null; if (hexColorUsed) { try { Color localColor = new Color(altColor[0], altColor[1], altColor[2]); role = guild.CreateRoleAsync( name, perms, localColor, isHoisted ).Result; }catch (Exception e) { throw e; } } else { try { role = guild.CreateRoleAsync( name, perms, color, isHoisted ).Result; }catch (Exception e) { throw e; } } if (positionedByIndex) { //(roles count - pos) since this class rests on supposition //that top most role is 0, and not bottom as discord API does ReorderRoleProperties x = new ReorderRoleProperties(role.Id, guild.Roles.Count() - pos); IEnumerable <ReorderRoleProperties> roleNewPos = (IEnumerable <ReorderRoleProperties>) (new List <ReorderRoleProperties> { x }); guild.ReorderRolesAsync(roleNewPos); } return(role); }
public async Task ReorderRoles() { var allRoles = Util.LoadAllRolesFromServer().OrderBy(x => x.difficulty).Select(x => x._id).ToList(); var allGuildRoles = Context.Guild.Roles.OrderBy(y => allRoles.IndexOf(y.Name)).ToList(); //reorder leaderbot roles to be at the top for hierarchy purposes var leaderbotRoles = allGuildRoles.Where(x => x.Name.Contains("leaderbot")).Reverse().ToList(); allGuildRoles.RemoveAll(x => x.Name.Contains("leaderbot")); allGuildRoles.AddRange(leaderbotRoles); //sort the list based on the difficulty var sorting = allGuildRoles.Select((role, pos) => { var res = new ReorderRoleProperties(role.Id, pos); return(res); }); await Context.Guild.ReorderRolesAsync(sorting); await ReplyAsync($"Roles have been reordered"); }