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); + } } }