diff --git a/ChaosBot/Database/QueryBuilder.cs b/ChaosBot/Database/QueryBuilder.cs index e970161..b023eed 100644 --- a/ChaosBot/Database/QueryBuilder.cs +++ b/ChaosBot/Database/QueryBuilder.cs @@ -125,11 +125,15 @@ namespace ChaosBot.Database public int Count() { QueryBuilderRaw query = new QueryBuilderRaw(); - long amount = (long)query + query .Select() .Append("COUNT(*) as count") - .FromTable(_table) - .Run().Rows[0]["count"]; + .FromTable(_table); + + if (_whereConditions.Count > 0) + query.Where(_whereConditions); + + long amount = (long)query.Run().Rows[0]["count"]; return Convert.ToInt32(amount); } @@ -139,21 +143,29 @@ namespace ChaosBot.Database public bool Exists() { QueryBuilderRaw query = new QueryBuilderRaw(); - return (bool)query + query .Select() .Append("COUNT(*)>0 as present") - .FromTable(_table) - .Run().Rows[0]["present"]; + .FromTable(_table); + + if (_whereConditions.Count > 0) + query.Where(_whereConditions); + + return (bool)query.Run().Rows[0]["present"]; } public bool DoesNotExist() { QueryBuilderRaw query = new QueryBuilderRaw(); - return (bool)query + query .Select() .Append("COUNT(*)=0 as notPresent") - .FromTable(_table) - .Run().Rows[0]["notPresent"]; + .FromTable(_table); + + if (_whereConditions.Count > 0) + query.Where(_whereConditions); + + return (bool)query.Run().Rows[0]["notPresent"]; } /* diff --git a/ChaosBot/Database/Repository/RaffleRepository.cs b/ChaosBot/Database/Repository/RaffleRepository.cs index 728f242..348b5e1 100644 --- a/ChaosBot/Database/Repository/RaffleRepository.cs +++ b/ChaosBot/Database/Repository/RaffleRepository.cs @@ -133,7 +133,7 @@ namespace ChaosBot.Database.Repository /// Delete a Raffle by id /// /// - public static void Delete(int id) + public static void Delete(long id) { Raffle.Query().Where("id", id).Delete(); } diff --git a/ChaosBot/Discord/Modules/RaffleSystem.cs b/ChaosBot/Discord/Modules/RaffleSystem.cs index 6158c29..5e01484 100644 --- a/ChaosBot/Discord/Modules/RaffleSystem.cs +++ b/ChaosBot/Discord/Modules/RaffleSystem.cs @@ -18,165 +18,105 @@ namespace ChaosBot.Discord.Modules [Command("raffle")] [Alias("raffle info")] - [RequireUserPermission(GuildPermission.ManageGuild)] - public async Task RaffleCommandInfo() { - try - { - var sb = new StringBuilder(); - var embed = new EmbedBuilder(); - string prefix = ConfigurationRepository.GetValue("Discord:Prefix"); - - embed.WithColor(new Color(255, 255,0)); - embed.Title = $"Raffle system"; - sb.AppendLine($"{Context.User.Mention} has requested raffle information."); - sb.AppendLine(); - sb.AppendLine($"Usage:"); - sb.AppendLine($"{prefix}raffle status"); - sb.AppendLine($"{prefix}raffle info"); - sb.AppendLine(); - sb.AppendLine("Moderation commands:"); - sb.AppendLine($"{prefix}raffle add "); - sb.AppendLine($"{prefix}raffle pick"); - sb.AppendLine($"{prefix}raffle clear"); - - /* - * Add the string to the Embed - */ - embed.Description = sb.ToString(); + public async Task RaffleInfo() + { + var eb = new EmbedBuilder(); + var sb = new StringBuilder(); + string prefix = ConfigurationRepository.GetValue("Discord.Prefix"); - /* - * Reply with the Embed created above - */ - await ReplyAsync(null, false, embed.Build()); - } - catch (Exception ex) - { - _logger.Error($"RaffleSystem.RaffleCommandInfo: Exception [{ex}] thrown, <[{ex.Message}]>."); - } + sb.AppendLine($"{Context.User.Mention} has requested raffle information."); + sb.AppendLine(); + sb.AppendLine("Usage:"); + sb.AppendLine($"{prefix}raffle status"); + sb.AppendLine($"{prefix}raffle info"); + sb.AppendLine(); + sb.AppendLine("Moderation commands:"); + sb.AppendLine($"{prefix}raffle add "); + sb.AppendLine($"{prefix}raffle pick"); + sb.AppendLine($"{prefix}raffle clear"); + + eb.Title = "Raffle System"; + eb.Description = sb.ToString(); + + await ReplyAsync(null, false, eb.Build()); + } + + [Command("raffle status")] + public async Task RaffleStatus() + { + long userAmount = RaffleRepository.Count(Convert.ToInt64(Context.Guild.Id), Convert.ToInt64(Context.User.Id)); + long totalAmount = RaffleRepository.Count(Convert.ToInt64(Context.Guild.Id)); + + var eb = new EmbedBuilder(); + var sb = new StringBuilder(); + string prefix = ConfigurationRepository.GetValue("Discord.Prefix"); + + sb.AppendLine($"{Context.User.Mention}, you have {userAmount} rafflepoints."); + sb.AppendLine($"There is a total of {totalAmount} rafflepoints."); + + eb.Title = "Raffle System"; + eb.Description = sb.ToString(); + + await ReplyAsync(null, false, eb.Build()); } [Command("raffle add")] - [RequireUserPermission(GuildPermission.ManageGuild)] - public async Task RaffleCommandAdd(string user, int amount = 1) + public async Task RaffleAdd(string userMention, int amount) { - if (ConfigurationRepository.GetValue($"Raffle:Max", Context.Guild.Id) >= amount) - await RaffleCommandHelper("add", user, amount); - else - { - await ReplyAsync( - $"You cannot give more then {ConfigurationRepository.GetValue($"Raffle:Max", Context.Guild.Id).ToString()} tickets at a time", false); - _logger.Warn($"{Context.User.Username} attempted to give {amount} tickets to {user}!"); - } + IUser user = await Context.Channel.GetUserAsync(Convert.ToUInt64(userMention.Substring(3, userMention.Length-4))); + + long userAmount = RaffleRepository.Count(Convert.ToInt64(user.Id), Convert.ToInt64(Context.Guild.Id)); + + List raffleList = new List(); + for (int i = 0; i < amount; i++) + raffleList.Add(new Raffle(Convert.ToInt64(user.Id), Convert.ToInt64(Context.Guild.Id))); + RaffleRepository.MassInsert(raffleList); + + var eb = new EmbedBuilder(); + var sb = new StringBuilder(); + + sb.AppendLine($"{Context.User.Mention} has added {amount} rafflepoints to {user.Mention}"); + sb.AppendLine($"{user.Mention} now has {userAmount + amount} rafflepoints!"); + + eb.Title = "Raffle System"; + eb.Description = sb.ToString(); + + await ReplyAsync(null, false, eb.Build()); } [Command("raffle pick")] - [RequireUserPermission(GuildPermission.ManageGuild)] - public async Task RaffleCommandPick() + public async Task RafflePick() { - await RaffleCommandHelper("pick"); + Raffle winningRaffle = RaffleRepository.PickRandom(Convert.ToInt64(Context.Guild.Id)); + if (winningRaffle.id == null) return; // Shouldn't be needed, but it suppresses the warning + RaffleRepository.Delete((long)winningRaffle.id); + + var eb = new EmbedBuilder(); + var sb = new StringBuilder(); + + sb.AppendLine($"<@{winningRaffle.userId}> has won the raffle!"); + + eb.Title = "Raffle System"; + eb.Description = sb.ToString(); + + await ReplyAsync(null, false, eb.Build()); } [Command("raffle clear")] - [RequireUserPermission(GuildPermission.ManageGuild)] - public async Task RaffleCommandClear() + public async Task RaffleClear() { - await RaffleCommandHelper("clear"); - } - - [Command("raffle status")] - public async Task RaffleCommandStatus() - { - await RaffleCommandHelper("status", $"<@!{Context.User.Id}>"); - } - - private async Task RaffleCommandHelper(string action, string user = null, int amount = 0) { - try - { - StringBuilder sb = new StringBuilder(); - var embed = new EmbedBuilder(); - string prefix = ConfigurationRepository.GetValue("Discord:Prefix"); - - embed.WithColor(new Color(255, 255,0)); - embed.Title = $"Raffle system"; - - switch (action) - { - case "add": - AddRaffle(sb, user, amount); - break; - case "pick": - PickRaffle(sb); - break; - case "clear": - ClearRaffle(sb); - break; - case "status": - StatusRaffle(sb, user); - break; - } - - /* - * Add the string to the Embed - */ - embed.Description = sb.ToString(); - - /* - * Reply with the Embed created above - */ - await ReplyAsync(null, false, embed.Build()); - } - catch (Exception ex) - { - _logger.Error($"RaffleSystem.RaffleCommandHelper: Exception [{ex}] thrown, <[{ex.Message}]>."); - } - } - - private void AddRaffle(StringBuilder sb, string user, int amount = 1) - { - ulong userId = Convert.ToUInt64(user.Substring(3, user.Length-4)); - - if (amount > 1) - { - List raffles = new List(); - - for (int i = 0; i < amount; i++) - raffles.Add(new Raffle(Convert.ToInt64(userId), Convert.ToInt64(Context.Guild.Id))); - - RaffleRepository.MassInsert(raffles); - } - else - { - RaffleRepository.Insert(new Raffle(Convert.ToInt64(userId), Convert.ToInt64(Context.Guild.Id))); - } - - sb.AppendLine($"{Context.User.Mention} has added {amount} rafflepoints to <@{userId}>."); - sb.AppendLine(); - sb.AppendLine($"<@{userId}> now has {RaffleRepository.Count(Convert.ToInt64(userId), Convert.ToInt64(Context.Guild.Id))} rafflepoints!"); - } - - private void PickRaffle(StringBuilder sb) - { - Raffle winner = RaffleRepository.PickRandom(Convert.ToInt64(Context.Guild.Id)); - if (winner.id != null) - RaffleRepository.Delete((int)winner.id); - sb.Append($"<@{winner.userId}> has won the raffle!"); - } - - private void ClearRaffle(StringBuilder sb, bool noOutput = false) - { - int removed = RaffleRepository.Count(Convert.ToInt64(Context.Guild.Id)); - + long totalAmount = RaffleRepository.Count(Convert.ToInt64(Context.Guild.Id)); RaffleRepository.ClearRaffle(Convert.ToInt64(Context.Guild.Id)); - - sb.AppendLine($"{Context.User.Mention} has cleared all {removed} rafflepoints"); - } - private void StatusRaffle(StringBuilder sb, string user) - { - ulong userId = Convert.ToUInt64(user.Substring(3, user.Length-4)); + var eb = new EmbedBuilder(); + var sb = new StringBuilder(); - sb.AppendLine($"<@{userId}>, you have {RaffleRepository.Count(Convert.ToInt64(userId), Convert.ToInt64(Context.Guild.Id))} rafflepoints."); - sb.AppendLine($"There is a total of {RaffleRepository.Count(Convert.ToInt64(Context.Guild.Id))} rafflepoints."); + sb.AppendLine($"{Context.User.Mention} has cleared all {totalAmount} rafflepoints."); + + eb.Title = "Raffle System"; + eb.Description = sb.ToString(); + + await ReplyAsync(null, false, eb.Build()); } } } \ No newline at end of file