private void SimulatePoll(int polls, int options) { for (int i = 1; i <= polls; i++) { Polls.Add(new Poll { Title = "Poll " + i, Date = DateTime.Now.Date.ToString().Substring(0, 9), Options = new ObservableCollection <Poll.Option>() }); } for (int i = 1; i <= Polls.Count; i++) { for (int j = 1; j <= options; j++) { Polls[i - 1].Options.Add(new Poll.Option() { Text = "Poll " + i + " Option " + j }); } } for (int i = 1; i <= polls; i++) { if (i > 3) { Polls[i - 1].isActive = true; } } }
/// <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(); }
/// <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(); }
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); }); }