Example #1
0
            /// <summary>
            /// Start the poll
            /// </summary>
            public void Start()
            {
                EssLang.Broadcast("POLL_STARTED", Name, Description);

                var thiz = this;

                lock (Polls) Polls.Add(Name, thiz);

                if (Duration > 0)
                {
                    Task.Create()
                    .Id("Poll Stop")
                    .Delay(TimeSpan.FromSeconds(Duration))
                    .Action(() =>
                    {
                        lock (Polls)
                        {
                            if (!Polls.ContainsKey(thiz.Name))
                            {
                                return;
                            }
                            thiz.Stop();
                        }
                    })
                    .Submit();
                }

                if (!UEssentials.Config.EnablePollRunningMessage)
                {
                    return;
                }

                var interval = UEssentials.Config.PollRunningMessageCooldown * 1000;

                Task.Create()
                .Id("Poll Running Message")
                .Interval(interval)
                .UseIntervalAsDelay()
                .Action(task =>
                {
                    lock (Polls)
                    {
                        if (!Polls.ContainsKey(thiz.Name))
                        {
                            task.Cancel();
                            return;
                        }

                        EssLang.Broadcast("POLL_RUNNING", thiz.Name, thiz.Description,
                                          thiz.YesVotes, thiz.NoVotes);
                    }
                })
                .Submit();
            }
Example #2
0
            /// <summary>
            /// Start the poll
            /// </summary>
            public void Start()
            {
                EssLang.POLL_STARTED.Broadcast(Name, Description);

                lock (Polls) Polls.Add(Name, this);

                var thiz = this;

                if (Duration > 0)
                {
                    Tasks.New(task =>
                    {
                        lock ( Polls )
                        {
                            if (!Polls.ContainsKey(thiz.Name))
                            {
                                return;
                            }

                            thiz.Stop();
                        }
                    }).Delay(Duration * 1000).Go();
                }

                if (!UEssentials.Config.EnablePollRunningMessage)
                {
                    return;
                }

                var interval = UEssentials.Config.PollRunningMessageCooldown * 1000;

                Tasks.New(task =>
                {
                    lock ( Polls )
                    {
                        if (!Polls.ContainsKey(thiz.Name))
                        {
                            task.Cancel();
                            return;
                        }

                        EssLang.POLL_RUNNING.Broadcast(
                            thiz.Name,
                            thiz.Description,
                            thiz.YesVotes,
                            thiz.NoVotes
                            );
                    }
                }).Interval(interval).Delay(interval).Go();
            }
Example #3
0
        public int CreatePollId(ulong UserId)
        {
            if (!Polls.ContainsKey(UserId))
            {
                return(1);
            }
            int[] ids = Polls[UserId].Select(t => t.PollId).ToArray();

            for (int id = 1; id < int.MaxValue; id++)
            {
                if (!ids.Contains(id))
                {
                    return(id);
                }
            }

            return(-1);
        }
Example #4
0
 public bool CancelPoll(ulong UserId, int PollId)
 {
     if (Polls.ContainsKey(UserId))
     {
         int Index = Polls[UserId].FindIndex(t => t.PollId == PollId);
         if (Index > -1)
         {
             Poll poll = Polls[UserId][Index];
             if (poll.Active)
             {
                 Polls[UserId][Index].Duration = TimeSpan.Zero;
                 Polls[UserId][Index].CTokenSource.Cancel();
                 return(true);
             }
         }
     }
     return(false);
 }
Example #5
0
        public async Task RegisterPollAsync(Poll Poll, bool SendEmbed = true)
        {
            if (SendEmbed)
            {
                SocketGuild  guild   = Global.Client.GetGuild(Poll.GuildId);
                ITextChannel ch      = guild.GetTextChannel(Poll.ChannelId);
                IUserMessage message = await ch.SendMessageAsync("", embed : Poll.CreateEmbed().Build());

                foreach (IEmote emote in Poll.ReactionEmotes.Select(t => new Emoji(t)))
                {
                    await message.AddReactionAsync(emote);
                }

                IUser usr = Global.Client.GetUser(Poll.PollCreator);
                if (usr == null)
                {
                    foreach (IGuild g in Global.Client.Guilds)
                    {
                        IUser u = g.GetUserAsync(Poll.PollCreator).Result;
                        if (u != null)
                        {
                            usr = u;
                            break;
                        }
                    }
                }

                IDMChannel channel = await usr.CreateDMChannelAsync();

                EmbedBuilder builder = new EmbedBuilder()
                {
                    Title       = "New poll registered!",
                    Description = $"Question: { Poll.PollText }\nID: { Poll.PollId }\nMade on guild: { guild.Name } in #{ ch.Name }",
                    Color       = Color.Green
                };
                await channel.SendMessageAsync("", embed : builder.Build());

                Poll.MessageId = message.Id;

                if (!Polls.ContainsKey(Poll.PollCreator))
                {
                    Polls.Add(Poll.PollCreator, new List <Poll>());
                }

                Polls[Poll.PollCreator].Add(Poll);
            }

            if (PollMessageIds.ContainsKey(Poll.MessageId))
            {
                PollMessageIds[Poll.MessageId].Add(Poll);
            }
            else
            {
                PollMessageIds.Add(Poll.MessageId, new List <PollIdentifier>()
                {
                    Poll
                });
            }

            Task pollTask = Task.Run(async() =>
            {
                TimeSpan ts = Poll.PollCreatedAt + Poll.Duration - TimeSpan.FromTicks(DateTime.Now.Ticks);
                if (ts > TimeSpan.Zero)
                {
                    try
                    {
                        await Task.Delay(ts, Poll.CToken);
                    }
                    catch (OperationCanceledException) { }
                }
                await UnregisterPollAsync(Poll);
            });
        }