Ejemplo n.º 1
0
        public async Task PostAsync()
        {
            if (_gameState != GameState.Running)
            {
                return;
            }
            _postImage = true;
            int counter = 0;

            while (true)
            {
                if (_gameState != GameState.Running) // Can happen if the game is canceled
                {
                    return;
                }
                string finding = ""; // For error handling
                try
                {
                    PostType type = GetPostType();
                    if (type == PostType.Text)
                    {
                        foreach (string s in await GetPostAsync())
                        {
                            if (s == null)
                            {
                                continue;
                            }
                            finding = s;
                            await PostText(s);
                        }
                    }
                    else if (type == PostType.Url)
                    {
                        foreach (string s in await GetPostAsync())
                        {
                            if (s == null)
                            {
                                continue;
                            }
                            finding = s;
                            var answer = await _http.SendAsync(new HttpRequestMessage(HttpMethod.Head, s));

                            if (!answer.IsSuccessStatusCode)
                            {
                                throw new HttpRequestException("Invalid code " + answer.StatusCode);
                            }
                            await PostFromUrl(s);
                        }
                    }
                    else if (type == PostType.LocalPath)
                    {
                        foreach (string s in await GetPostAsync())
                        {
                            if (s == null)
                            {
                                continue;
                            }
                            finding = s;
                            await PostFromLocalPath(s);

                            File.Delete(s);
                        }
                    }
                    else
                    {
                        if (_audioFilePath != null)
                        {
                            File.Delete(_audioFilePath);
                        }
                        string url = (await GetPostAsync()).First();
                        _audioFilePath = "Saves/" + _guild.Id + DateTime.Now.ToString("HHmmssff") + ".mp3";
                        File.WriteAllBytes(_audioFilePath, await _http.GetByteArrayAsync(url));
                        _audioStream = _voiceSession.CreatePCMStream(AudioApplication.Voice);
                        await PostText("Audio started, use the 'replay' command to play it again.");
                        await PlayAudioFile();
                    }
                    _startTime = DateTime.Now;
                    _isFound   = false;
                    break;
                }
                catch (LooseException le)
                {
                    _postImage = false;
                    await LooseAsync(le.Message);

                    break;
                }
                catch (Exception e)
                {
                    counter++;
                    string msg = "Error posting for game " + _gameName + ": " + finding;
                    if (counter == 3)
                    {
                        await _chan.SendMessageAsync("", false, new EmbedBuilder()
                        {
                            Color       = Color.Red,
                            Title       = e.GetType().ToString(),
                            Description = Sentences.ExceptionGameStop(_guild),
                            Footer      = new EmbedFooterBuilder()
                            {
                                Text = e.Message
                            }
                        }.Build());

                        await Program.p.LogError(new LogMessage(LogSeverity.Error, e.Source, e.Message, new GameException(msg, e)));

                        _postImage = false;
                        await LooseAsync(null);

                        break;
                    }
                    else
                    {
                        await _chan.SendMessageAsync("", false, new EmbedBuilder()
                        {
                            Color       = Color.Orange,
                            Title       = e.GetType().ToString(),
                            Description = Sentences.ExceptionGame(_guild, e.Message),
                            Footer      = new EmbedFooterBuilder()
                            {
                                Text = e.Message
                            }
                        }.Build());

                        await Program.p.LogError(new LogMessage(LogSeverity.Error, e.Source, e.Message, new GameException(msg, e)));
                    }
                }
            }
            string help = Help();

            if (help != null)
            {
                await PostText(help);
            }
            _postImage = false;
        }
Ejemplo n.º 2
0
        public async Task PostAsync()
        {
            if (_gameState != GameState.Running)
            {
                return;
            }
            _postImage = true;
            int counter = 0;

            while (true)
            {
                if (_gameState != GameState.Running) // Can happen if the game is canceled
                {
                    return;
                }
                string finding = ""; // For error handling
                try
                {
                    PostType type = GetPostType();
                    if (type == PostType.Text)
                    {
                        foreach (string s in await GetPostAsync())
                        {
                            if (s == null)
                            {
                                continue;
                            }
                            finding = s;
                            await PostText(s);
                        }
                    }
                    else if (type == PostType.Url)
                    {
                        foreach (string s in await GetPostAsync())
                        {
                            if (s == null)
                            {
                                continue;
                            }
                            finding = s;
                            using (HttpClient hc = new HttpClient())
                            {
                                var answer = await hc.SendAsync(new HttpRequestMessage(HttpMethod.Head, s));

                                if (!answer.IsSuccessStatusCode)
                                {
                                    throw new HttpRequestException("Invalid code " + answer.StatusCode);
                                }
                            }
                            await PostFromUrl(s);
                        }
                    }
                    else
                    {
                        foreach (string s in await GetPostAsync())
                        {
                            if (s == null)
                            {
                                continue;
                            }
                            finding = s;
                            await PostFromLocalPath(s);

                            File.Delete(s);
                        }
                    }
                    _startTime = DateTime.Now;
                    _isFound   = false;
                    break;
                }
                catch (LooseException le)
                {
                    _postImage = false;
                    await LooseAsync(le.Message);

                    break;
                }
                catch (Exception e)
                {
                    counter++;
                    string msg = "Error posting " + finding;
                    if (counter == 3)
                    {
                        await _chan.SendMessageAsync("", false, new EmbedBuilder()
                        {
                            Color       = Color.Red,
                            Title       = e.GetType().ToString(),
                            Description = Sentences.ExceptionGameStop(_chan.GuildId),
                            Footer      = new EmbedFooterBuilder()
                            {
                                Text = e.Message
                            }
                        }.Build());

                        await Program.p.LogError(new LogMessage(LogSeverity.Error, e.Source, e.Message, new GameException(msg, e)));

                        _postImage = false;
                        await LooseAsync(null);

                        break;
                    }
                    else
                    {
                        await _chan.SendMessageAsync("", false, new EmbedBuilder()
                        {
                            Color       = Color.Orange,
                            Title       = e.GetType().ToString(),
                            Description = Sentences.ExceptionGame(_chan.GuildId, e.Message),
                            Footer      = new EmbedFooterBuilder()
                            {
                                Text = e.Message
                            }
                        }.Build());

                        await Program.p.LogError(new LogMessage(LogSeverity.Error, e.Source, e.Message, new GameException(msg, e)));
                    }
                }
            }
            string help = Help();

            if (help != null)
            {
                await PostText(help);
            }
            _postImage = false;
        }