Example #1
0
        public async Task <HotOpSession> UpdateHotOpSession(int hotOpId, HotOpSession session)
        {
            try
            {
                var content  = new StringContent(JsonConvert.SerializeObject(session), Encoding.UTF8, "application/json");
                var response = await _client.PutAsync($"hotops/{hotOpId}/sessions/{session.Id}", content);

                if (!response.IsSuccessStatusCode)
                {
                    throw new BrobotServiceException($"Failed to update hot op session {session.Id} for hot op {hotOpId} with a status code of {response.StatusCode}");
                }

                var sessionResponseString = await response.Content.ReadAsStringAsync();

                return(JsonConvert.DeserializeObject <HotOpSession>(sessionResponseString));
            }
            catch (BrobotServiceException)
            {
                throw;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failed to update hot op session");
                throw new BrobotServiceException("Failed to update hot op session", ex);
            }
        }
Example #2
0
        private async Task CheckHotOps(SocketUser user, SocketVoiceState oldVoiceState, SocketVoiceState newVoiceState)
        {
            try
            {
                if (user.IsBot)
                {
                    return;
                }

                var hotOps = await _brobotService.GetHotOps(true);

                foreach (var hotOp in hotOps)
                {
                    if (user.Id == hotOp.Owner.DiscordUserId)
                    {
                        if (oldVoiceState.VoiceChannel == null || (newVoiceState.VoiceChannel != null && oldVoiceState.VoiceChannel?.Id != newVoiceState.VoiceChannel?.Id))
                        {
                            foreach (var connectedUser in newVoiceState.VoiceChannel.Users.Where(u => u.VoiceChannel.Id == newVoiceState.VoiceChannel.Id))
                            {
                                if (connectedUser.Id == user.Id || connectedUser.IsBot)
                                {
                                    continue;
                                }

                                var hotOpSession = new HotOpSession
                                {
                                    HotOpId          = hotOp.Id,
                                    DiscordUserId    = connectedUser.Id,
                                    StartDateTimeUtc = DateTime.UtcNow,
                                    EndDateTimeUtc   = null,
                                    VoiceChannelId   = newVoiceState.VoiceChannel.Id
                                };

                                await _brobotService.CreateHotOpSession(hotOp.Id, hotOpSession);
                            }
                        }

                        if (newVoiceState.VoiceChannel == null || (oldVoiceState.VoiceChannel != null && oldVoiceState.VoiceChannel?.Id != newVoiceState.VoiceChannel?.Id))
                        {
                            foreach (var connectedUser in oldVoiceState.VoiceChannel.Users.Where(u => u.VoiceChannel.Id == oldVoiceState.VoiceChannel.Id))
                            {
                                if (connectedUser.IsBot)
                                {
                                    continue;
                                }

                                var hotOpSession = hotOp.Sessions.FirstOrDefault(hos => hos.DiscordUserId == connectedUser.Id &&
                                                                                 hos.VoiceChannelId == oldVoiceState.VoiceChannel.Id &&
                                                                                 hos.EndDateTimeUtc == null);
                                if (hotOpSession == null)
                                {
                                    _logger.LogWarning($"User {connectedUser.Id} does not have a current session for hot op {hotOp.Id}");
                                    continue;
                                }

                                hotOpSession.EndDateTimeUtc = DateTime.UtcNow;
                                await _brobotService.UpdateHotOpSession(hotOp.Id, hotOpSession);
                            }
                        }
                    }
                    else
                    {
                        if (oldVoiceState.VoiceChannel == null || (newVoiceState.VoiceChannel != null && oldVoiceState.VoiceChannel?.Id != newVoiceState.VoiceChannel?.Id))
                        {
                            if (!newVoiceState.VoiceChannel.Users.Any(u => u.Id == hotOp.Owner.DiscordUserId && u.VoiceChannel.Id == newVoiceState.VoiceChannel.Id))
                            {
                                continue;
                            }

                            var hotOpSession = new HotOpSession
                            {
                                HotOpId          = hotOp.Id,
                                StartDateTimeUtc = DateTime.UtcNow,
                                DiscordUserId    = user.Id,
                                VoiceChannelId   = newVoiceState.VoiceChannel.Id
                            };
                            await _brobotService.CreateHotOpSession(hotOp.Id, hotOpSession);
                        }

                        if (newVoiceState.VoiceChannel == null || (oldVoiceState.VoiceChannel != null && oldVoiceState.VoiceChannel?.Id != newVoiceState.VoiceChannel?.Id))
                        {
                            if (!oldVoiceState.VoiceChannel.Users.Any(u => u.Id == hotOp.Owner.DiscordUserId && u.VoiceChannel.Id == oldVoiceState.VoiceChannel.Id))
                            {
                                continue;
                            }

                            var hotOpSession = hotOp.Sessions.FirstOrDefault(hos => hos.DiscordUserId == user.Id &&
                                                                             hos.VoiceChannelId == oldVoiceState.VoiceChannel.Id &&
                                                                             hos.EndDateTimeUtc == null);
                            if (hotOpSession == null)
                            {
                                _logger.LogWarning($"User {user.Id} does not have a current session for hot op {hotOp.Id}");
                                continue;
                            }

                            hotOpSession.EndDateTimeUtc = DateTime.UtcNow;
                            await _brobotService.UpdateHotOpSession(hotOp.Id, hotOpSession);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Failed to log hot op information for user {user.Id}");
            }
        }