/// <summary> /// Discord Event Handler for when a <paramref name="role"/> is deleted /// </summary> /// <param name="role"></param> /// <returns></returns> public async Task RoleDeleted(SocketRole role) { var context = new DiscordRoleDelete { GuildId = role.Guild.Id, RoleId = role.Id }; await _bus.Publish(context); }
public async Task Consume(ConsumeContext <IDiscordGuildAvailable> context) { try { var message = context.Message; var guild = _client.GetGuild(message.GuildId); if (guild == null) { return; } DiscordGuild discordGuild = new DiscordGuild() { DiscordId = message.GuildId, Name = message.GuildName, IconUrl = guild.IconUrl }; await _work.GuildRepository.AddOrUpdateAsync(discordGuild, (d => d.DiscordId == message.GuildId)); discordGuild = await _work.GuildRepository.SingleOrDefaultAsync(d => d.DiscordId == message.GuildId); #region Handle Channels var dbChannels = await _work.ChannelRepository.FindAsync(i => i.DiscordGuild == discordGuild); foreach (SocketGuildChannel channel in guild.TextChannels) { var existingChannels = dbChannels.ToList().Where(i => i.DiscordId == channel.Id && i.Name == channel.Name); if (existingChannels.Count() > 0) { continue; } DiscordChannelUpdate channelUpdateContext = new DiscordChannelUpdate { GuildId = guild.Id, ChannelId = channel.Id, ChannelName = channel.Name }; await _bus.Publish(channelUpdateContext); } List <ulong> channelIDs = guild.TextChannels.Select(i => i.Id).Distinct().ToList(); if (dbChannels.Count() > 0) { foreach (var channelId in dbChannels.Select(i => i.DiscordId).Distinct().Except(channelIDs)) { DiscordChannelDelete channelDeleteContext = new DiscordChannelDelete { GuildId = guild.Id, ChannelId = channelId }; await _bus.Publish(channelDeleteContext); } } #endregion Handle Channels #region Handle Roles var dbRoles = await _work.RoleRepository.FindAsync(i => i.DiscordGuild == discordGuild); foreach (SocketRole role in guild.Roles) { var existingRoles = dbRoles.ToList().Where(i => i.DiscordId == role.Id && i.Name == role.Name); if (existingRoles.Count() > 0) { continue; } DiscordRoleUpdate roleUpdateContext = new DiscordRoleUpdate { GuildId = guild.Id, RoleId = role.Id, RoleName = role.Name }; await _bus.Publish(roleUpdateContext); } List <ulong> roleIDs = guild.Roles.Select(i => i.Id).Distinct().ToList(); if (dbRoles.Count() > 0) { foreach (var roleId in dbRoles.Select(i => i.DiscordId).Distinct().Except(roleIDs)) { DiscordRoleDelete roleDeleteContext = new DiscordRoleDelete { GuildId = guild.Id, RoleId = roleId }; await _bus.Publish(roleDeleteContext); } } #endregion Handle Roles } catch (Exception e) { Serilog.Log.Error($"{e}"); } }
public async Task Consume(ConsumeContext <IDiscordGuildDelete> context) { var message = context.Message; var discordGuild = await _work.GuildRepository.SingleOrDefaultAsync(i => i.DiscordId == message.GuildId); if (discordGuild == null) { return; } var discordChannels = discordGuild.DiscordChannels; var discordRoles = discordGuild.DiscordRoles; var streamSubscriptions = discordGuild.StreamSubscriptions; foreach (var streamSubscription in streamSubscriptions) { try { await _work.SubscriptionRepository.RemoveAsync(streamSubscription.Id); } catch { Log.Error($"Unable to remove Stream Subscription {streamSubscription.User.SourceID} {streamSubscription.User.Username} in {discordGuild.DiscordId} {discordGuild.Name}"); continue; } } foreach (var discordChannel in discordChannels) { try { var channelContext = new DiscordChannelDelete { GuildId = discordGuild.DiscordId, ChannelId = discordChannel.DiscordId }; await _bus.Publish(channelContext); } catch { Log.Error($"Unable to remove Channel {discordChannel.DiscordId} {discordChannel.Name}"); continue; } } foreach (var discordRole in discordRoles) { try { var roleContext = new DiscordRoleDelete { GuildId = discordGuild.DiscordId, RoleId = discordRole.DiscordId }; await _bus.Publish(roleContext); } catch { Log.Error($"Unable to remove Role {discordRole.DiscordId} {discordRole.Name}"); continue; } } try { await _work.GuildRepository.RemoveAsync(discordGuild.Id); } catch { Log.Error($"Unable to remove Guild {discordGuild.DiscordId} {discordGuild.Name}"); } }