diff --git a/ChaosBot/Discord/Modules/ConfigCommands.cs b/ChaosBot/Discord/Modules/ConfigCommands.cs index fe7c769..815d49f 100644 --- a/ChaosBot/Discord/Modules/ConfigCommands.cs +++ b/ChaosBot/Discord/Modules/ConfigCommands.cs @@ -33,7 +33,6 @@ namespace ChaosBot.Discord.Modules } using (ChaosbotContext dbContext = new ChaosbotContext()) { - // ConfigurationRepository.SetValue(configFlag, Context.Guild.Id, value); Configuration config = dbContext.Configuration .FirstOrDefault(c => c.DiscordGuildId == Context.Guild.Id && c.Key == configFlag); // TODO: Is this warning valid? diff --git a/ChaosBot/Discord/Modules/InfoCommands.cs b/ChaosBot/Discord/Modules/InfoCommands.cs index 0f0eecb..9975add 100644 --- a/ChaosBot/Discord/Modules/InfoCommands.cs +++ b/ChaosBot/Discord/Modules/InfoCommands.cs @@ -3,6 +3,7 @@ using Discord; using System.Text; using Discord.Commands; using System.Threading.Tasks; +using ChaosBot.Repositories; using Microsoft.Extensions.Configuration; using NLog; @@ -28,7 +29,7 @@ namespace ChaosBot.Discord.Modules sb.AppendLine($"{Context.User.Mention} has requested information from {Program.AppSettingsHandler.GetValue("Bot:Name")}."); sb.AppendLine(); sb.AppendLine($"Bot Version: {Program.AppSettingsHandler.GetValue("Bot:Version")}"); - sb.AppendLine($"Bot Prefix: {Program.AppSettingsHandler.GetValue("Discord:Prefix")}"); + sb.AppendLine($"Bot Prefix: {ConfigurationRepository.GetValue("Discord:Prefix", Context.Guild.Id, "!")}"); /* * Add the string to the Embed diff --git a/ChaosBot/Discord/Modules/PointsCommands.cs b/ChaosBot/Discord/Modules/PointsCommands.cs.txt similarity index 66% rename from ChaosBot/Discord/Modules/PointsCommands.cs rename to ChaosBot/Discord/Modules/PointsCommands.cs.txt index f6bb74a..0257089 100644 --- a/ChaosBot/Discord/Modules/PointsCommands.cs +++ b/ChaosBot/Discord/Modules/PointsCommands.cs.txt @@ -1,6 +1,10 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.Text; using System.Threading.Tasks; +using ChaosBot.Migrations; +using ChaosBot.Models; using Discord; using Discord.Commands; using NLog; @@ -53,7 +57,15 @@ namespace ChaosBot.Discord.Modules [Alias("points info")] public async Task PointsCommandTotal() { - long cur = PointsRepository.Total(Convert.ToInt64(Context.User.Id), Convert.ToInt64(Context.Guild.Id)); + ulong cur; + 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; + } await ReplyAsync($"You have {cur} points.", false); } @@ -77,10 +89,18 @@ namespace ChaosBot.Discord.Modules [Command("points remove")] [RequireUserPermission(ChannelPermission.ManageMessages)] - public async Task RaffleCommandRemove(string user, long amount = 1) + public async Task RaffleCommandRemove(string user, ulong amount = 1) { ulong userId = Convert.ToUInt64(user.Substring(3, user.Length - 4)); - long cur = PointsRepository.Total(Convert.ToInt64(Context.User.Id), Convert.ToInt64(Context.Guild.Id)); + ulong cur; + 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; + } if (cur > amount) await ReplyAsync($"{Context.User.Mention} has removed {amount} points from <@{userId}> for a total of {PointsRepository.Remove(Convert.ToInt64(userId), amount, Convert.ToInt64(Context.Guild.Id))} points.", false); else @@ -93,10 +113,34 @@ namespace ChaosBot.Discord.Modules { ulong userId = Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)); - int matches = PointsRepository.Count(Convert.ToInt64(userId), Convert.ToInt64(Context.Guild.Id)); + // int matches = PointsRepository.Count(Convert.ToInt64(userId), Convert.ToInt64(Context.Guild.Id)); + + int matches; + using (ChaosbotContext dbContext = new ChaosbotContext()) + { + IQueryable points = dbContext.Points; + matches = points + .Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)) + .Where(p => p.DiscordUserId.Equals(Context.User.Id)) + .Count(); + } if (matches > 0) { - Points.Query().Where("userId", userId).Where("guildId", Context.Guild.Id).Delete(); + using (ChaosbotContext dbContext = new ChaosbotContext()) + { + IQueryable points = dbContext.Points; + List pointList = points + .Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)) + .Where(p => p.DiscordUserId.Equals(Context.User.Id)) + .ToList(); + + foreach (Point point in pointList) + { + dbContext.Remove(point); + } + + dbContext.SaveChanges(); + } string message = $"{Context.User.Mention} has removed <@{userId}> from the database."; await ReplyAsync(message, false); diff --git a/ChaosBot/Discord/Modules/RaffleSystem.cs b/ChaosBot/Discord/Modules/RaffleSystem.cs.txt similarity index 100% rename from ChaosBot/Discord/Modules/RaffleSystem.cs rename to ChaosBot/Discord/Modules/RaffleSystem.cs.txt diff --git a/ChaosBot/Repositories/ConfigurationRepository.cs b/ChaosBot/Repositories/ConfigurationRepository.cs index abab954..93fca37 100644 --- a/ChaosBot/Repositories/ConfigurationRepository.cs +++ b/ChaosBot/Repositories/ConfigurationRepository.cs @@ -2,6 +2,7 @@ using System; using System.Linq; using ChaosBot.Models; using System.Text.Json; +using Microsoft.Extensions.Configuration; namespace ChaosBot.Repositories { @@ -18,10 +19,15 @@ namespace ChaosBot.Repositories { Configuration config = dbContext.Configuration .SingleOrDefault(c => c.DiscordGuildId == guildId && c.Key == key); - if (String.IsNullOrEmpty(config.SerializedValue)) - return defaultValue; + if (config == null || string.IsNullOrEmpty(config.SerializedValue)) + return GetValueFromAppSettings(key, guildId, defaultValue); return JsonSerializer.Deserialize(config.SerializedValue); } } + + private static T GetValueFromAppSettings(string key, ulong guildId, T defaultValue) + { + return Program.AppSettingsHandler.GetValue($"Servers.{guildId}.{key}", defaultValue); + } } } \ No newline at end of file