public async Task SetCall([Remainder] string args = null)
        {
            StringBuilder       sb       = new StringBuilder();
            var                 embed    = new EmbedBuilder();
            CallSignAssociation callInfo = null;


            callInfo = _db.CallSignAssociation.Where(d => d.DiscordUserId == (long)Context.User.Id).FirstOrDefault();


            if (callInfo != null)
            {
                var record = _db.CallSignAssociation.Where(r => r.DiscordUserId == (long)Context.User.Id).FirstOrDefault();
                record.CallSign = args.ToUpper();
                await _db.SaveChangesAsync();
            }
            else
            {
                _db.CallSignAssociation.Add(new CallSignAssociation
                {
                    DiscordUserId   = (long)Context.User.Id,
                    DiscordUserName = Context.User.Username,
                    CallSign        = args.ToUpper()
                });
                await _db.SaveChangesAsync();
            }

            sb.AppendLine($"{Context.User.Mention} your call sign is now set to:");
            sb.AppendLine($"{args.ToUpper()}");
            embed.Title       = $"Call sign information for {Context.User.Username}!";
            embed.Description = sb.ToString();
            await ReplyAsync(null, false, embed.Build());
        }
Beispiel #2
0
        public async Task ClearAfterTaken()
        {
            var sb = new StringBuilder();

            var discordSettings = await _db.QuizSettings.Where(s => s.DiscordGuildId == Context.Guild.Id).FirstOrDefaultAsync();

            if (discordSettings != null && discordSettings.ExtraChannelId != null || discordSettings.GeneralChannelId != null || discordSettings.TechChannelId != null)
            {
                if (discordSettings.ClearAfterTaken == true)
                {
                    discordSettings.ClearAfterTaken = false;
                }
                else
                {
                    discordSettings.ClearAfterTaken = true;
                }
                sb.AppendLine($"Test channel contents will be cleared upon test completion!");
            }
            else
            {
                sb.AppendLine("Please set a channel to a specific test before using this command!");
            }
            await _db.SaveChangesAsync();

            await ReplyAsync(sb.ToString());
        }
Beispiel #3
0
        private async Task IncorrectAnswer(SocketMessage msg, char?answerChar)
        {
            _db.UserAnswer.Add(new UserAnswer
            {
                UserId     = (long)msg.Author.Id,
                UserName   = msg.Author.Username,
                Question   = CurrentQuestion,
                AnswerText = answerChar.ToString(),
                Quiz       = Quiz,
                IsAnswer   = false
            });
            await _db.SaveChangesAsync();

            if (_isDmTest)
            {
                _tokenSource.Cancel();
            }
        }
Beispiel #4
0
        public async Task GetKey()
        {
            Cred   creds  = null;
            string result = string.Empty;

            try
            {
                creds            = _db.Cred.FirstOrDefault();
                _sessionCheckUrl = $"{_baseUrl}/?s={_apiKey};callsign=kf7ign";

                using (var client = new HttpClient())
                {
                    var fullUrl  = $"{_baseUrl}/?username={creds.User};password={creds.Pass};agent=q5.0";
                    var response = await client.GetAsync(fullUrl);

                    result = await response.Content.ReadAsStringAsync();
                }
                _logger.LogInformation($"{result}");
            }
            catch
            {
                _logger.LogError("Error updating QRZ Api key!");
            }
            finally
            {
                creds = null;
            }
            var qrzApi = ConvertResultToXml(result);

            if (!string.IsNullOrEmpty(qrzApi.Session.Key))
            {
                _qrzApiData.ApiKey = qrzApi.Session.Key;
                await _db.SaveChangesAsync();

                _apiKey = _qrzApiData.ApiKey;
                _logger.LogInformation("Updated QRZ Api Key!");
            }
            else
            {
                if (!string.IsNullOrEmpty(qrzApi.Session.Error))
                {
                    _logger.LogError($"{qrzApi.Session.Error}");
                }
            }
        }
Beispiel #5
0
        private async Task QuizCleanup()
        {
            var quizzes = await _db.Quiz.ToListAsync();

            foreach (var quiz in quizzes.Where(q => q.IsActive))
            {
                quiz.IsActive = false;
            }
            foreach (var quiz in quizzes)
            {
                var userAnswers = await _db.UserAnswer.Where(u => u.Quiz.QuizId == quiz.QuizId).ToListAsync();

                if (userAnswers != null)
                {
                    foreach (var answer in userAnswers)
                    {
                        _db.Remove(answer);
                    }
                }
            }
            await _db.SaveChangesAsync();
        }
Beispiel #6
0
        public async Task ChangePrefix(char prefix)
        {
            var currentPrefix = _db.PrefixList.Where(p => p.ServerId == (long)Context.Guild.Id).FirstOrDefault();

            if (currentPrefix != null)
            {
                currentPrefix.Prefix  = prefix;
                currentPrefix.SetById = (long)Context.User.Id;
            }
            else
            {
                _db.PrefixList.Add(new PrefixList
                {
                    ServerId   = (long)Context.Guild.Id,
                    ServerName = Context.Guild.Name,
                    Prefix     = prefix,
                    SetById    = (long)Context.User.Id
                });
            }
            await _db.SaveChangesAsync();

            await ReplyAsync($"Prefix for [**{Context.Guild.Name}**] changed to [**{prefix}**]");
        }
        public async Task ImportQuestions([Remainder] string args = null)
        {
            if (args == null)
            {
                await ReplyAsync("Please specify tech, general, or extra!");

                return;
            }

            var testName = string.Empty;
            var testDesc = string.Empty;

            switch (args.ToLower())
            {
            case "tech":
            {
                testName = "tech";
                testDesc = "U.S. Ham Radio test for the technician class license.";
                break;
            }

            case "general":
            {
                testName = "general";
                testDesc = "U.S. Ham Radio test for the general class license.";
                break;
            }

            case "extra":
            {
                testName = "extra";
                testDesc = "U.S. Ham Radio test for the extra class license.";
                break;
            }

            default:
            {
                await ReplyAsync("Please specify tech, general, or extra!");

                return;
            }
            }
            var curFile  = Directory.GetFiles($"import/{testName}").Where(f => f.Contains(testName) && f.Contains(".json")).FirstOrDefault();
            var fileName = Path.GetFileNameWithoutExtension(curFile);

            DateTime startDate = DateTime.Parse(fileName.Split('_')[1]);
            DateTime endDate   = DateTime.Parse(fileName.Split('_')[2]);

            var test = _db.HamTest.Where(t => t.TestName == testName).FirstOrDefault();

            if (test == null)
            {
                await _db.AddAsync(
                    new HamTest {
                    TestName        = testName,
                    TestDescription = testDesc,
                    FromDate        = startDate,
                    ToDate          = endDate
                });

                await _db.SaveChangesAsync();
            }
            else
            {
                //clear old test items
                var figures = _db.Figure.Where(f => f.Test.TestName == testName).ToList();
                if (figures != null)
                {
                    foreach (var figure in figures)
                    {
                        _db.Remove(figure);
                    }
                    await _db.SaveChangesAsync();
                }
                var answers = _db.Answer.Where(a => a.Question.Test.TestName == testName).ToList();
                if (answers != null)
                {
                    foreach (var answer in answers)
                    {
                        _db.Remove(answer);
                    }
                    await _db.SaveChangesAsync();
                }
                var questions = _db.Questions.Where(q => q.Test.TestName == testName).ToList();
                if (questions != null)
                {
                    foreach (var questionItem in questions)
                    {
                        _db.Remove(questionItem);
                    }
                    await _db.SaveChangesAsync();
                }
            }

            test          = _db.HamTest.Where(t => t.TestName == testName).FirstOrDefault();
            test.FromDate = startDate;
            test.ToDate   = endDate;
            await _db.SaveChangesAsync();

            //get questions converted from json to C# objects
            var question = QuestionIngest.FromJson(File.ReadAllText(curFile));

            //loop through and add to the database
            foreach (var item in question)
            {
                var questionText = item.Question;
                var answerchar   = Char.Parse(item.AnswerKey.ToString());
                var questionId   = item.QuestionId;
                var fccPart      = item.FccPart;
                var subDesc      = item.SubelementDesc;
                var subName      = item.SubelementName;

                if (string.IsNullOrEmpty(fccPart))
                {
                    await _db.AddAsync(
                        new Questions {
                        QuestionText    = questionText,
                        QuestionSection = questionId,
                        SubelementDesc  = subDesc,
                        FigureName      = item.Figure,
                        SubelementName  = subName.ToString(),
                        Test            = test
                    });
                }
                else
                {
                    await _db.AddAsync(
                        new Questions {
                        QuestionText    = questionText,
                        QuestionSection = questionId,
                        FccPart         = fccPart,
                        SubelementDesc  = subDesc,
                        FigureName      = item.Figure,
                        SubelementName  = subName.ToString(),
                        Test            = test
                    });
                }

                //save question to db
                await _db.SaveChangesAsync();

                //get the current question we just added, so we can associate the answer
                var curQuestion = _db.Questions.Where(q => q.QuestionSection == item.QuestionId).FirstOrDefault();

                //iterate through all the answers and add them
                foreach (var answer in item.PossibleAnswer)
                {
                    bool isAnswer      = false;
                    var  posAnswerText = answer.Substring(3);
                    var  posAnswerChar = answer.Substring(0, 1);
                    if (answerchar == Char.Parse(posAnswerChar))
                    {
                        isAnswer = true;
                    }
                    await _db.AddAsync(
                        new Answer {
                        Question   = curQuestion,
                        AnswerText = posAnswerText,
                        IsAnswer   = isAnswer
                    });
                }
                await _db.SaveChangesAsync();
            }

            var files = Directory.EnumerateFiles($"import/{testName}", $"{testName}_*.png");

            if (test != null)
            {
                foreach (var file in files)
                {
                    string curFigure = file;
                    if (File.Exists(curFigure))
                    {
                        var contents = File.ReadAllBytes(curFigure);

                        await _db.AddAsync(
                            new Figure {
                            Test        = test,
                            FigureName  = file.Split('_')[1].Replace(".png", "").Trim(),
                            FigureImage = contents
                        });

                        await _db.SaveChangesAsync();
                    }
                }
            }
            await ReplyAsync($"Imported {testName} into the database!");
        }