Exemple #1
0
        public async Task EvaluateAsync(CommandContext ctx,
                                        [RemainingText, Description("desc-code")] string code)
        {
            if (string.IsNullOrWhiteSpace(code))
            {
                throw new InvalidCommandUsageException(ctx, "cmd-err-cmd-add-cb");
            }

            DiscordMessage msg = await ctx.RespondWithLocalizedEmbedAsync(emb => {
                emb.WithLocalizedTitle("str-eval");
                emb.WithColor(this.ModuleColor);
            });

            Script <object>?snippet = CSharpCompilationService.Compile(code, out ImmutableArray <Diagnostic> diag, out Stopwatch compileTime);

            if (snippet is null)
            {
                await msg.DeleteAsync();

                throw new InvalidCommandUsageException(ctx, "cmd-err-cmd-add-cb");
            }

            var emb = new LocalizedEmbedBuilder(this.Localization, ctx.Guild?.Id);

            if (diag.Any(d => d.Severity == DiagnosticSeverity.Error))
            {
                emb.WithLocalizedTitle("str-eval-fail-compile");
                emb.WithLocalizedDescription("fmt-eval-fail-compile", compileTime.ElapsedMilliseconds, diag.Length);
                emb.WithColor(DiscordColor.Red);

                foreach (Diagnostic d in diag.Take(3))
                {
                    FileLinePositionSpan ls = d.Location.GetLineSpan();
                    emb.AddLocalizedTitleField("fmt-eval-err", Formatter.InlineCode(d.GetMessage()),
                                               titleArgs: new object[] { ls.StartLinePosition.Line, ls.StartLinePosition.Character }
                                               );
                }

                if (diag.Length > 3)
                {
                    emb.AddLocalizedField("str-eval-omit", "fmt-eval-omit", contentArgs: new object[] { diag.Length - 3 });
                }

                await UpdateOrRespondAsync();

                return;
            }

            Exception?           exc = null;
            ScriptState <object>?res = null;
            var runTime = Stopwatch.StartNew();

            try {
                res = await snippet.RunAsync(new EvaluationEnvironment(ctx));
            } catch (Exception e) {
                exc = e;
            }
            runTime.Stop();

            if (exc is { } || res is null)
Exemple #2
0
        public static DiscordEmbed ToDiscordEmbed(this Poll poll, LocalizationService lcs)
        {
            var emb = new LocalizedEmbedBuilder(lcs, poll.Channel.GuildId);

            emb.WithTitle(poll.Question);
            emb.WithLocalizedDescription("str-vote-text");
            emb.WithColor(DiscordColor.Orange);

            for (int i = 0; i < poll.Options.Count; i++)
            {
                if (!string.IsNullOrWhiteSpace(poll.Options[i]))
                {
                    emb.AddField($"{i + 1} : {poll.Options[i]}", $"{poll.Results.Count(kvp => kvp.Value == i)}");
                }
            }

            if (poll.EndTime is { })
Exemple #3
0
        public static DiscordEmbed ToEmbed(this ReactionsPoll poll, LocalizationService lcs)
        {
            var emb = new LocalizedEmbedBuilder(lcs, poll.Channel.GuildId);

            emb.WithTitle(poll.Question);
            emb.WithLocalizedDescription("str-vote-react");
            emb.WithColor(DiscordColor.Orange);

            for (int i = 0; i < poll.Options.Count; i++)
            {
                if (!string.IsNullOrWhiteSpace(poll.Options[i]))
                {
                    emb.AddField($"{i + 1}", poll.Options[i], inline: true);
                }
            }

            if (poll.EndTime is { })
Exemple #4
0
        public override async Task RunAsync(LocalizationService lcs)
        {
            if (_countries is null)
            {
                throw new InvalidOperationException("The quiz flag paths have not been loaded.");
            }

            var questions = new Queue <string>(_countries.Keys.Shuffle().Take(this.NumberOfQuestions));

            int timeouts = 0;

            for (int i = 0; i < this.NumberOfQuestions; i++)
            {
                string question = questions.Dequeue();

                var emb = new LocalizedEmbedBuilder(lcs, this.Channel.GuildId);
                emb.WithLocalizedDescription("fmt-game-quiz-q", i + 1);
                await this.Channel.SendFileAsync("flag.png", new FileStream(question, FileMode.Open), embed : emb.Build());

                bool timeout     = true;
                var  failed      = new ConcurrentHashSet <ulong>();
                var  answerRegex = new Regex($@"\b{_countries[question]}\b", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
                InteractivityResult <DiscordMessage> res = await this.Interactivity.WaitForMessageAsync(
                    xm => {
                    if (xm.ChannelId != this.Channel.Id || xm.Author.IsBot || failed.Contains(xm.Author.Id))
                    {
                        return(false);
                    }
                    timeout = false;
                    if (answerRegex.IsMatch(xm.Content))
                    {
                        return(true);
                    }
                    else
                    {
                        failed.Add(xm.Author.Id);
                    }
                    return(false);
                },
                    TimeSpan.FromSeconds(10)
                    );

                if (res.TimedOut)
                {
                    if (!failed.Any())
                    {
                        timeouts = timeout ? timeouts + 1 : 0;
                        if (timeouts == 3)
                        {
                            this.IsTimeoutReached = true;
                            return;
                        }
                    }
                    else
                    {
                        timeouts = 0;
                    }
                    await this.Channel.LocalizedEmbedAsync(lcs, Emojis.AlarmClock, DiscordColor.Teal, "fmt-game-quiz-timeout", _countries[question]);
                }
                else
                {
                    await this.Channel.LocalizedEmbedAsync(lcs, Emojis.CheckMarkSuccess, DiscordColor.Teal, "fmt-game-quiz-correct", res.Result.Author.Mention);

                    this.results.AddOrUpdate(res.Result.Author, u => 1, (u, v) => v + 1);
                }

                await Task.Delay(TimeSpan.FromSeconds(2));
            }
        }