Make it work, however, uncomment two commands

This commit is contained in:
Daniel_I_Am 2020-08-05 21:54:46 +02:00
parent b8a1f91d0a
commit 92a676beff
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
5 changed files with 59 additions and 9 deletions

View File

@ -33,7 +33,6 @@ namespace ChaosBot.Discord.Modules
} }
using (ChaosbotContext dbContext = new ChaosbotContext()) using (ChaosbotContext dbContext = new ChaosbotContext())
{ {
// ConfigurationRepository.SetValue<String>(configFlag, Context.Guild.Id, value);
Configuration config = dbContext.Configuration Configuration config = dbContext.Configuration
.FirstOrDefault(c => c.DiscordGuildId == Context.Guild.Id && c.Key == configFlag); .FirstOrDefault(c => c.DiscordGuildId == Context.Guild.Id && c.Key == configFlag);
// TODO: Is this warning valid? // TODO: Is this warning valid?

View File

@ -3,6 +3,7 @@ using Discord;
using System.Text; using System.Text;
using Discord.Commands; using Discord.Commands;
using System.Threading.Tasks; using System.Threading.Tasks;
using ChaosBot.Repositories;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using NLog; using NLog;
@ -28,7 +29,7 @@ namespace ChaosBot.Discord.Modules
sb.AppendLine($"{Context.User.Mention} has requested information from {Program.AppSettingsHandler.GetValue<string>("Bot:Name")}."); sb.AppendLine($"{Context.User.Mention} has requested information from {Program.AppSettingsHandler.GetValue<string>("Bot:Name")}.");
sb.AppendLine(); sb.AppendLine();
sb.AppendLine($"Bot Version: {Program.AppSettingsHandler.GetValue<string>("Bot:Version")}"); sb.AppendLine($"Bot Version: {Program.AppSettingsHandler.GetValue<string>("Bot:Version")}");
sb.AppendLine($"Bot Prefix: {Program.AppSettingsHandler.GetValue<string>("Discord:Prefix")}"); sb.AppendLine($"Bot Prefix: {ConfigurationRepository.GetValue<string>("Discord:Prefix", Context.Guild.Id, "!")}");
/* /*
* Add the string to the Embed * Add the string to the Embed

View File

@ -1,6 +1,10 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using ChaosBot.Migrations;
using ChaosBot.Models;
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using NLog; using NLog;
@ -53,7 +57,15 @@ namespace ChaosBot.Discord.Modules
[Alias("points info")] [Alias("points info")]
public async Task PointsCommandTotal() 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<Point> 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); await ReplyAsync($"You have {cur} points.", false);
} }
@ -77,10 +89,18 @@ namespace ChaosBot.Discord.Modules
[Command("points remove")] [Command("points remove")]
[RequireUserPermission(ChannelPermission.ManageMessages)] [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)); 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<Point> 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) 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); 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 else
@ -93,10 +113,34 @@ namespace ChaosBot.Discord.Modules
{ {
ulong userId = Convert.ToUInt64(userMention.Substring(3, userMention.Length - 4)); 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<Point> 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) if (matches > 0)
{ {
Points.Query().Where("userId", userId).Where("guildId", Context.Guild.Id).Delete(); using (ChaosbotContext dbContext = new ChaosbotContext())
{
IQueryable<Point> points = dbContext.Points;
List<Point> 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."; string message = $"{Context.User.Mention} has removed <@{userId}> from the database.";
await ReplyAsync(message, false); await ReplyAsync(message, false);

View File

@ -2,6 +2,7 @@ using System;
using System.Linq; using System.Linq;
using ChaosBot.Models; using ChaosBot.Models;
using System.Text.Json; using System.Text.Json;
using Microsoft.Extensions.Configuration;
namespace ChaosBot.Repositories namespace ChaosBot.Repositories
{ {
@ -18,10 +19,15 @@ namespace ChaosBot.Repositories
{ {
Configuration config = dbContext.Configuration Configuration config = dbContext.Configuration
.SingleOrDefault(c => c.DiscordGuildId == guildId && c.Key == key); .SingleOrDefault(c => c.DiscordGuildId == guildId && c.Key == key);
if (String.IsNullOrEmpty(config.SerializedValue)) if (config == null || string.IsNullOrEmpty(config.SerializedValue))
return defaultValue; return GetValueFromAppSettings(key, guildId, defaultValue);
return JsonSerializer.Deserialize<T>(config.SerializedValue); return JsonSerializer.Deserialize<T>(config.SerializedValue);
} }
} }
private static T GetValueFromAppSettings<T>(string key, ulong guildId, T defaultValue)
{
return Program.AppSettingsHandler.GetValue($"Servers.{guildId}.{key}", defaultValue);
}
} }
} }