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