Skip to content

Commit

Permalink
Added votes
Browse files Browse the repository at this point in the history
You can now do votes.
  • Loading branch information
Voltstro committed May 4, 2019
1 parent 83d99d2 commit 0b6e76f
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 19 deletions.
27 changes: 26 additions & 1 deletion Pootis-Bot/Core/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Discord.Commands;
using Discord.WebSocket;
using Pootis_Bot.Entities;
using Pootis_Bot.Services;

namespace Pootis_Bot.Core
{
Expand Down Expand Up @@ -114,7 +115,31 @@ private Task ReactionAdded(Cacheable<IUserMessage, ulong> cache, ISocketMessageC
}
}
else
LevelingSystem.UserSentMessage((SocketGuildUser)reaction.User, (SocketTextChannel)reaction.Channel, 5);
{
if(VoteGivewayService.isVoteRunning) // If there is a vote going on then check to make sure the reaction doesn't have anything to do with that.
{
foreach (var vote in VoteGivewayService.votes)
{
if(reaction.MessageId == vote.VoteMessageID)
{
if(reaction.Emote.Name == vote.YesEmoji)
{
vote.YesCount++;
}

else if(reaction.Emote.Name == vote.NoEmoji)
{
vote.NoCount++;
}
}
}
}
else
{
if (!((SocketGuildUser)reaction.User).IsBot)
LevelingSystem.UserSentMessage((SocketGuildUser)reaction.User, (SocketTextChannel)reaction.Channel, 5);
}
}

return Task.CompletedTask;
}
Expand Down
8 changes: 4 additions & 4 deletions Pootis-Bot/Modules/Misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Misc : ModuleBase<SocketCommandContext>
// Description - Misc commands
// Contributors - Creepysin,

VoteGivewayService voteGivewayService;
private readonly VoteGivewayService voteGivewayService;

public Misc()
{
Expand Down Expand Up @@ -77,11 +77,11 @@ public async Task Ping()
await Context.Channel.SendMessageAsync($"Pong! {Context.Client.Latency}ms");
}

[Command("vote")]
[Command("vote", RunMode = RunMode.Async)]
[Summary("Starts a vote")]
public async Task Vote()
public async Task Vote(string time, string title, string description, string yesEmoji, string noEmoji)
{
await voteGivewayService.StartVote(Context.Guild, Context.Channel, "", "", "", "", "");
await voteGivewayService.StartVote(Context.Guild, Context.Channel, Context.User, time, title, description, yesEmoji, noEmoji);
}

#region Functions
Expand Down
149 changes: 135 additions & 14 deletions Pootis-Bot/Services/VoteGivewayService.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,153 @@
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
using Pootis_Bot.Core;

namespace Pootis_Bot.Services
{
public class VoteGivewayService
{
enum TimeType { Hour, Days, Seconds }
private enum TimeType { Days, Hours, Secs}

public static List<Vote> votes = new List<Vote>();
public static bool isVoteRunning;

public async Task StartVote(SocketGuild guild, ISocketMessageChannel channel, string time, string title, string description, string yesEmoji, string noEmoji)
public async Task StartVote(SocketGuild guild, ISocketMessageChannel channel, SocketUser user, string time, string title, string description, string yesEmoji, string noEmoji)
{
TimeType timeType;

if (time.EndsWith("h") || time.EndsWith("hrs") || time.EndsWith("hours"))
timeType = TimeType.Hour;
else if (time.EndsWith("s") || time.EndsWith("sec") || time.EndsWith("secs") || time.EndsWith("seconds"))
timeType = TimeType.Seconds;
else if (time.EndsWith("d") || time.EndsWith("days"))
timeType = TimeType.Days;
else
string[] times = time.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
int totalTime = 0;

foreach(var _time in times)
{
await channel.SendMessageAsync("Invaild time format, the time must end with either `h` for hours, `s` for seconds and `d` for days!");
TimeType timeType;

string formated;
double currentTime = 0;

//Remove either h, hrs, hours, s, sec, secs, seconds, d, days depending on the time format
if (_time.EndsWith("h") || time.EndsWith("hrs") || time.EndsWith("hours"))
{
formated = _time.Replace("h", "").Replace("hrs", "").Replace("hours", "");
timeType = TimeType.Hours;
}
else if (_time.EndsWith("s") || time.EndsWith("sec") || time.EndsWith("secs") || time.EndsWith("seconds"))
{
formated = _time.Replace("s", "").Replace("sec", "").Replace("secs", "").Replace("seconds", "");
timeType = TimeType.Secs;
}
else if (_time.EndsWith("d") || time.EndsWith("days"))
{
formated = _time.Replace("d", "").Replace("days", "");
timeType = TimeType.Days;
}
else //It didn't include a support 'format' i geuss u would call it?
{
await channel.SendMessageAsync("Invaild time format, the time must end with either `h` for hours, `s` for seconds and `d` for days!");
return;
}

if (!int.TryParse(formated, out int temp)) //Convert the formated string to a int
{
await channel.SendMessageAsync("The time foramt is incorrect! Make sure it is something like this: `1h`.");
return;
}

//Convert the time depending on what TimeType it is.
if (timeType == TimeType.Days)
{
TimeSpan tp = TimeSpan.FromDays(temp);
currentTime = tp.TotalMilliseconds;
}
else if(timeType == TimeType.Hours)
{
TimeSpan tp = TimeSpan.FromHours(temp);
currentTime = tp.TotalMilliseconds;
}
else
{
TimeSpan tp = TimeSpan.FromSeconds(temp);
currentTime = tp.TotalMilliseconds;
}

totalTime += (int) currentTime;
}

//Setup emojis
if(!Global.ContainsUnicodeCharacter(yesEmoji) || !Global.ContainsUnicodeCharacter(noEmoji))
{
await channel.SendMessageAsync("Your emoji(s) are not unicode! Use https://unicode.org/emoji/charts/full-emoji-list.html to select an emoji.");
return;
}

Global.WriteMessage("The server {} has started vote that lasting {} seconds");
var yesEmote = new Emoji(yesEmoji);
var noEmote = new Emoji(noEmoji);

EmbedBuilder embed = new EmbedBuilder();
embed.WithTitle("**Vote**: " + title);
embed.WithDescription(description + $"\n\nReact with {yesEmoji} to say **YES** or {noEmoji} to say **NO**.");
embed.WithFooter($"Vote started by {user.Username} @ ", user.GetAvatarUrl());
embed.WithCurrentTimestamp();

var message = await channel.SendMessageAsync("", false, embed.Build());

// Add yes and no reactions
await message.AddReactionAsync(yesEmote);
await message.AddReactionAsync(noEmote);

isVoteRunning = true;
var vote = new Vote
{
VoteMessageID = message.Id,
YesEmoji = yesEmoji,
NoEmoji = noEmoji
};

votes.Add(vote);

Global.WriteMessage($"A vote has started on the guild {guild.Name}({guild.Id})", ConsoleColor.Green);

await Task.Delay(totalTime); // Wait for the vote to finish

EmbedBuilder finishedVote = new EmbedBuilder();
finishedVote.WithTitle("**Vote Results**: " + title);
finishedVote.WithDescription($"The vote has finished! Here are the results: \n**Yes**: {vote.YesCount}\n**No**: {vote.NoCount}");
finishedVote.WithFooter($"Vote was started by {user.Username} at ", user.GetAvatarUrl());
finishedVote.WithCurrentTimestamp();

await channel.SendMessageAsync("", false, finishedVote.Build());

votes.Remove(vote);

Global.WriteMessage($"The vote on {guild.Name}({guild.Id}) has finished.", ConsoleColor.Green);

//Check to make sure no other votes are running.
if (votes.Count == 0)
isVoteRunning = false;
}

public static Vote GetVote(ulong id)
{
var result = from a in votes
where a.VoteMessageID == id
select a;

var server = result.FirstOrDefault();
return server;
}

public class Vote
{
public ulong VoteMessageID { get; set; }

public string YesEmoji { get; set; }
public string NoEmoji { get; set; }

public int YesCount { get; set; }
public int NoCount { get; set; }
}

}
}

0 comments on commit 0b6e76f

Please sign in to comment.