From 17a59ad5161ead0e3f26816d71a2ac11aee61483 Mon Sep 17 00:00:00 2001 From: Ben Yost Date: Fri, 5 Jun 2020 20:25:04 -0400 Subject: [PATCH 1/3] Adding ablity to delete user from points table --- .../Database/Repository/PointsRepository.cs | 6 ---- ChaosBot/Discord/Modules/PointsCommands.cs | 30 +++++++++++++++++-- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/ChaosBot/Database/Repository/PointsRepository.cs b/ChaosBot/Database/Repository/PointsRepository.cs index d6d063f..6d389c7 100644 --- a/ChaosBot/Database/Repository/PointsRepository.cs +++ b/ChaosBot/Database/Repository/PointsRepository.cs @@ -76,12 +76,6 @@ namespace ChaosBot.Database.Repository } - - - - - - public static int Add(string userId, int points, string guildId) { Dictionary selectfilterDict = new Dictionary(); diff --git a/ChaosBot/Discord/Modules/PointsCommands.cs b/ChaosBot/Discord/Modules/PointsCommands.cs index b86d70b..6fc839d 100644 --- a/ChaosBot/Discord/Modules/PointsCommands.cs +++ b/ChaosBot/Discord/Modules/PointsCommands.cs @@ -8,7 +8,8 @@ using Discord.Commands; using Microsoft.Extensions.Configuration; using NLog; using ChaosBot.Database.Repository; - +using ChaosBot.Database; +using System.Data; namespace ChaosBot.Discord.Modules { @@ -35,7 +36,8 @@ namespace ChaosBot.Discord.Modules sb.AppendLine(); sb.AppendLine("Moderation commands:"); sb.AppendLine($"{prefix}points add "); - sb.AppendLine($"{prefix}point remove"); + sb.AppendLine($"{prefix}point remove "); + sb.AppendLine($"{prefix}point delete "); /* * Add the string to the Embed @@ -89,5 +91,29 @@ namespace ChaosBot.Discord.Modules else await ReplyAsync($"{Context.User.Mention} has tried to remove {amount} points from <@{userId}> they only had {cur} points. None were taken...", false); } + + [Command("points delete")] + [RequireUserPermission(ChannelPermission.ManageMessages)] + public async Task DeletePoints(string userMention) + { + ulong userId = Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)); + Dictionary filterColumns = new Dictionary + { + { "userId", userId }, + { "guildId", Context.Guild.Id } + }; + Dictionary selectfilterDict = new Dictionary(); + selectfilterDict.Add("userId", userId); + selectfilterDict.Add("guildId", Context.Guild.Id); + DataTable dt = Controller.SelectQuery(Table, "COUNT(*)", selectfilterDict); + int matches = dt.Rows[0]["COUNT(*)"]; + if (matches > 0) + { + Controller.DeleteQuery("PointsTable", filterColumns); + await ReplyAsync($"{Context.User.Mention} has removed <@{userId}> from the database.", false); + } + else + await ReplyAsync($"{Context.User.Mention} has failed to remove <@{userId}> from the database, <@{userId}> does not exist. ", false); + } } } From 343e0e7626a12ff0944226b8436f7c35f8f0a55e Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 6 Jun 2020 02:41:36 +0200 Subject: [PATCH 2/3] Add Count for PointRepository --- ChaosBot/Database/Repository/PointsRepository.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ChaosBot/Database/Repository/PointsRepository.cs b/ChaosBot/Database/Repository/PointsRepository.cs index 6d389c7..d6145bf 100644 --- a/ChaosBot/Database/Repository/PointsRepository.cs +++ b/ChaosBot/Database/Repository/PointsRepository.cs @@ -76,6 +76,18 @@ namespace ChaosBot.Database.Repository } + public static int Count(string userId, string guildId) + { + Dictionary selectfilterDict = new Dictionary(); + selectfilterDict.Add("userId", userId); + selectfilterDict.Add("guildId", guildId); + + DataTable dt = Controller.SelectQuery(Table, "COUNT(*)", selectfilterDict); + + return Convert.ToInt32(dt.Rows[0]["COUNT(*)"]); + } + + public static int Add(string userId, int points, string guildId) { Dictionary selectfilterDict = new Dictionary(); From 9871d402de82faf51b439ec8e40ca237b6481a15 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Sat, 6 Jun 2020 02:42:14 +0200 Subject: [PATCH 3/3] Rework DeletePoints method to use Count Repository method --- ChaosBot/Discord/Modules/PointsCommands.cs | 24 +++++++++++++--------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/ChaosBot/Discord/Modules/PointsCommands.cs b/ChaosBot/Discord/Modules/PointsCommands.cs index 6fc839d..5a4bbdd 100644 --- a/ChaosBot/Discord/Modules/PointsCommands.cs +++ b/ChaosBot/Discord/Modules/PointsCommands.cs @@ -102,18 +102,22 @@ namespace ChaosBot.Discord.Modules { "userId", userId }, { "guildId", Context.Guild.Id } }; - Dictionary selectfilterDict = new Dictionary(); - selectfilterDict.Add("userId", userId); - selectfilterDict.Add("guildId", Context.Guild.Id); - DataTable dt = Controller.SelectQuery(Table, "COUNT(*)", selectfilterDict); - int matches = dt.Rows[0]["COUNT(*)"]; + + int matches = PointsRepository.Count(userId.ToString(), Context.Guild.Id.ToString()); if (matches > 0) - { - Controller.DeleteQuery("PointsTable", filterColumns); - await ReplyAsync($"{Context.User.Mention} has removed <@{userId}> from the database.", false); - } + { + Controller.DeleteQuery("PointsTable", filterColumns); + + string message = $"{Context.User.Mention} has removed <@{userId}> from the database."; + await ReplyAsync(message, false); + _logger.Info($"PointsCommands.DeletePoints: {message}"); + } else - await ReplyAsync($"{Context.User.Mention} has failed to remove <@{userId}> from the database, <@{userId}> does not exist. ", false); + { + string message = $"{Context.User.Mention} has failed to remove <@{userId}> from the database, <@{userId}> does not exist."; + await ReplyAsync(message, false); + _logger.Warn($"PointsCommands.DeletePoints: {message}"); + } } } }