private Task RoleCreated(SocketRole socketRole) { // Give the new role properties corresponding to its permission level. // This is done to avoid administrative roles being joinable directly after creation and to minimize the extra work needed by admins. bool joinable = false; RoleType roleType = RoleType.Other; if (Data.HasAdministrativePermission(socketRole)) { joinable = false; roleType = RoleType.Admin; } else if (socketRole.Permissions.Connect || socketRole.Permissions.SendMessages || socketRole.Permissions.SendTTSMessages || socketRole.Permissions.Speak || socketRole.Permissions.ViewChannel) { joinable = true; roleType = RoleType.Game; } // Create RoleContainer and add it to the file. RoleContainer roleContainer = new RoleContainer(socketRole.Name, joinable, roleType); Data.AddContainer(roleContainer, Data.FILE_PATH + Data.ROLE_FILE); return(Task.CompletedTask); }
public async Task ScanRolesAsync() { // Get all SocketRoles on the server and give them properties corresponding to their permission level. // This is done to avoid administrative roles being joinable directly after the scan and to minimize the extra work needed by admins. List <RoleContainer> roleContainers = new List <RoleContainer>(); List <SocketGuildChannel> channels = Context.Guild.Channels.ToList(); foreach (SocketRole sr in Context.Guild.Roles) { bool joinable = false; RoleType roleType = RoleType.Other; if (Data.HasAdministrativePermission(sr)) { roleType = RoleType.Admin; joinable = false; } else { // Get all Overwrites for the current SocketRole (sr) //Context.Guild.Channels.Where(c => c.GetPermissionOverwrite(sr).HasValue).ToList().ForEach(c => roleOverwrites.AddRange(c.PermissionOverwrites.Where(o => o.TargetType == PermissionTarget.Role && o.TargetId == sr.Id))); List <Overwrite> roleOverwrites = new List <Overwrite>(); foreach (SocketGuildChannel c in channels) { List <Overwrite> channelOverwrites = c.PermissionOverwrites.ToList(); foreach (Overwrite o in channelOverwrites) { if (o.TargetType == PermissionTarget.Role && o.TargetId == sr.Id) { roleOverwrites.Add(o); } } } // Go through Overwrites and check channel specific permissions bool channelAccess = false, channelModerator = false; foreach (Overwrite o in roleOverwrites) { if (o.Permissions.ViewChannel == PermValue.Allow) { channelAccess = true; } if (o.Permissions.ManageMessages == PermValue.Allow) { channelModerator = true; } } if (sr.Permissions.ToList().Count <= 0) { if (channelAccess) { if (sr.IsMentionable) { roleType = RoleType.Other; // no privs, outside of channel access, mentionable joinable = false; } else { if (channelModerator) { roleType = RoleType.Moderator; // no privs, outside of specific channels, not mentionable joinable = false; } else { roleType = RoleType.Game; // no privs, outside of channel access, not mentionable joinable = true; } } } else { roleType = RoleType.Color; // no privs, not mentionable joinable = true; } } /*if (sr.Permissions.Connect || * sr.Permissions.SendMessages || * sr.Permissions.SendTTSMessages || * sr.Permissions.Speak || * sr.Permissions.ViewChannel) * { * roleType = RoleType.Game; * joinable = true; * }*/ } roleContainers.Add(new RoleContainer(sr.Name, joinable, roleType)); } // Sort the roleContainers after roleType and save them to file. roleContainers.Sort((a, b) => a.roleType.CompareTo(b.roleType)); Data.SaveContainers(roleContainers, Data.FILE_PATH + Data.ROLE_FILE); // Return feedback message. IMessage m = await ReplyAsync(embed : new EmbedBuilder() .WithTitle("Scan Complete") .WithDescription($"All roles on `{Context.Guild.Name}` have now been scanned. To get a list of them, use the `Roles` command. You can also further edit these roles by using the `Edit`, `Add` and `Remove` commands.") .WithAutoDeletionFooter() .Build()); // Delete prompt and feedback messages. await Task.Delay(Data.MESSAGE_DELETE_DELAY * 1000); await Context.Message.DeleteAsync(); await m.DeleteAsync(); }