From 289de62f8f9a0c5df4415dd03aaf606151fe9179 Mon Sep 17 00:00:00 2001 From: Daniel-I-Am Date: Wed, 26 Aug 2020 23:24:23 +0200 Subject: [PATCH] Change userString parameters to SocketUser params --- ChaosBot/Discord/Modules/User/Points.cs | 123 +++++++++++---------- ChaosBot/Discord/Modules/User/RaffleCmd.cs | 38 +++---- ChaosBot/wwwroot | 2 +- 3 files changed, 81 insertions(+), 82 deletions(-) diff --git a/ChaosBot/Discord/Modules/User/Points.cs b/ChaosBot/Discord/Modules/User/Points.cs index ebe593d..11aebef 100644 --- a/ChaosBot/Discord/Modules/User/Points.cs +++ b/ChaosBot/Discord/Modules/User/Points.cs @@ -12,6 +12,7 @@ using Discord.Commands; using NLog; using ChaosBot.Repositories; using ChaosBot.Services; +using Discord.WebSocket; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; @@ -23,7 +24,7 @@ namespace ChaosBot.Discord.Modules.User [Command("points")] [CheckCommandPerm("User")] - public async Task PointsCommand(string cmd = "total", string userMention = null, ulong Amount = 0) + public async Task PointsCommand(string cmd = "total", SocketUser user = null, ulong Amount = 0) { try { @@ -32,7 +33,7 @@ namespace ChaosBot.Discord.Modules.User if(Amount != 0) { Boolean adminAccess = CheckPermissions.CheckPerms(Context, "points.add", "Admin"); - await AddPoints(userMention, Amount, adminAccess); + await AddPoints(user, Amount, adminAccess); } else { @@ -43,7 +44,7 @@ namespace ChaosBot.Discord.Modules.User { if((Amount != 0) && (CheckPermissions.CheckPerms(Context, "points.remove", "Admin"))) { - await RemPoints(userMention, Amount); + await RemPoints(user, Amount); } else { @@ -54,7 +55,7 @@ namespace ChaosBot.Discord.Modules.User { if((Amount == 0) && (CheckPermissions.CheckPerms(Context, "points.remove", "Admin"))) { - await DelPoints(userMention); + await DelPoints(user); } else { @@ -140,7 +141,52 @@ namespace ChaosBot.Discord.Modules.User } - public async Task AddPoints(string userMention = null, ulong Amount = 0, bool admin = false) + public async Task RemPoints(SocketUser user = null, ulong Amount = 0) + { + ulong cur = 0; + try + { + using (ChaosbotContext dbContext = new ChaosbotContext()) + { + IQueryable ctxPoints = dbContext.Points; + + IQueryable ctxusrPoint = ctxPoints.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)).Where(p => p.DiscordUserId.Equals(user.Id)); + + Point usrPoint; + + if(ctxusrPoint.Any()) + { + usrPoint = ctxusrPoint.First(); + if (usrPoint.Amount >= Amount) + cur = usrPoint.Amount - Amount; + else + cur = 0; + } + else + { + usrPoint = new Point(); + cur = 0; + } + usrPoint.Amount = cur; + usrPoint.DiscordGuildId = Context.Guild.Id; + usrPoint.DiscordUserId = user.Id; + + await dbContext.Points.Upsert(usrPoint) + .On(x => new { x.DiscordGuildId, x.DiscordUserId}).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 {user.Mention} leaving them with a total of {cur} points.", + false); + } + + public async Task AddPoints(SocketUser user = null, ulong Amount = 0, bool admin = false) { ulong cur = 0; try @@ -151,7 +197,7 @@ namespace ChaosBot.Discord.Modules.User { IQueryable ctxPoints = dbContext.Points; - IQueryable ctxPointCheck = ctxPoints.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)).Where(p => p.DiscordUserId.Equals(Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)))); + IQueryable ctxPointCheck = ctxPoints.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)).Where(p => p.DiscordUserId.Equals(user.Id)); Point usrPoint; if (ctxPointCheck.Any()) @@ -167,7 +213,7 @@ namespace ChaosBot.Discord.Modules.User } usrPoint.Amount = cur; usrPoint.DiscordGuildId = Context.Guild.Id; - usrPoint.DiscordUserId = Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)); + usrPoint.DiscordUserId = user.Id; await dbContext.Points.Upsert(usrPoint) .On(x => new { x.DiscordGuildId, x.DiscordUserId}).RunAsync(); @@ -175,7 +221,7 @@ namespace ChaosBot.Discord.Modules.User } await ReplyAsync( - $"{Context.User.Mention} has added {Amount} points to <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}> for a total of {cur} points.", + $"{Context.User.Mention} has added {Amount} points to {user.Mention} for a total of {cur} points.", false); } else @@ -185,7 +231,7 @@ namespace ChaosBot.Discord.Modules.User IQueryable ctxPoints = dbContext.Points; IQueryable ctxUserPoint = ctxPoints.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)).Where(p => p.DiscordUserId.Equals(Context.User.Id)); - IQueryable ctxRctPoint = ctxPoints.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)).Where(p => p.DiscordUserId.Equals(Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)))); + IQueryable ctxRctPoint = ctxPoints.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)).Where(p => p.DiscordUserId.Equals(user.Id)); if (ctxUserPoint.Any()) { @@ -213,27 +259,27 @@ namespace ChaosBot.Discord.Modules.User } rctPoint.Amount = cur; rctPoint.DiscordGuildId = Context.Guild.Id; - rctPoint.DiscordUserId = Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)); + rctPoint.DiscordUserId = user.Id; await dbContext.Points.Upsert(rctPoint) .On(x => new { x.DiscordGuildId, x.DiscordUserId}).RunAsync(); await ReplyAsync( - $"{Context.User.Mention} has given <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}> {Amount} points for a total of {cur} points.", + $"{Context.User.Mention} has given {user.Mention} {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.", + $"{Context.User.Mention}, you do not have enough points to give {user.Mention} {Amount} 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.", + $"{Context.User.Mention}, you do not have enough points to give {user.Mention} {Amount} points.", false); } } @@ -246,52 +292,7 @@ namespace ChaosBot.Discord.Modules.User } } - public async Task RemPoints(string userMention = null, ulong Amount = 0) - { - ulong cur = 0; - try - { - using (ChaosbotContext dbContext = new ChaosbotContext()) - { - IQueryable ctxPoints = dbContext.Points; - - IQueryable ctxusrPoint = ctxPoints.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)).Where(p => p.DiscordUserId.Equals(Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)))); - - Point usrPoint; - - if(ctxusrPoint.Any()) - { - usrPoint = ctxusrPoint.First(); - if (usrPoint.Amount >= Amount) - cur = usrPoint.Amount - Amount; - else - cur = 0; - } - else - { - usrPoint = new Point(); - 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 => new { x.DiscordGuildId, x.DiscordUserId}).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) + public async Task DelPoints(SocketUser user = null) { try { @@ -301,7 +302,7 @@ namespace ChaosBot.Discord.Modules.User usrPoint.Amount = 0; usrPoint.DiscordGuildId = Context.Guild.Id; - usrPoint.DiscordUserId = Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)); + usrPoint.DiscordUserId = user.Id; await dbContext.Points.Upsert(usrPoint) .On(x => new { x.DiscordGuildId, x.DiscordUserId}).RunAsync(); @@ -314,7 +315,7 @@ namespace ChaosBot.Discord.Modules.User } await ReplyAsync( - $"{Context.User.Mention} has removed all points from <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}>.", + $"{Context.User.Mention} has removed all points from {user.Mention}.", false); } } diff --git a/ChaosBot/Discord/Modules/User/RaffleCmd.cs b/ChaosBot/Discord/Modules/User/RaffleCmd.cs index 6191e1b..725429d 100644 --- a/ChaosBot/Discord/Modules/User/RaffleCmd.cs +++ b/ChaosBot/Discord/Modules/User/RaffleCmd.cs @@ -10,6 +10,7 @@ using Discord.Commands; using NLog; using ChaosBot.Repositories; using ChaosBot.Services; +using Discord.WebSocket; namespace ChaosBot.Discord.Modules.User { @@ -19,7 +20,7 @@ namespace ChaosBot.Discord.Modules.User [Command("raffle")] [CheckCommandPerm("User")] - public async Task RaffleCommand(string cmd = "total", string userMention = null, int Amount = 0) + public async Task RaffleCommand(string cmd = "total", SocketUser user = null, int Amount = 0) { try { @@ -27,7 +28,7 @@ namespace ChaosBot.Discord.Modules.User { if((Amount != 0) && (CheckPermissions.CheckPerms(Context, "raffle.add", "Admin"))) { - await AddRaffle(userMention, Amount, true); + await AddRaffle(user, Amount, true); } else { @@ -49,7 +50,7 @@ namespace ChaosBot.Discord.Modules.User { if((Amount != 0) && (CheckPermissions.CheckPerms(Context, "raffle.remove", "Admin"))) { - await RemRaffle(userMention, Amount); + await RemRaffle(user, Amount); } else { @@ -60,7 +61,7 @@ namespace ChaosBot.Discord.Modules.User { if((Amount == 0) && (CheckPermissions.CheckPerms(Context, "raffle.remove", "Admin"))) { - await DelRaffle(userMention); + await DelRaffle(user); } else { @@ -71,10 +72,6 @@ namespace ChaosBot.Discord.Modules.User { await TotalRaffle(); } - else if ((cmd.ToLower() == "clear") && (CheckPermissions.CheckPerms(Context, "raffle.clear", "Admin"))) - { - await ClearRaffle(userMention); - } else { await RaffleHelp(); @@ -154,7 +151,7 @@ namespace ChaosBot.Discord.Modules.User } - public async Task AddRaffle(string userMention = null, int Amount = 0, bool admin = false) + public async Task AddRaffle(SocketUser user = null, int Amount = 0, bool admin = false) { int cur = 0; try @@ -170,7 +167,7 @@ namespace ChaosBot.Discord.Modules.User Raffle usrRaff = new Raffle(); usrRaff.DiscordGuildId = Context.Guild.Id; - usrRaff.DiscordUserId = Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)); + usrRaff.DiscordUserId = user.Id; dbContext.Raffles.Add(usrRaff); } @@ -179,11 +176,12 @@ namespace ChaosBot.Discord.Modules.User cur = ctxRaffles .Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)) - .Where(p => p.DiscordUserId.Equals(Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)))).Count(); + .Where(p => p.DiscordUserId.Equals(user.Id)) + .Count(); } await ReplyAsync( - $"{Context.User.Mention} has added {Amount} tickets to <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}> for a total of {cur} tickets.", + $"{Context.User.Mention} has added {Amount} tickets to {user.Mention} for a total of {cur} tickets.", false); } } @@ -194,7 +192,7 @@ namespace ChaosBot.Discord.Modules.User } } - public async Task RemRaffle(string userMention = null, int Amount = 0) + public async Task RemRaffle(SocketUser user = null, int Amount = 0) { int cur = 0; try @@ -205,8 +203,7 @@ namespace ChaosBot.Discord.Modules.User IQueryable ctxRaffleDetail = ctxRaffles .Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)) - .Where(p => p.DiscordUserId.Equals( - Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)))); + .Where(p => p.DiscordUserId.Equals(user.Id)); cur = ctxRaffleDetail.Count(); @@ -231,11 +228,11 @@ namespace ChaosBot.Discord.Modules.User } await ReplyAsync( - $"{Context.User.Mention} has taken {Amount} raffle tickets from <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}> leaving them with a total of {cur-Amount} raffle tickets.", + $"{Context.User.Mention} has taken {Amount} raffle tickets from {user.Mention} leaving them with a total of {cur-Amount} raffle tickets.", false); } - public async Task DelRaffle(string userMention = null) + public async Task DelRaffle(SocketUser user = null) { try { @@ -245,8 +242,7 @@ namespace ChaosBot.Discord.Modules.User IQueryable ctxRaffleDetail = ctxRaffles .Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)) - .Where(p => p.DiscordUserId.Equals( - Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)))); + .Where(p => p.DiscordUserId.Equals(user.Id)); int cur = ctxRaffleDetail.Count(); @@ -264,10 +260,12 @@ namespace ChaosBot.Discord.Modules.User } await ReplyAsync( - $"{Context.User.Mention} has removed all raffle tickets from <@{Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4))}>.", + $"{Context.User.Mention} has removed all raffle tickets from {user.Mention}.", false); } + [Command("raffle clear")] + [CheckCommandPerm("Admin")] public async Task ClearRaffle(string confirm = null) { try diff --git a/ChaosBot/wwwroot b/ChaosBot/wwwroot index 88776d6..0185381 160000 --- a/ChaosBot/wwwroot +++ b/ChaosBot/wwwroot @@ -1 +1 @@ -Subproject commit 88776d67515e8228b31500a9e5fde663fa73ef90 +Subproject commit 018538162a471a6a028149d27f4b3e992c3c7998