diff --git a/ChaosBot/Discord/Modules/User/Points.cs b/ChaosBot/Discord/Modules/User/Points.cs new file mode 100644 index 0000000..f8bef62 --- /dev/null +++ b/ChaosBot/Discord/Modules/User/Points.cs @@ -0,0 +1,275 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; +using ChaosBot.Discord.PreConditions; +using ChaosBot.Migrations; +using ChaosBot.Models; +using Discord; +using Discord.Commands; +using NLog; +using ChaosBot.Repositories; +using ChaosBot.Services; +using Microsoft.EntityFrameworkCore; +using Newtonsoft.Json; + +namespace ChaosBot.Discord.Modules.User +{ + public class Points : ModuleBase + { + private static ILogger _logger = Program.Logger; + + [Command("points")] + [CheckCommandPerm("User")] + public async Task PointsCommand(string cmd = null, string userMention = null, ulong Amount = 0) + { + try + { + if ((cmd.ToLower() == "add") || (cmd.ToLower() == "+") || (cmd.ToLower() == "give")) + { + if(Amount != 0) + { + Boolean adminAccess = await CheckPermissions.CheckPerms(Context, "points.give", "Admin"); + await AddPoints(userMention, Amount, adminAccess); + } + else + { + await PointsHelp(); + } + } + else if ((cmd.ToLower() == "remove") || (cmd.ToLower() == "rem") || (cmd.ToLower() == "take") || (cmd.ToLower() == "-")) + { + if((Amount != 0) || (await CheckPermissions.CheckPerms(Context, "points.remove", "Admin"))) + { + await RemPoints(userMention, Amount); + } + else + { + await PointsHelp(); + } + } + else if ((cmd.ToLower() == "delete") || (cmd.ToLower() == "del")) + { + if((Amount == 0) || (await CheckPermissions.CheckPerms(Context, "points.remove", "Admin"))) + { + await DelPoints(userMention); + } + else + { + await PointsHelp(); + } + } + else if ((cmd.ToLower() == "=") || (cmd.ToLower() == "info") || (cmd.ToLower() == "total") || (cmd == null)) + { + await TotalPoints(); + } + else + { + await PointsHelp(); + } + } + catch (Exception ex) + { + _logger.Error( + $"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } + + public async Task PointsHelp() + { + try + { + var sb = new StringBuilder(); + var embed = new EmbedBuilder(); + + 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($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}points status"); + sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}points help"); + sb.AppendLine(); + sb.AppendLine("Moderation commands:"); + sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}points add "); + sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}point remove "); + sb.AppendLine($"{ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}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( + $"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } + + public async Task TotalPoints() + { + ulong cur = 0; + + try + { + using (ChaosbotContext dbContext = new ChaosbotContext()) + { + IQueryable points = dbContext.Points; + cur = points + .Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)) + .Where(p => p.DiscordUserId.Equals(Context.User.Id)) + .First().Amount; + } + } + catch (Exception ex) + { + _logger.Error( + $"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + + + await ReplyAsync($"{Context.User.Mention}, you have {cur} points.", false); + + } + + public async Task AddPoints(string userMention = null, ulong Amount = 0, bool admin = false) + { + ulong cur = 0; + try + { + if (admin) + { + using (ChaosbotContext dbContext = new ChaosbotContext()) + { + IQueryable ctxPoints = dbContext.Points; + + Point usrPoint = ctxPoints.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)).Where(p => p.DiscordUserId.Equals(Context.User.Id)).First(); + + cur = usrPoint.Amount + Amount; + usrPoint.Amount = cur; + usrPoint.DiscordGuildId = Context.Guild.Id; + usrPoint.DiscordUserId = Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)); + + await dbContext.Points.Upsert(usrPoint) + .On(x => usrPoint).RunAsync(); + } + + await ReplyAsync( + $"{Context.User.Mention} has added {Amount} points to <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}> for a total of {cur} points.", + false); + } + else + { + using (ChaosbotContext dbContext = new ChaosbotContext()) + { + IQueryable ctxPoints = dbContext.Points; + + Point usrPoint = ctxPoints.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)).Where(p => p.DiscordUserId.Equals(Context.User.Id)).First(); + Point rctPoint = ctxPoints.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)).Where(p => p.DiscordUserId.Equals(Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)))).First(); + + if (usrPoint.Amount >= Amount) + { + usrPoint.Amount -= Amount; + usrPoint.DiscordGuildId = Context.Guild.Id; + usrPoint.DiscordUserId = Context.User.Id; + + await dbContext.Points.Upsert(usrPoint) + .On(x => usrPoint).RunAsync(); + + cur = rctPoint.Amount + Amount; + rctPoint.Amount = cur; + rctPoint.DiscordGuildId = Context.Guild.Id; + rctPoint.DiscordUserId = Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)); + + await dbContext.Points.Upsert(rctPoint) + .On(x => rctPoint).RunAsync(); + + await ReplyAsync( + $"{Context.User.Mention} has given <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}> {Amount} points for a total of {cur} points.", + false); + + } + else + { + await ReplyAsync( + $"{Context.User.Mention}, you do not have enough points to give <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}> {Amount} points.", + false); + } + } + + } + } + catch (Exception ex) + { + _logger.Error( + $"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } + + public async Task RemPoints(string userMention = null, ulong Amount = 0) + { + ulong cur = 0; + try + { + using (ChaosbotContext dbContext = new ChaosbotContext()) + { + IQueryable ctxPoints = dbContext.Points; + + Point usrPoint = ctxPoints.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)).Where(p => p.DiscordUserId.Equals(Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)))).First(); + + cur = (usrPoint.Amount - Amount) >= 1 ? cur : 0; + usrPoint.Amount = cur; + usrPoint.DiscordGuildId = Context.Guild.Id; + usrPoint.DiscordUserId = Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)); + + await dbContext.Points.Upsert(usrPoint) + .On(x => usrPoint).RunAsync(); + } + } + catch (Exception ex) + { + _logger.Error( + $"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + + await ReplyAsync( + $"{Context.User.Mention} has taken {Amount} points from <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}> leaving them with a total of {cur} points.", + false); + } + + public async Task DelPoints(string userMention = null) + { + try + { + using (ChaosbotContext dbContext = new ChaosbotContext()) + { + Point usrPoint = new Point(); + + usrPoint.Amount = 0; + usrPoint.DiscordGuildId = Context.Guild.Id; + usrPoint.DiscordUserId = Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)); + + await dbContext.Points.Upsert(usrPoint) + .On(x => usrPoint).RunAsync(); + } + } + catch (Exception ex) + { + _logger.Error( + $"{MethodBase.GetCurrentMethod().ReflectedType.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + + await ReplyAsync( + $"{Context.User.Mention} has removed all points from <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}>.", + false); + } + } +} \ No newline at end of file