public static async Task SyncUsersWithZoneRole(SocketCommandContext context, int zoneID, global::Discord.IRole role = null) { var roleName = $"zone-{zoneID}"; await context.Guild.DownloadUsersAsync(); if (role == null) { role = context.Guild.Roles.Where(x => x.Name == roleName).FirstOrDefault(); } if (role == null) { return; } var zone = ZoneManager.Zones[zoneID]; var validRoleUsers = new HashSet <ulong>(); using (var dbConnection = new DatabaseConnection(DatabaseID.Players)) { foreach (var member in zone.Members) { var discordID = PlayerDataManager.FindLinkedCharacterDiscord(dbConnection.Database, member.CharacterID); if (discordID > 0) { var user = context.Guild.Users.Where(x => x.Id == discordID).FirstOrDefault(); if (user != null) { var userRole = user.Roles.Where(x => x.Name == roleName).FirstOrDefault(); if (userRole == null) { await user.AddRoleAsync(role); } validRoleUsers.Add(discordID); } } } } foreach (var user in context.Guild.Users) { if (!validRoleUsers.Contains(user.Id)) { var userRole = user.Roles.Where(x => x.Name == roleName).FirstOrDefault(); if (userRole != null) { await userRole.DeleteAsync(); } } } }
public async Task ChannelAsync(int zoneID, string command) { if (!ZoneManager.Zones.Zones.ContainsKey(zoneID)) { await Context.Channel.SendMessageAsync("Invalid zone id specified."); return; } if (await ZoneSupport.IsUserLeader(Context, zoneID, Context.User) == false) { await Context.Channel.SendMessageAsync("You are not a leader of this zone."); return; } var zone = ZoneManager.Zones[zoneID]; var channelName = $"{zoneID}-{zone.Name.Substring(0, System.Math.Min(zone.Name.Length, 17))}"; channelName = channelName.ToLower().Replace(' ', '-'); var roleName = $"zone-{zoneID}"; switch (command.ToLower()) { case "open": { var category = Context.Guild.CategoryChannels.Where(x => x.Name.ToLower() == "zones").FirstOrDefault(); if (category == null) { await Context.Channel.SendMessageAsync("Category not found."); return; } var channel = await Context.Guild.CreateTextChannelAsync(channelName); await channel.ModifyAsync(o => { o.CategoryId = category.Id; o.Topic = $"Discussion for Zone {zoneID} - {zone.Name}."; }); global::Discord.IRole role = Context.Guild.Roles.Where(x => x.Name == roleName).FirstOrDefault(); if (role == null) { role = await Context.Guild.CreateRoleAsync(roleName); } var botRole = Context.Guild.Roles.Where(x => x.Name == "Bot").FirstOrDefault(); if (botRole != null) { await channel.AddPermissionOverwriteAsync(botRole, new global::Discord.OverwritePermissions(readMessages: global::Discord.PermValue.Allow, sendMessages: global::Discord.PermValue.Allow)); } await channel.AddPermissionOverwriteAsync(Context.Guild.EveryoneRole, new global::Discord.OverwritePermissions(readMessages: global::Discord.PermValue.Deny, sendMessages: global::Discord.PermValue.Deny)); await channel.AddPermissionOverwriteAsync(role, new global::Discord.OverwritePermissions(readMessages: global::Discord.PermValue.Allow, sendMessages: global::Discord.PermValue.Allow)); await ZoneSupport.SyncUsersWithZoneRole(Context, zoneID, role); } break; case "close": { var channel = Context.Guild.Channels.Where(x => x.Name == channelName).FirstOrDefault(); if (channel != null) { await channel.DeleteAsync(); await Context.Channel.SendMessageAsync("Channel closed!"); } else { await Context.Channel.SendMessageAsync("Channel not found."); } var role = Context.Guild.Roles.Where(x => x.Name == roleName).FirstOrDefault(); if (role != null) { await role.DeleteAsync(); } } break; } }