示例#1
0
        public async Task GiveawayAsync(EventContext e)
        {
            DiscordEmoji emoji = new DiscordEmoji();

            emoji.Name = "🎁";

            e.Arguments.Take(out string giveawayText);

            while (!e.Arguments.Pack.Peek().StartsWith("-"))
            {
                giveawayText += " " + e.Arguments.Pack.Take();
            }

            var mml = new MMLParser(e.Arguments.Pack.TakeAll()).Parse();

            int      amount   = mml.Get("amount", 1);
            TimeSpan timeLeft = mml.Get("time", "1h").GetTimeFromString();

            if (amount > 10)
            {
                await e.ErrorEmbed("We can only allow up to 10 picks per giveaway")
                .ToEmbed().QueueToChannelAsync(e.Channel);

                return;
            }

            giveawayText = giveawayText + ((amount > 1) ? " x " + amount : "");

            List <IDiscordUser> winners = new List <IDiscordUser>();

            IDiscordMessage msg = await CreateGiveawayEmbed(e, giveawayText)
                                  .AddField("Time", timeLeft.ToTimeString(e.Locale), true)
                                  .AddField("React to participate", "good luck", true)
                                  .ToEmbed().SendToChannel(e.Channel);

            await msg.CreateReactionAsync(emoji);

            int updateTask = -1;

            int task = taskScheduler.AddTask(e.Author.Id, async(desc) =>
            {
                msg = await e.Channel.GetMessageAsync(msg.Id);

                if (msg != null)
                {
                    await msg.DeleteReactionAsync(emoji);

                    await Task.Delay(1000);

                    var reactions = await msg.GetReactionsAsync(emoji);

                    //do
                    //{
                    //	reactions.AddRange();
                    //	reactionsGained += 100;
                    //} while (reactions.Count == reactionsGained);

                    // Select random winners
                    for (int i = 0; i < amount; i++)
                    {
                        if (reactions.Count == 0)
                        {
                            break;
                        }

                        int index = MikiRandom.Next(reactions.Count);
                        winners.Add(reactions[index]);
                    }

                    if (updateTask != -1)
                    {
                        taskScheduler.CancelReminder(e.Author.Id, updateTask);
                    }

                    string winnerText = string.Join("\n", winners.Select(x => x.Username + "#" + x.Discriminator).ToArray());
                    if (string.IsNullOrEmpty(winnerText))
                    {
                        winnerText = "nobody!";
                    }

                    await msg.EditAsync(new EditMessageArgs
                    {
                        embed = CreateGiveawayEmbed(e, giveawayText)
                                .AddField("Winners", winnerText)
                                .ToEmbed()
                    });
                }
            }, "description var", timeLeft);
        }
示例#2
0
        public async Task GiveawayAsync(IContext e)
        {
            if (!e.HasFeatureEnabled(FeatureFlags.GiveawayUsingNewScheduler))
            {
                throw new CommandDisabledException();
            }

            DiscordEmoji.TryParse("🎁", out var emoji);

            var args = e.GetArgumentPack();

            var giveawayText = args.TakeRequired <string>();

            if (args.CanTake)
            {
                while (!args.Pack.Peek().HasValue)
                {
                    giveawayText += " " + args.Pack.Take();
                }
            }

            var mml = new MMLParser(e.GetArgumentPack().Pack.TakeAll()).Parse();

            int      amount   = mml.Get("amount", 1);
            TimeSpan timeLeft = mml.Get("time", "1h").GetTimeFromString();

            if (amount > 10)
            {
                await e.ErrorEmbed("We can only allow up to 10 picks per giveaway")
                .ToEmbed()
                .QueueAsync(e, e.GetChannel())
                .ConfigureAwait(false);

                return;
            }

            giveawayText += amount > 1 ? $" x {amount:N0}" : "";

            List <IDiscordUser> winners = new List <IDiscordUser>();

            IDiscordMessage msg = await CreateGiveawayEmbed(e, giveawayText)
                                  .AddField("Time", timeLeft.ToTimeString(e.GetLocale()), true)
                                  .AddField("React to participate", "good luck", true)
                                  .ToEmbed()
                                  .SendToChannelAsync(e.GetChannel())
                                  .ConfigureAwait(false);

            await msg.CreateReactionAsync(emoji)
            .ConfigureAwait(false);

            //taskScheduler.AddTask(e.GetAuthor().Id, async (desc) =>
            //{
            //    msg = await e.GetChannel()
            //        .GetMessageAsync(msg.Id)
            //        .ConfigureAwait(false);
            //    if(msg == null)
            //    {
            //        return;
            //    }

            //    await msg.DeleteReactionAsync(emoji)
            //        .ConfigureAwait(false);

            //    await Task.Delay(1000)
            //        .ConfigureAwait(false);

            //    var reactions = (await msg.GetReactionsAsync(emoji)
            //            .ConfigureAwait(false))
            //        .ToList();

            //    //do
            //    //{
            //    //	reactions.AddRange();
            //    //	reactionsGained += 100;
            //    //} while (reactions.Count == reactionsGained);

            //    // Select random winners
            //    for(int i = 0; i < amount; i++)
            //    {
            //        if(!reactions.Any())
            //        {
            //            break;
            //        }

            //        int index = MikiRandom.Next(reactions.Count());
            //        winners.Add(reactions.ElementAtOrDefault(index));
            //    }

            //    if(updateTask != -1)
            //    {
            //        taskScheduler.CancelReminder(e.GetAuthor().Id, updateTask);
            //    }

            //    string winnerText = string.Join(
            //        "\n", winners.Select(x => $"{x.Username}#{x.Discriminator}").ToArray());
            //    if(string.IsNullOrEmpty(winnerText))
            //    {
            //        winnerText = "nobody!";
            //    }

            //    await msg.EditAsync(new EditMessageArgs
            //    {
            //        Embed = CreateGiveawayEmbed(e, giveawayText)
            //            .AddField("Winners", winnerText)
            //            .ToEmbed()
            //    }).ConfigureAwait(false);
            //}, "description var", timeLeft);
        }