/// <summary>
 /// Discord Event Handler for when a <paramref name="channel"/> is created
 /// </summary>
 /// <param name="channel"></param>
 /// <returns></returns>
 public async Task ChannelCreated(SocketChannel channel)
 {
     if (!(channel is SocketGuildChannel))
     {
         return;
     }
     SocketGuildChannel socketGuildChannel = (SocketGuildChannel)channel;
     var context = new DiscordChannelUpdate {
         GuildId = socketGuildChannel.Guild.Id, ChannelId = socketGuildChannel.Id, ChannelName = socketGuildChannel.Name
     };
     await _bus.Publish(context);
 }
        /// <summary>
        /// Discord Event Handler for when a Channel is updated from <paramref
        /// name="beforeChannel"/> to <paramref name="afterChannel"/>
        /// </summary>
        /// <param name="beforeChannel"></param>
        /// <param name="afterChannel"></param>
        /// <returns></returns>
        public async Task ChannelUpdated(SocketChannel beforeChannel, SocketChannel afterChannel)
        {
            if (!(beforeChannel is SocketTextChannel) || !(afterChannel is SocketTextChannel))
            {
                return;
            }
            SocketTextChannel beforeGuildChannel = (SocketTextChannel)beforeChannel;
            SocketTextChannel afterGuildChannel  = (SocketTextChannel)afterChannel;

            if (beforeGuildChannel.Name == afterGuildChannel.Name)
            {
                return;
            }

            var context = new DiscordChannelUpdate {
                GuildId = afterGuildChannel.Guild.Id, ChannelId = afterGuildChannel.Id, ChannelName = afterGuildChannel.Name
            };
            await _bus.Publish(context);
        }
예제 #3
0
        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}");
            }
        }