Started points system.

This commit is contained in:
Ben Yost 2020-06-04 22:25:53 -04:00
parent 11ab5186b8
commit c556edccff
4 changed files with 263 additions and 0 deletions

1
.gitignore vendored
View File

@ -6,3 +6,4 @@ obj/
.idea/
/ChaosBot/appsettings.json
/ChaosBot/ChaosBotSQL.db
/.vs

View File

@ -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;
}
}
}

View File

@ -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<Points> pointslist = new List<Points>();
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<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("guildId", guildId);
DataTable dataTable = Controller.SelectQuery(Table, filterColumns: filterDict);
List<Points> pointslist = new List<Points>();
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<string, object> selectfilterDict = new Dictionary<string, object>();
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<string, object> dict = new Dictionary<string, object>();
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<string, object> selectfilterDict = new Dictionary<string, object>();
selectfilterDict.Add("userId", userId);
selectfilterDict.Add("guildId", guildId);
int curPoints = Convert.ToInt32(Controller.SelectQuery(Table, "points", selectfilterDict).Rows[0]["points"]);
Console.WriteLine($"{curPoints}");
Dictionary<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("userId", userId);
filterDict.Add("guildId", guildId);
Dictionary<string, object> updateDict = new Dictionary<string, object>();
// 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<string,object> filterDict = new Dictionary<string, object>();
filterDict.Add("guildId", guildId);
Controller.DeleteQuery(Table, filterDict);
}
public static void Delete(int userId)
{
Dictionary<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("userId", userId);
Controller.DeleteQuery(Table, filterDict);
}
public static int Remove(int userId, int points, int guildId )
{
Dictionary<string, object> selectfilterDict = new Dictionary<string, object>();
selectfilterDict.Add("userId", userId);
selectfilterDict.Add("guildId", guildId);
DataTable dataTable = Controller.SelectQuery(Table, "points", selectfilterDict);
Dictionary<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("userId", userId);
filterDict.Add("guildId", guildId);
Dictionary<string, object> updateDict = new Dictionary<string, object>();
updateDict.Add("points", Convert.ToInt32(dataTable.Rows[0]["points"]) - points);
// Controller.UpdateQuery(Table, updateDict, filterDict);
return Convert.ToInt32(dataTable.Rows[0]["points"]) - points;
}
}
}

View File

@ -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<string>("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 <discord mention> <amount>");
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<int>($"Servers:{Context.Guild.Id}:Raffle:Max") >= amount)
// await RaffleCommandHelper("add", user, amount);
else
{
await ReplyAsync(
$"You cannot give more then {Program.Cfg.GetValue<int>($"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");
} */
}
}