public async Task NewPoll(string time, [Remainder] string pollName) { // Discord user info var msg = Context.Message; var discordId = msg.Author.Username; //Set all the date time info var currentTime = DateTime.Now; var userTime = DateTime.Parse(time); var timeToWait = userTime.Subtract(currentTime); TimeSpan timeToGo = timeToWait; PollSchema pollInfo = PollController.AddNewPoll(discordId, pollName, userTime); // Handle the timer if its in the past if (timeToGo < TimeSpan.Zero) { await ReplyAsync("Time Passed Fam"); } //EVENT HANDLER FOR THE TIMER REACHING THE TIME this.timer = new System.Threading.Timer(x => { this.EndPollNote(pollInfo); }, null, timeToGo, Timeout.InfiniteTimeSpan); //Message to confirm the poll has been set up await ReplyAsync($"{discordId} has started a new poll. [id = {pollInfo.Id}] \n {pollName} \n You have until {userTime} to vote! \n Use 'votepoll [id] [yes/no]'"); }
public async void EndPollNote(PollSchema poll) { bool timerexists = PollController.FindPoll(poll.Question); if (timerexists == true) { await ReplyAsync($"id: {poll.Id} - '{poll.Question}' has finished \r Yes Votes: {poll.Yes} \r No Votes: {poll.No}"); PollController.stopPollRunning(poll.Question); this.timer.Dispose(); } else { this.timer.Dispose(); } }
/// <summary> /// Gets the results of the poll. (Method cant be called by user, its auto) /// </summary> /// <param name="question"></param> /// <returns>Object containing poll results</returns> public static PollSchema GetPollResults(string question) { PollSchema pollResults = new PollSchema(); using (var db = new LiteDatabase(Constants.pollPath)) { var polls = db.GetCollection <PollSchema>("poll"); var result = polls.FindOne(x => x.Question.Equals(question)); var poll = result; pollResults.Id = poll.Id; pollResults.Yes = poll.Yes; pollResults.No = poll.No; pollResults.Question = poll.Question; return(pollResults); } }
/// <summary> /// Starts a new poll, sets up db and passes all information in /// </summary> /// <param name="userName"></param> /// <param name="question"></param> /// <param name="time"></param> /// <returns>Object with user info</returns> public static PollSchema AddNewPoll(string userName, string question, DateTime time) { using (var db = new LiteDatabase(Constants.pollPath)) { var Polls = db.GetCollection <PollSchema>("poll"); List <string> l = new List <string>(); var newPoll = new PollSchema { Question = question, Time = time, Owner = userName, IsRunning = true, UsersVoted = l }; Polls.Insert(newPoll); PollSchema initialUserInfo = GetPollResults(question); return(initialUserInfo); } }