diff --git a/DiscordBot/Extensions/UserDBRepository.cs b/DiscordBot/Extensions/UserDBRepository.cs index 3824347b..b5c62837 100644 --- a/DiscordBot/Extensions/UserDBRepository.cs +++ b/DiscordBot/Extensions/UserDBRepository.cs @@ -37,8 +37,10 @@ public static class UserProps public interface IServerUserRepo { - [Sql($"INSERT INTO {UserProps.TableName} ({UserProps.UserID}) VALUES (@{UserProps.UserID})")] - Task InsertUser(ServerUser user); + [Sql($@" + INSERT INTO {UserProps.TableName} ({UserProps.UserID}) VALUES (@{UserProps.UserID}); + SELECT * FROM {UserProps.TableName} WHERE {UserProps.UserID} = @{UserProps.UserID}")] + Task InsertUser(ServerUser user); [Sql($"DELETE FROM {UserProps.TableName} WHERE {UserProps.UserID} = @userId")] Task RemoveUser(string userId); diff --git a/DiscordBot/Modules/UserModule.cs b/DiscordBot/Modules/UserModule.cs index 0cb46d00..e3c93d77 100644 --- a/DiscordBot/Modules/UserModule.cs +++ b/DiscordBot/Modules/UserModule.cs @@ -678,6 +678,12 @@ public async Task RollDice(int sides, int number) [Summary("Roll a D20 dice.")] public async Task RollD20(int number) { + if (number < 1) + { + await ReplyAsync("Invalid number. Please choose a number 1 or above.").DeleteAfterSeconds(seconds: 10); + await Context.Message.DeleteAsync(); + return; + } var roll = _random.Next(1, 21); var message = $"**{Context.User.Username}** rolled a D20 and got **{roll}**!"; if (roll > number) diff --git a/DiscordBot/Services/CurrencyService.cs b/DiscordBot/Services/CurrencyService.cs index c27f33b4..d274f5e2 100644 --- a/DiscordBot/Services/CurrencyService.cs +++ b/DiscordBot/Services/CurrencyService.cs @@ -10,6 +10,7 @@ public class CurrencyService #region Configuration private const int ApiVersion = 1; + private const string TargetDate = "latest"; private const string ValidCurrenciesEndpoint = "currencies.min.json"; private const string ExchangeRatesEndpoint = "currencies"; @@ -23,24 +24,25 @@ private class Currency private readonly Dictionary _currencies = new(); - private static readonly string ApiUrl = $"https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@{ApiVersion}/latest/"; + private static readonly string ApiUrl = $"https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@{TargetDate}/v{ApiVersion}/"; public async Task GetConversion(string toCurrency, string fromCurrency = "usd") { toCurrency = toCurrency.ToLower(); fromCurrency = fromCurrency.ToLower(); - var url = $"{ApiUrl}{ExchangeRatesEndpoint}/{fromCurrency.ToLower()}/{toCurrency.ToLower()}.min.json"; + var url = $"{ApiUrl}{ExchangeRatesEndpoint}/{fromCurrency.ToLower()}.min.json"; // Check if success var (success, response) = await WebUtil.TryGetObjectFromJson(url); if (!success) return -1; - // Check currency exists in response - response.TryGetValue($"{toCurrency}", out var value); + // json[fromCurrency][toCurrency] + var value = response.SelectToken($"{fromCurrency}.{toCurrency}"); if (value == null) return -1; + return value.Value(); } diff --git a/DiscordBot/Services/DatabaseService.cs b/DiscordBot/Services/DatabaseService.cs index 95670a13..afe61ae4 100644 --- a/DiscordBot/Services/DatabaseService.cs +++ b/DiscordBot/Services/DatabaseService.cs @@ -12,7 +12,7 @@ public class DatabaseService private readonly ILoggingService _logging; private string ConnectionString { get; } - + public IServerUserRepo Query { get; } public DatabaseService(ILoggingService logging, BotSettings settings) @@ -159,6 +159,20 @@ await _logging.LogChannelAndFile( /// Existing or newly created user. Null on database error. public async Task GetOrAddUser(SocketGuildUser socketUser) { + if (socketUser == null) + { + await _logging.Log(LogBehaviour.ConsoleChannelAndFile, + $"SocketUser is null", ExtendedLogSeverity.Warning); + return null; + } + + if (Query == null) + { + await _logging.Log(LogBehaviour.ConsoleChannelAndFile, + $"Query is null", ExtendedLogSeverity.Warning); + return null; + } + try { var user = await Query.GetUser(socketUser.Id.ToString()); @@ -170,8 +184,14 @@ public async Task GetOrAddUser(SocketGuildUser socketUser) UserID = socketUser.Id.ToString(), }; - await Query.InsertUser(user); - user = await Query.GetUser(socketUser.Id.ToString()); + user = await Query.InsertUser(user); + + if (user == null) + { + await _logging.Log(LogBehaviour.ConsoleChannelAndFile, + $"User is null after InsertUser", ExtendedLogSeverity.Warning); + return null; + } await _logging.Log(LogBehaviour.File, $"User {socketUser.GetPreferredAndUsername()} successfully added to the database."); diff --git a/DiscordBot/Services/UserService.cs b/DiscordBot/Services/UserService.cs index 86da65db..26cf478d 100644 --- a/DiscordBot/Services/UserService.cs +++ b/DiscordBot/Services/UserService.cs @@ -215,13 +215,13 @@ public async Task UpdateXp(SocketMessage messageParam) return; var userId = messageParam.Author.Id; + if (_xpCooldown.HasUser(userId)) + return; + var waitTime = _rand.Next(_xpMinCooldown, _xpMaxCooldown); float baseXp = _rand.Next(_xpMinPerMessage, _xpMaxPerMessage); float bonusXp = 0; - if (_xpCooldown.HasUser(userId)) - return; - // Add Delay and delay action by 200ms to avoid some weird database collision? _xpCooldown.AddCooldown(userId, waitTime); Task.Run(async () => diff --git a/DiscordBot/Settings/Deserialized/Settings.cs b/DiscordBot/Settings/Deserialized/Settings.cs index 6dbdf6c3..19f282b1 100644 --- a/DiscordBot/Settings/Deserialized/Settings.cs +++ b/DiscordBot/Settings/Deserialized/Settings.cs @@ -24,8 +24,8 @@ public class BotSettings #region Fun Commands - public List UserModuleSlapChoices { get; set; } = new List() { "trout", "duck", "truck", "paddle", "magikarp", "sausage", "student loan", "low poly donut" }; - + public List UserModuleSlapChoices { get; set; } = new List() { "trout", "duck", "truck", "paddle", "magikarp", "sausage", "student loan", "life choice", "bug report", "unhandled exception", "null pointer", "keyboard", "cheese wheel", "banana peel", "unresolved bug", "low poly donut" }; + #endregion // Fun Commands #region Service Enabling