Merge branch 'master' into feature-3-lodestone-linkup

This commit is contained in:
Daniel_I_Am 2020-06-06 02:51:19 +02:00
commit 270fc56430
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
11 changed files with 369 additions and 22 deletions

1
.gitignore vendored
View File

@ -7,3 +7,4 @@ obj/
/ChaosBot/appsettings.json
/ChaosBot/ChaosBotSQL.db
*.sln.DotSettings.user
/.vs

View File

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

View File

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using Discord;
using Microsoft.Data.Sqlite;
namespace ChaosBot.Attribute

View File

@ -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<string, object> values, Dictionary<string, object> parameters)
{
try
{
using (_conn)
{
_conn.Open();
SqliteCommand cmd = _conn.CreateCommand();
StringBuilder commandText = new StringBuilder();
commandText.Append("UPDATE ");
commandText.Append(table);
commandText.Append(" SET ");
List<string> updateList = new List<string>();
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<string> filterList = new List<string>();
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}]>.");
}
}
}

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,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<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 Count(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, "COUNT(*)", selectfilterDict);
return Convert.ToInt32(dt.Rows[0]["COUNT(*)"]);
}
public static int Add(string userId, int points, 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)
{
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(dt.Rows[0]["points"]) + points);
Controller.UpdateQuery(Table, updateDict, filterDict);
return Convert.ToInt32(dt.Rows[0]["points"]) + points;
}
else
{
Dictionary<string, object> dict = new Dictionary<string, object>();
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<string, object> selectfilterDict = new Dictionary<string, object>();
selectfilterDict.Add("userId", userId);
selectfilterDict.Add("guildId", guildId);
DataTable dt = 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(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<string, object> filterDict = new Dictionary<string, object>();
filterDict.Add("userId", userId);
Controller.DeleteQuery(Table, filterDict);
}
}
}

View File

@ -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;
@ -32,7 +30,7 @@ namespace ChaosBot.Discord.Modules
}
catch (Exception ex)
{
_logger.Error($"InfoCommands.InfoCommand: Exception [{ex}] thrown, <[{ex.Message}]>.");
_logger.Error($"AdminCommands.ClearCommand: Exception [{ex}] thrown, <[{ex.Message}]>.");
}
}
}

View File

@ -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}]>.");
}
}

View File

@ -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<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 <discord mention> <amount>");
sb.AppendLine($"{prefix}point delete <discord mention>");
/*
* 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<string, object> filterColumns = new Dictionary<string, object>
{
{ "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}");
}
}
}
}

View File

@ -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}]>.");
}
}

View File

@ -1,6 +1,5 @@
using NLog;
using System;
using System.Reflection;
using ChaosBot.Discord;
using System.Threading.Tasks;
using ChaosBot.Attribute;