diff --git a/.gitignore b/.gitignore index c7f5f33..4935d60 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ obj/ /ChaosBot/appsettings.json /ChaosBot/ChaosBotSQL.db *.sln.DotSettings.user +/.vs 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 103672a..f99aceb 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,57 @@ 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}]>."); + } + } + + 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 "); + 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("AND ", filterList)); + } + + cmd.CommandText = commandText.ToString(); + Console.WriteLine(commandText.ToString()); + cmd.Prepare(); + cmd.ExecuteNonQuery(); + + _conn.Close(); + } + } + catch (Exception ex) + { + _logger.Fatal($"Database.Controller.UpdateQuery: Exception [{ex}] thrown, <[{ex.Message}]>."); } } @@ -203,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; @@ -255,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/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..d6145bf --- /dev/null +++ b/ChaosBot/Database/Repository/PointsRepository.cs @@ -0,0 +1,149 @@ +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 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(); + 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); + + 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); + + 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(dt.Rows[0]["points"]) - points); + Controller.UpdateQuery(Table, updateDict, filterDict); + return Convert.ToInt32(dt.Rows[0]["points"]) - points; + + + } + + public static void Delete(int userId) + { + Dictionary filterDict = new Dictionary(); + filterDict.Add("userId", userId); + + Controller.DeleteQuery(Table, filterDict); + } + + } +} 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/PointsCommands.cs b/ChaosBot/Discord/Modules/PointsCommands.cs new file mode 100644 index 0000000..5a4bbdd --- /dev/null +++ b/ChaosBot/Discord/Modules/PointsCommands.cs @@ -0,0 +1,123 @@ +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; +using ChaosBot.Database; +using System.Data; + +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 "); + sb.AppendLine($"{prefix}point delete "); + + /* + * 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(ChannelPermission.ManageMessages)] + public async Task RaffleCommandAdd(string user, int amount = 1) + { + + if (ChannelPermissions.Text.ManageMessages) + { + 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(ChannelPermission.ManageMessages)] + public async Task RaffleCommandRemove(string user, int amount = 1) + { + 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); + } + + [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 } + }; + + int matches = PointsRepository.Count(userId.ToString(), Context.Guild.Id.ToString()); + if (matches > 0) + { + 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 + { + 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}"); + } + } + } +} 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;