From f4472e044d677d2915eefc2d0bbb9a7bd2ad0b33 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Fri, 5 Jun 2020 03:09:04 +0200 Subject: [PATCH 1/7] Add UpdateQuery to DB controller --- ChaosBot/Database/Controller.cs | 50 +++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/ChaosBot/Database/Controller.cs b/ChaosBot/Database/Controller.cs index 103672a..e35202b 100644 --- a/ChaosBot/Database/Controller.cs +++ b/ChaosBot/Database/Controller.cs @@ -102,6 +102,56 @@ namespace ChaosBot.Database _logger.Fatal($"Controllers.DBWork.RawQuery: Exception [{ex}] thrown, <[{ex.Message}]>."); } } + + public static void UpdateQuery(string table, Dictionary values, Dictionary parameters) + { + try + { + using (_conn) + { + _conn.Open(); + + SqliteCommand cmd = _conn.CreateCommand(); + StringBuilder commandText = new StringBuilder(); + commandText.Append("UPDATE OR FAIL "); + commandText.Append(table); + commandText.Append(" SET "); + + List updateList = new List(); + foreach (string key in values.Keys) + { + updateList.Add($"{key}=@val_{key} "); + cmd.Parameters.AddWithValue($@"val_{key}", values.GetValueOrDefault(key)); + } + + commandText.Append(string.Join(", ", updateList)); + + List filterList = new List(); + foreach (string key in parameters.Keys) + { + filterList.Add($"{key}=@fil_{key} "); + cmd.Parameters.AddWithValue($"@fil_{key}", parameters.GetValueOrDefault(key)); + } + + if (filterList.Count > 0) + { + commandText.Append("WHERE "); + commandText.Append(string.Join(", ", filterList)); + } + + cmd.CommandText = commandText.ToString(); + + cmd.Prepare(); + cmd.ExecuteNonQuery(); + + _conn.Close(); + } + } + catch (Exception ex) + { + _logger.Fatal($"Controllers.DBWork.RawQuery: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } public static int TransactionQuery(List cmds) { From 11ab5186b850622c9760e11e68246639e145d7d9 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Fri, 5 Jun 2020 03:26:54 +0200 Subject: [PATCH 2/7] General cleanup of imports, indenting and logging source --- ChaosBot/Attribute/AssemblyController.cs | 2 -- ChaosBot/Attribute/DBEntity.cs | 1 - ChaosBot/Database/Controller.cs | 11 +++++------ ChaosBot/Discord/Modules/AdminCommands.cs | 16 +++++++--------- ChaosBot/Discord/Modules/DiceCommands.cs | 2 +- ChaosBot/Discord/Modules/RaffleSystem.cs | 5 ++--- ChaosBot/Program.cs | 1 - 7 files changed, 15 insertions(+), 23 deletions(-) diff --git a/ChaosBot/Attribute/AssemblyController.cs b/ChaosBot/Attribute/AssemblyController.cs index 89bdefa..8d02727 100644 --- a/ChaosBot/Attribute/AssemblyController.cs +++ b/ChaosBot/Attribute/AssemblyController.cs @@ -3,9 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; -using Antlr4.Runtime; using ChaosBot.Database; -using Microsoft.Data.Sqlite; using NLog; namespace ChaosBot.Attribute diff --git a/ChaosBot/Attribute/DBEntity.cs b/ChaosBot/Attribute/DBEntity.cs index 6ee2b60..da812b2 100644 --- a/ChaosBot/Attribute/DBEntity.cs +++ b/ChaosBot/Attribute/DBEntity.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using Discord; using Microsoft.Data.Sqlite; namespace ChaosBot.Attribute diff --git a/ChaosBot/Database/Controller.cs b/ChaosBot/Database/Controller.cs index e35202b..f506cf4 100644 --- a/ChaosBot/Database/Controller.cs +++ b/ChaosBot/Database/Controller.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Data; -using System.Linq; using System.Text; using Microsoft.Data.Sqlite; using Microsoft.Extensions.Configuration; @@ -48,7 +47,7 @@ namespace ChaosBot.Database throw; } - _logger.Fatal($"Controllers.DBWork.RawQuery: Exception [{ex}] thrown, <[{ex.Message}]>."); + _logger.Fatal($"Database.Controller.RawQuery: Exception [{ex}] thrown, <[{ex.Message}]>."); } return dt; @@ -99,7 +98,7 @@ namespace ChaosBot.Database } catch (Exception ex) { - _logger.Fatal($"Controllers.DBWork.RawQuery: Exception [{ex}] thrown, <[{ex.Message}]>."); + _logger.Fatal($"Database.Controller.InsertQuery: Exception [{ex}] thrown, <[{ex.Message}]>."); } } @@ -149,7 +148,7 @@ namespace ChaosBot.Database } catch (Exception ex) { - _logger.Fatal($"Controllers.DBWork.RawQuery: Exception [{ex}] thrown, <[{ex.Message}]>."); + _logger.Fatal($"Database.Controller.UpdateQuery: Exception [{ex}] thrown, <[{ex.Message}]>."); } } @@ -253,7 +252,7 @@ namespace ChaosBot.Database } catch (Exception ex) { - _logger.Fatal($"Controllers.DBWork.RawQuery: Exception [{ex}] thrown, <[{ex.Message}]>."); + _logger.Fatal($"Database.Controller.SelectQuery: Exception [{ex}] thrown, <[{ex.Message}]>."); } return dt; @@ -305,7 +304,7 @@ namespace ChaosBot.Database } catch (Exception ex) { - _logger.Fatal($"Controllers.DBWork.RawQuery: Exception [{ex}] thrown, <[{ex.Message}]>."); + _logger.Fatal($"Database.Controller.DeleteQuery: Exception [{ex}] thrown, <[{ex.Message}]>."); } } } diff --git a/ChaosBot/Discord/Modules/AdminCommands.cs b/ChaosBot/Discord/Modules/AdminCommands.cs index 82f8fb1..83d3f88 100644 --- a/ChaosBot/Discord/Modules/AdminCommands.cs +++ b/ChaosBot/Discord/Modules/AdminCommands.cs @@ -1,10 +1,8 @@ using System; using Discord; -using System.Text; using Discord.Commands; using System.Threading.Tasks; using System.Collections.Generic; -using Microsoft.Extensions.Configuration; using NLog; @@ -23,16 +21,16 @@ namespace ChaosBot.Discord.Modules { try { - IEnumerable messages = await Context.Channel.GetMessagesAsync(msgtoDelete + 1).FlattenAsync(); - await ((ITextChannel) Context.Channel).DeleteMessagesAsync(messages); - const int delay = 3000; - IUserMessage m = await ReplyAsync($"{msgtoDelete} messages deleted."); - await Task.Delay(delay); - await m.DeleteAsync(); + IEnumerable messages = await Context.Channel.GetMessagesAsync(msgtoDelete + 1).FlattenAsync(); + await ((ITextChannel) Context.Channel).DeleteMessagesAsync(messages); + const int delay = 3000; + IUserMessage m = await ReplyAsync($"{msgtoDelete} messages deleted."); + await Task.Delay(delay); + await m.DeleteAsync(); } catch (Exception ex) { - _logger.Error($"InfoCommands.InfoCommand: Exception [{ex}] thrown, <[{ex.Message}]>."); + _logger.Error($"AdminCommands.ClearCommand: Exception [{ex}] thrown, <[{ex.Message}]>."); } } } diff --git a/ChaosBot/Discord/Modules/DiceCommands.cs b/ChaosBot/Discord/Modules/DiceCommands.cs index 828a2a9..7386aec 100644 --- a/ChaosBot/Discord/Modules/DiceCommands.cs +++ b/ChaosBot/Discord/Modules/DiceCommands.cs @@ -44,7 +44,7 @@ namespace ChaosBot.Discord.Modules } catch (Exception ex) { - _logger.Error($"diceCommands.RollCommand: Exception [{ex}] thrown, <[{ex.Message}]>."); + _logger.Error($"DiceCommands.Roll: Exception [{ex}] thrown, <[{ex.Message}]>."); } } diff --git a/ChaosBot/Discord/Modules/RaffleSystem.cs b/ChaosBot/Discord/Modules/RaffleSystem.cs index 6cc9494..f286bec 100644 --- a/ChaosBot/Discord/Modules/RaffleSystem.cs +++ b/ChaosBot/Discord/Modules/RaffleSystem.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Text; using System.Threading.Tasks; using ChaosBot.Database.Entity; @@ -52,7 +51,7 @@ namespace ChaosBot.Discord.Modules } catch (Exception ex) { - _logger.Error($"InfoCommands.InfoCommand: Exception [{ex}] thrown, <[{ex.Message}]>."); + _logger.Error($"RaffleSystem.RaffleCommandInfo: Exception [{ex}] thrown, <[{ex.Message}]>."); } } @@ -128,7 +127,7 @@ namespace ChaosBot.Discord.Modules } catch (Exception ex) { - _logger.Error($"InfoCommands.InfoCommand: Exception [{ex}] thrown, <[{ex.Message}]>."); + _logger.Error($"RaffleSystem.RaffleCommandHelper: Exception [{ex}] thrown, <[{ex.Message}]>."); } } diff --git a/ChaosBot/Program.cs b/ChaosBot/Program.cs index e882adb..11d40b7 100644 --- a/ChaosBot/Program.cs +++ b/ChaosBot/Program.cs @@ -1,6 +1,5 @@ using NLog; using System; -using System.Reflection; using ChaosBot.Discord; using System.Threading.Tasks; using ChaosBot.Attribute; From c556edccff29c919b975f8a68587e466aed44d12 Mon Sep 17 00:00:00 2001 From: Ben Yost Date: Thu, 4 Jun 2020 22:25:53 -0400 Subject: [PATCH 3/7] Started points system. --- .gitignore | 1 + ChaosBot/Database/Entity/Points.cs | 32 ++++ .../Database/Repository/PointsRepository.cs | 146 ++++++++++++++++++ ChaosBot/Discord/Modules/PointsCommands.cs | 84 ++++++++++ 4 files changed, 263 insertions(+) create mode 100644 ChaosBot/Database/Entity/Points.cs create mode 100644 ChaosBot/Database/Repository/PointsRepository.cs create mode 100644 ChaosBot/Discord/Modules/PointsCommands.cs diff --git a/.gitignore b/.gitignore index a800e90..4295aab 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ obj/ .idea/ /ChaosBot/appsettings.json /ChaosBot/ChaosBotSQL.db +/.vs diff --git a/ChaosBot/Database/Entity/Points.cs b/ChaosBot/Database/Entity/Points.cs new file mode 100644 index 0000000..a51a027 --- /dev/null +++ b/ChaosBot/Database/Entity/Points.cs @@ -0,0 +1,32 @@ +using ChaosBot.Attribute; + +namespace ChaosBot.Database.Entity +{ + [DBEntity("PointsTable")] + public class Points + { + [DBPrimaryKey] + [DBAutoIncrement] + [DBNotNull] + [DBUnique] + public int id { get; } + public int points { get; private set; } + public string userId { get; private set; } + public string guildId { get; private set; } + + public Points(int id, string userId, string guildId, int points) + { + this.id = id; + this.userId = userId; + this.guildId = guildId; + this.points = points; + } + + public Points(string userId, string guildId, int points) + { + this.points = points; + this.userId = userId; + this.guildId = guildId; + } + } +} \ No newline at end of file diff --git a/ChaosBot/Database/Repository/PointsRepository.cs b/ChaosBot/Database/Repository/PointsRepository.cs new file mode 100644 index 0000000..a8d8152 --- /dev/null +++ b/ChaosBot/Database/Repository/PointsRepository.cs @@ -0,0 +1,146 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Drawing; +using System.Linq; +using ChaosBot.Database.Entity; +using Microsoft.Data.Sqlite; + +namespace ChaosBot.Database.Repository +{ + public static class PointsRepository + { + private static readonly string Table = "PointsTable"; + + public static Points[] All() + { + DataTable dataTable = Controller.SelectQuery(Table); + + List pointslist = new List(); + foreach (DataRow row in dataTable.Rows) + { + int id = Convert.ToInt32((long) row["id"]); + int points = Convert.ToInt32(row["points"]); + string userId = row["userId"].ToString(); + string guildId = row["guildId"].ToString(); + + pointslist.Add(new Points(id, userId, guildId, points)); + } + + return pointslist.ToArray(); + } + + public static Points[] All(string guildId) + { + Dictionary filterDict = new Dictionary(); + filterDict.Add("guildId", guildId); + + DataTable dataTable = Controller.SelectQuery(Table, filterColumns: filterDict); + + List pointslist = new List(); + foreach (DataRow row in dataTable.Rows) + { + int idFetch = Convert.ToInt32((long) row["id"]); + int pointsFetch = Convert.ToInt32(row["points"]); + string userIdFetch = row["userId"].ToString(); + string guildIdFetch = row["guildId"].ToString(); + + pointslist.Add(new Points(idFetch, userIdFetch, guildIdFetch ,pointsFetch)); + } + + return pointslist.ToArray(); + } + + + public static int Total(string userId, string guildId) + { + Dictionary selectfilterDict = new Dictionary(); + selectfilterDict.Add("userId", userId); + selectfilterDict.Add("guildId", guildId); + + DataTable dt = Controller.SelectQuery(Table, "points", selectfilterDict); + + if (Convert.ToInt32(dt.Rows.Count) != 0) + return Convert.ToInt32(dt.Rows[0]["points"]); + else + { + Dictionary dict = new Dictionary(); + dict.Add("userId", userId); + dict.Add("guildId", guildId); + dict.Add("points", 0); + + Controller.InsertQuery(Table, dict); + } + + return 0; + } + + + + + + + + + public static int Add(int userId, int points, int guildId) + { + Dictionary selectfilterDict = new Dictionary(); + selectfilterDict.Add("userId", userId); + selectfilterDict.Add("guildId", guildId); + + int curPoints = Convert.ToInt32(Controller.SelectQuery(Table, "points", selectfilterDict).Rows[0]["points"]); + + + Console.WriteLine($"{curPoints}"); + + + + Dictionary filterDict = new Dictionary(); + filterDict.Add("userId", userId); + filterDict.Add("guildId", guildId); + + Dictionary updateDict = new Dictionary(); + // updateDict.Add("points", Convert.ToInt32(dataTable.Rows[0]["points"]) + points); + return 0; + //Controller.UpdateQuery(Table, updateDict, filterDict); + //return Convert.ToInt32(dataTable.Rows[0]["points"]) + points; + } + + public static void ClearPoints(string guildId) + { + Dictionary filterDict = new Dictionary(); + + filterDict.Add("guildId", guildId); + + Controller.DeleteQuery(Table, filterDict); + } + + public static void Delete(int userId) + { + Dictionary filterDict = new Dictionary(); + filterDict.Add("userId", userId); + + Controller.DeleteQuery(Table, filterDict); + } + + public static int Remove(int userId, int points, int guildId ) + { + Dictionary selectfilterDict = new Dictionary(); + selectfilterDict.Add("userId", userId); + selectfilterDict.Add("guildId", guildId); + + DataTable dataTable = Controller.SelectQuery(Table, "points", selectfilterDict); + + Dictionary filterDict = new Dictionary(); + filterDict.Add("userId", userId); + filterDict.Add("guildId", guildId); + + Dictionary updateDict = new Dictionary(); + updateDict.Add("points", Convert.ToInt32(dataTable.Rows[0]["points"]) - points); + + // Controller.UpdateQuery(Table, updateDict, filterDict); + return Convert.ToInt32(dataTable.Rows[0]["points"]) - points; + + } + } +} \ No newline at end of file diff --git a/ChaosBot/Discord/Modules/PointsCommands.cs b/ChaosBot/Discord/Modules/PointsCommands.cs new file mode 100644 index 0000000..2e73aab --- /dev/null +++ b/ChaosBot/Discord/Modules/PointsCommands.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using ChaosBot.Database.Entity; +using Discord; +using Discord.Commands; +using Microsoft.Extensions.Configuration; +using NLog; +using ChaosBot.Database.Repository; + + +namespace ChaosBot.Discord.Modules +{ + public class PointsCommands : ModuleBase + { + private static Logger _logger = Program._logger; + + [Command("points help")] + public async Task PointsCommandInfo() + { + try + { + var sb = new StringBuilder(); + var embed = new EmbedBuilder(); + string prefix = Program.Cfg.GetValue("Discord:Prefix"); + + embed.WithColor(new Color(255, 255, 0)); + embed.Title = $"Points system"; + sb.AppendLine($"{Context.User.Mention} has requested points information."); + sb.AppendLine(); + sb.AppendLine($"Usage:"); + sb.AppendLine($"{prefix}points status"); + sb.AppendLine($"{prefix}points help"); + sb.AppendLine(); + sb.AppendLine("Moderation commands:"); + sb.AppendLine($"{prefix}points add "); + sb.AppendLine($"{prefix}point remove"); + + /* + * 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($"InfoCommands.InfoCommand: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } + [Command("points")] + [Alias("points info")] + public async Task PointsCommandTotal() + { + int cur = PointsRepository.Total(Context.User.Id.ToString(), Context.Guild.Id.ToString()); + await ReplyAsync($"You have {cur} points.", false); + + } + + /*[Command("points add")] + [RequireUserPermission(GuildPermission.ManageGuild)] + public async Task RaffleCommandAdd(string user, int amount = 1) + { + if (Program.Cfg.GetValue($"Servers:{Context.Guild.Id}:Raffle:Max") >= amount) + // await RaffleCommandHelper("add", user, amount); + else + { + await ReplyAsync( + $"You cannot give more then {Program.Cfg.GetValue($"Servers:{Context.Guild.Id}:Raffle:Max").ToString()} tickets at a time", false); + _logger.Warn($"{Context.User.Username} attempted to give {amount} tickets to {user}!"); + } + } + [Command("points remove")] + [RequireUserPermission(GuildPermission.ManageGuild)] + public async Task RaffleCommandClear() + { + await RaffleCommandHelper("clear"); + } */ + } +} From 01e46f873f88a2da65ab92d35b6c193b41955259 Mon Sep 17 00:00:00 2001 From: Ben Yost Date: Fri, 5 Jun 2020 00:29:39 -0400 Subject: [PATCH 4/7] Added points system --- ChaosBot/Database/Controller.cs | 6 +- .../Database/Repository/PointsRepository.cs | 131 +++++++++--------- ChaosBot/Discord/Modules/PointsCommands.cs | 39 ++++-- 3 files changed, 91 insertions(+), 85 deletions(-) diff --git a/ChaosBot/Database/Controller.cs b/ChaosBot/Database/Controller.cs index f506cf4..f99aceb 100644 --- a/ChaosBot/Database/Controller.cs +++ b/ChaosBot/Database/Controller.cs @@ -112,7 +112,7 @@ namespace ChaosBot.Database SqliteCommand cmd = _conn.CreateCommand(); StringBuilder commandText = new StringBuilder(); - commandText.Append("UPDATE OR FAIL "); + commandText.Append("UPDATE "); commandText.Append(table); commandText.Append(" SET "); @@ -135,11 +135,11 @@ namespace ChaosBot.Database if (filterList.Count > 0) { commandText.Append("WHERE "); - commandText.Append(string.Join(", ", filterList)); + commandText.Append(string.Join("AND ", filterList)); } cmd.CommandText = commandText.ToString(); - + Console.WriteLine(commandText.ToString()); cmd.Prepare(); cmd.ExecuteNonQuery(); diff --git a/ChaosBot/Database/Repository/PointsRepository.cs b/ChaosBot/Database/Repository/PointsRepository.cs index a8d8152..d6d063f 100644 --- a/ChaosBot/Database/Repository/PointsRepository.cs +++ b/ChaosBot/Database/Repository/PointsRepository.cs @@ -10,55 +10,55 @@ namespace ChaosBot.Database.Repository { public static class PointsRepository { - private static readonly string Table = "PointsTable"; - + private static readonly string Table = "PointsTable"; + public static Points[] All() - { + { DataTable dataTable = Controller.SelectQuery(Table); - + List pointslist = new List(); foreach (DataRow row in dataTable.Rows) { - int id = Convert.ToInt32((long) row["id"]); + int id = Convert.ToInt32((long)row["id"]); int points = Convert.ToInt32(row["points"]); string userId = row["userId"].ToString(); string guildId = row["guildId"].ToString(); - + pointslist.Add(new Points(id, userId, guildId, points)); } return pointslist.ToArray(); - } - + } + public static Points[] All(string guildId) - { + { Dictionary filterDict = new Dictionary(); filterDict.Add("guildId", guildId); - + DataTable dataTable = Controller.SelectQuery(Table, filterColumns: filterDict); - + List pointslist = new List(); foreach (DataRow row in dataTable.Rows) { - int idFetch = Convert.ToInt32((long) row["id"]); + int idFetch = Convert.ToInt32((long)row["id"]); int pointsFetch = Convert.ToInt32(row["points"]); string userIdFetch = row["userId"].ToString(); string guildIdFetch = row["guildId"].ToString(); - - pointslist.Add(new Points(idFetch, userIdFetch, guildIdFetch ,pointsFetch)); + + pointslist.Add(new Points(idFetch, userIdFetch, guildIdFetch, pointsFetch)); } return pointslist.ToArray(); - } - - + } + + public static int Total(string userId, string guildId) { - Dictionary selectfilterDict = new Dictionary(); - selectfilterDict.Add("userId", userId); - selectfilterDict.Add("guildId", guildId); + Dictionary selectfilterDict = new Dictionary(); + selectfilterDict.Add("userId", userId); + selectfilterDict.Add("guildId", guildId); - DataTable dt = Controller.SelectQuery(Table, "points", selectfilterDict); + DataTable dt = Controller.SelectQuery(Table, "points", selectfilterDict); if (Convert.ToInt32(dt.Rows.Count) != 0) return Convert.ToInt32(dt.Rows[0]["points"]); @@ -74,73 +74,70 @@ namespace ChaosBot.Database.Repository return 0; } - - - - - - - - public static int Add(int userId, int points, int guildId) + + + + + + + + public static int Add(string userId, int points, string guildId) { Dictionary selectfilterDict = new Dictionary(); selectfilterDict.Add("userId", userId); selectfilterDict.Add("guildId", guildId); + DataTable dt = Controller.SelectQuery(Table, "points", selectfilterDict); + if (Convert.ToInt32(dt.Rows.Count) != 0) + { + Dictionary filterDict = new Dictionary(); + filterDict.Add("userId", userId); + filterDict.Add("guildId", guildId); - int curPoints = Convert.ToInt32(Controller.SelectQuery(Table, "points", selectfilterDict).Rows[0]["points"]); + Dictionary updateDict = new Dictionary(); + updateDict.Add("points", Convert.ToInt32(dt.Rows[0]["points"]) + points); + Controller.UpdateQuery(Table, updateDict, filterDict); + return Convert.ToInt32(dt.Rows[0]["points"]) + points; + } + else + { + Dictionary dict = new Dictionary(); + dict.Add("userId", userId); + dict.Add("guildId", guildId); + dict.Add("points", points); - Console.WriteLine($"{curPoints}"); - + Controller.InsertQuery(Table, dict); + return points; + } + } + public static int Remove(string userId, int points, string guildId) + { + Dictionary selectfilterDict = new Dictionary(); + selectfilterDict.Add("userId", userId); + selectfilterDict.Add("guildId", guildId); + DataTable dt = Controller.SelectQuery(Table, "points", selectfilterDict); Dictionary filterDict = new Dictionary(); filterDict.Add("userId", userId); filterDict.Add("guildId", guildId); Dictionary updateDict = new Dictionary(); - // updateDict.Add("points", Convert.ToInt32(dataTable.Rows[0]["points"]) + points); - return 0; - //Controller.UpdateQuery(Table, updateDict, filterDict); - //return Convert.ToInt32(dataTable.Rows[0]["points"]) + points; + updateDict.Add("points", Convert.ToInt32(dt.Rows[0]["points"]) - points); + Controller.UpdateQuery(Table, updateDict, filterDict); + return Convert.ToInt32(dt.Rows[0]["points"]) - points; + + } - public static void ClearPoints(string guildId) - { - Dictionary filterDict = new Dictionary(); - - filterDict.Add("guildId", guildId); - - Controller.DeleteQuery(Table, filterDict); - } - public static void Delete(int userId) - { + { Dictionary filterDict = new Dictionary(); filterDict.Add("userId", userId); - + Controller.DeleteQuery(Table, filterDict); } - public static int Remove(int userId, int points, int guildId ) - { - Dictionary selectfilterDict = new Dictionary(); - selectfilterDict.Add("userId", userId); - selectfilterDict.Add("guildId", guildId); - - DataTable dataTable = Controller.SelectQuery(Table, "points", selectfilterDict); - - Dictionary filterDict = new Dictionary(); - filterDict.Add("userId", userId); - filterDict.Add("guildId", guildId); - - Dictionary updateDict = new Dictionary(); - updateDict.Add("points", Convert.ToInt32(dataTable.Rows[0]["points"]) - points); - - // Controller.UpdateQuery(Table, updateDict, filterDict); - return Convert.ToInt32(dataTable.Rows[0]["points"]) - points; - - } } -} \ No newline at end of file +} diff --git a/ChaosBot/Discord/Modules/PointsCommands.cs b/ChaosBot/Discord/Modules/PointsCommands.cs index 2e73aab..b86d70b 100644 --- a/ChaosBot/Discord/Modules/PointsCommands.cs +++ b/ChaosBot/Discord/Modules/PointsCommands.cs @@ -15,7 +15,7 @@ namespace ChaosBot.Discord.Modules public class PointsCommands : ModuleBase { private static Logger _logger = Program._logger; - + [Command("points help")] public async Task PointsCommandInfo() { @@ -58,27 +58,36 @@ namespace ChaosBot.Discord.Modules { int cur = PointsRepository.Total(Context.User.Id.ToString(), Context.Guild.Id.ToString()); await ReplyAsync($"You have {cur} points.", false); - - } - /*[Command("points add")] - [RequireUserPermission(GuildPermission.ManageGuild)] + } + + [Command("points add")] + [RequireUserPermission(ChannelPermission.ManageMessages)] public async Task RaffleCommandAdd(string user, int amount = 1) { - if (Program.Cfg.GetValue($"Servers:{Context.Guild.Id}:Raffle:Max") >= amount) - // await RaffleCommandHelper("add", user, amount); - else + + if (ChannelPermissions.Text.ManageMessages) { - await ReplyAsync( - $"You cannot give more then {Program.Cfg.GetValue($"Servers:{Context.Guild.Id}:Raffle:Max").ToString()} tickets at a time", false); - _logger.Warn($"{Context.User.Username} attempted to give {amount} tickets to {user}!"); + ulong userId = Convert.ToUInt64(user.Substring(3, user.Length - 4)); + + await ReplyAsync($"{Context.User.Mention} has given <@{userId}> {amount} points for a total of {PointsRepository.Add(userId.ToString(), amount, Context.Guild.Id.ToString())} points.", false); } + else + await ReplyAsync($"NO ACCESS"); + + } + [Command("points remove")] - [RequireUserPermission(GuildPermission.ManageGuild)] - public async Task RaffleCommandClear() + [RequireUserPermission(ChannelPermission.ManageMessages)] + public async Task RaffleCommandRemove(string user, int amount = 1) { - await RaffleCommandHelper("clear"); - } */ + ulong userId = Convert.ToUInt64(user.Substring(3, user.Length - 4)); + int cur = PointsRepository.Total(Context.User.Id.ToString(), Context.Guild.Id.ToString()); + if (cur > amount) + await ReplyAsync($"{Context.User.Mention} has removed {amount} points from <@{userId}> for a total of {PointsRepository.Remove(userId.ToString(), amount, Context.Guild.Id.ToString())} points.", false); + else + await ReplyAsync($"{Context.User.Mention} has tried to remove {amount} points from <@{userId}> they only had {cur} points. None were taken...", false); + } } } From 17a59ad5161ead0e3f26816d71a2ac11aee61483 Mon Sep 17 00:00:00 2001 From: Ben Yost Date: Fri, 5 Jun 2020 20:25:04 -0400 Subject: [PATCH 5/7] 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 6/7] 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 7/7] 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}"); + } } } }