Exemple #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);
        }