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);
        }
Exemple #2
0
        public async Task GiveawayAsync(EventContext e)
        {
            //Emoji emoji = new Emoji("🎁");

            var    arg          = e.Arguments.FirstOrDefault();
            string giveAwayText = arg?.Argument ?? "";

            arg = arg?.Next();

            while (!(arg?.Argument ?? "-").StartsWith("-"))
            {
                giveAwayText += " " + arg.Argument;
                arg           = arg?.Next();
            }

            var mml = new MMLParser(arg?.TakeUntilEnd()?.Argument ?? "").Parse();

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

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

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

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

            IDiscordMessage msg = await CreateGiveawayEmbed(e, giveAwayText)
                                  .AddField("Time", (DateTime.Now + timeLeft).ToShortTimeString(), true)
                                  .AddField("React to participate", "good luck", true)
                                  .ToEmbed().SendToChannel(e.Channel);

            //await (msg as IUserMessage).AddReactionAsync(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.RemoveReactionAsync(emoji, await e.Guild.GetCurrentUserAsync());

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

                    int reactionsGained = 0;

                    do
                    {
                        //reactions.AddRange(await (msg as  IUserMessage).GetReactionUsersAsync(emoji, 100, reactions.LastOrDefault()?.Id ?? null));
                        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 (isUnique)
                        {
                            reactions.RemoveAt(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);
        }