Merge branch 'develop' #minor

This commit is contained in:
Daniel_I_Am 2021-05-08 20:18:27 +02:00
commit 7e05a74285
No known key found for this signature in database
GPG Key ID: 80C428FCC9743E84
25 changed files with 109 additions and 71 deletions

View File

@ -17,7 +17,7 @@ namespace ChaosBot.ConfigHelpers
public static T GetValue<T>(string key, ulong guildId, T defaultValue)
{
using ChaosbotContext dbContext = new ChaosbotContext();
using DatabaseContext dbContext = new DatabaseContext();
Models.Configuration config = dbContext.Configuration
.SingleOrDefault(c => c.DiscordGuildId == guildId && c.Key == key);
if (config == null || string.IsNullOrEmpty(config.SerializedValue))
@ -27,7 +27,7 @@ namespace ChaosBot.ConfigHelpers
public static void SetValue<T>(string key, T value, ulong guildId)
{
using ChaosbotContext dbContext = new ChaosbotContext();
using DatabaseContext dbContext = new DatabaseContext();
Models.Configuration cnfSet = new Models.Configuration();
cnfSet.Key = key;
@ -41,7 +41,7 @@ namespace ChaosBot.ConfigHelpers
public static void DeleteValue(string key, ulong guildId)
{
using ChaosbotContext dbContext = new ChaosbotContext();
using DatabaseContext dbContext = new DatabaseContext();
Models.Configuration config = dbContext.Configuration
.SingleOrDefault(c => c.DiscordGuildId == guildId && c.Key == key);
if (config == null) return;

View File

@ -1,9 +1,10 @@
using ChaosBot.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
namespace ChaosBot.Models
namespace ChaosBot
{
public class ChaosbotContext : DbContext
public class DatabaseContext : DbContext
{
public DbSet<LodestoneCharacter> LodestoneCharacter { get; set; }
public DbSet<LodestoneFreeCompany> LodestoneFreeCompany { get; set; }
@ -19,31 +20,20 @@ namespace ChaosBot.Models
{
if (!optionsBuilder.IsConfigured)
{
string server, user, pass, name;
int port;
IConfiguration appSettingsHandler = Program.AppSettingsHandler;
if (Program.AppSettingsHandler == null)
{
IConfiguration config = Program.AppSettingsHandler = new ConfigurationBuilder()
appSettingsHandler = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile("./appsettings.json", optional: false, reloadOnChange: true).Build();
server = config.GetValue<string>("Database:Host");
port = config.GetValue<int>("Database:Port");
user = config.GetValue<string>("Database:User");
pass = config.GetValue<string>("Database:Pass");
name = config.GetValue<string>("Database:Name");
.AddJsonFile(Program.AppSettingsPath).Build();
}
else
{
ConfigHelpers.Configuration config = new ConfigHelpers.Configuration();
server = config.GetByKey<string>("Database:Host").GetValue(readRestricted: true);
port = config.GetByKey<int>("Database:Port").GetValue(readRestricted: true);
user = config.GetByKey<string>("Database:User").GetValue(readRestricted: true);
pass = config.GetByKey<string>("Database:Pass").GetValue(readRestricted: true);
name = config.GetByKey<string>("Database:Name").GetValue(readRestricted: true);
}
string server = appSettingsHandler.GetValue<string>("Database:Host");
int port = appSettingsHandler.GetValue<int>("Database:Port");
string user = appSettingsHandler.GetValue<string>("Database:User");
string pass = appSettingsHandler.GetValue<string>("Database:Pass");
string name = appSettingsHandler.GetValue<string>("Database:Name");
optionsBuilder.UseMySql(
$"server={server};port={port};user={user};password={pass};database={name}",

View File

@ -60,7 +60,7 @@ namespace ChaosBot.Discord.Modules.Admin
{
try
{
await using (ChaosbotContext dbContext = new ChaosbotContext())
await using (DatabaseContext dbContext = new DatabaseContext())
{
string parameterString = String.Join("", parameters);
@ -134,7 +134,7 @@ namespace ChaosBot.Discord.Modules.Admin
{
try
{
await using (ChaosbotContext dbContext = new ChaosbotContext())
await using (DatabaseContext dbContext = new DatabaseContext())
{
string parameterString = String.Join("", parameters);

View File

@ -26,7 +26,7 @@ namespace ChaosBot.Discord.Modules.User
embed.Title = $"Current Level Statistics";
sb.AppendLine();
using (ChaosbotContext dbContext = new ChaosbotContext())
using (DatabaseContext dbContext = new DatabaseContext())
{
IQueryable<Experience> ctxUser = dbContext.ExperiencePoints;
IQueryable<Experience> usrXp = ctxUser

View File

@ -165,7 +165,7 @@ namespace ChaosBot.Discord.Modules.User
}
bool linked;
using (ChaosbotContext dbContext = new ChaosbotContext())
using (DatabaseContext dbContext = new DatabaseContext())
{
IQueryable<LodestoneCharacter> ctxlsChars = dbContext.LodestoneCharacter;
IQueryable<LodestoneCharacter> lsChar = ctxlsChars.Where(p => p.DiscordGuildId.Equals(Context.Guild.Id)).Where(p => p.LodestoneId.Equals(Convert.ToUInt64(character.Character.ID)));
@ -184,7 +184,7 @@ namespace ChaosBot.Discord.Modules.User
if (character.Character.Bio.Contains(b64))
{
using (ChaosbotContext dbContext = new ChaosbotContext())
using (DatabaseContext dbContext = new DatabaseContext())
{
LodestoneCharacter lsChar = new LodestoneCharacter();

View File

@ -113,7 +113,7 @@ namespace ChaosBot.Discord.Modules.User
try
{
using (ChaosbotContext dbContext = new ChaosbotContext())
using (DatabaseContext dbContext = new DatabaseContext())
{
IQueryable<Point> points = dbContext.Points;
cur = points
@ -135,7 +135,7 @@ namespace ChaosBot.Discord.Modules.User
ulong cur = 0;
try
{
using (ChaosbotContext dbContext = new ChaosbotContext())
using (DatabaseContext dbContext = new DatabaseContext())
{
IQueryable<Point> ctxPoints = dbContext.Points;
@ -180,7 +180,7 @@ namespace ChaosBot.Discord.Modules.User
{
if (admin)
{
using (ChaosbotContext dbContext = new ChaosbotContext())
using (DatabaseContext dbContext = new DatabaseContext())
{
IQueryable<Point> ctxPoints = dbContext.Points;
@ -211,7 +211,7 @@ namespace ChaosBot.Discord.Modules.User
}
else
{
using (ChaosbotContext dbContext = new ChaosbotContext())
using (DatabaseContext dbContext = new DatabaseContext())
{
IQueryable<Point> ctxPoints = dbContext.Points;
@ -276,7 +276,7 @@ namespace ChaosBot.Discord.Modules.User
{
try
{
using (ChaosbotContext dbContext = new ChaosbotContext())
using (DatabaseContext dbContext = new DatabaseContext())
{
Point usrPoint = new Point();

View File

@ -126,7 +126,7 @@ namespace ChaosBot.Discord.Modules.User
try
{
using (ChaosbotContext dbContext = new ChaosbotContext())
using (DatabaseContext dbContext = new DatabaseContext())
{
IQueryable<Raffle> ctxRaffles = dbContext.Raffles;
@ -150,7 +150,7 @@ namespace ChaosBot.Discord.Modules.User
{
if (admin)
{
using (ChaosbotContext dbContext = new ChaosbotContext())
using (DatabaseContext dbContext = new DatabaseContext())
{
IQueryable<Raffle> ctxRaffles = dbContext.Raffles;
@ -187,7 +187,7 @@ namespace ChaosBot.Discord.Modules.User
int cur = 0;
try
{
using (ChaosbotContext dbContext = new ChaosbotContext())
using (DatabaseContext dbContext = new DatabaseContext())
{
IQueryable<Raffle> ctxRaffles = dbContext.Raffles;
@ -224,7 +224,7 @@ namespace ChaosBot.Discord.Modules.User
{
try
{
using (ChaosbotContext dbContext = new ChaosbotContext())
using (DatabaseContext dbContext = new DatabaseContext())
{
IQueryable<Raffle> ctxRaffles = dbContext.Raffles;
@ -258,7 +258,7 @@ namespace ChaosBot.Discord.Modules.User
{
if(confirm == "confirm")
{
using (ChaosbotContext dbContext = new ChaosbotContext())
using (DatabaseContext dbContext = new DatabaseContext())
{
IQueryable<Raffle> ctxRaffles = dbContext.Raffles;
@ -293,7 +293,7 @@ namespace ChaosBot.Discord.Modules.User
{
try
{
using (ChaosbotContext dbContext = new ChaosbotContext())
using (DatabaseContext dbContext = new DatabaseContext())
{
IQueryable<Raffle> ctxRaffles = dbContext.Raffles;

View File

@ -2,6 +2,7 @@
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using ChaosBot.ConfigHelpers;
using ChaosBot.Models;
using Discord;
using Discord.Commands;
@ -106,7 +107,7 @@ namespace ChaosBot.Discord.Services
{
try
{
using (ChaosbotContext dbContext = new ChaosbotContext())
using (DatabaseContext dbContext = new DatabaseContext())
{
Experience newUser = new Experience();
newUser.Amount = 0;
@ -118,9 +119,33 @@ namespace ChaosBot.Discord.Services
await dbContext.ExperiencePoints.Upsert(newUser)
.On(x => new {x.DiscordGuildId, x.DiscordUserId}).RunAsync();
}
}
catch (Exception ex)
{
LoggingFacade.Exception(ex);
}
try
{
ulong channelId = ConfigurationRepository.GetValue<ulong>("AnnounceChannel:UserJoined", user.Guild.Id, 0);
if (channelId == 0)
{
await user.Guild.SystemChannel.SendMessageAsync($"{user.Username} has joined the server.");
}
else
{
try
{
await user.Guild.GetTextChannel(channelId)
.SendMessageAsync($"{user.Username} has joined the server.");
}
catch (Exception ex)
{
LoggingFacade.Exception(ex);
}
}
}
catch (Exception ex)
{
LoggingFacade.Exception(ex);
@ -131,7 +156,7 @@ namespace ChaosBot.Discord.Services
{
try
{
using (ChaosbotContext dbContext = new ChaosbotContext())
using (DatabaseContext dbContext = new DatabaseContext())
{
IQueryable<Experience> ctxUser = dbContext.ExperiencePoints;
IQueryable<Experience> usrXp = ctxUser
@ -178,9 +203,33 @@ namespace ChaosBot.Discord.Services
await dbContext.Points.Upsert(usrPoint)
.On(x => new { x.DiscordGuildId, x.DiscordUserId}).RunAsync();
}
}
catch (Exception ex)
{
LoggingFacade.Exception(ex);
}
try
{
ulong channelId = ConfigurationRepository.GetValue<ulong>("AnnounceChannel:UserLeft", user.Guild.Id, 0);
if (channelId == 0)
{
await user.Guild.SystemChannel.SendMessageAsync($"{user.Username} has left the server.");
}
else
{
try
{
await user.Guild.GetTextChannel(channelId)
.SendMessageAsync($"{user.Username} has left the server.");
}
catch (Exception ex)
{
LoggingFacade.Exception(ex);
}
}
}
catch (Exception ex)
{
LoggingFacade.Exception(ex);

View File

@ -15,7 +15,7 @@ namespace ChaosBot.Discord.Services
{
try
{
await using ChaosbotContext dbContext = new ChaosbotContext();
await using DatabaseContext dbContext = new DatabaseContext();
string command = context.Message.Content.Substring(argPos);
IQueryable<CustomCommand> customCommandQuery = dbContext.CustomCommands;

View File

@ -18,7 +18,7 @@ namespace ChaosBot.Discord.Services
{
if (!CheckModuleEnabled.GetResult(context, "Experience")) return;
using (ChaosbotContext dbContext = new ChaosbotContext())
using (DatabaseContext dbContext = new DatabaseContext())
{
IQueryable<Experience> ctxUser = dbContext.ExperiencePoints;
IQueryable<Experience> usrXp = ctxUser

View File

@ -17,7 +17,7 @@ namespace ChaosBot.Discord.Services
if (!(optionalUser.Value is IGuildUser user)) return;
if (!(socketMessageChannel is SocketGuildChannel channel)) return;
await using ChaosbotContext dbContext = new ChaosbotContext();
await using DatabaseContext dbContext = new DatabaseContext();
IQueryable<RoleReaction> roleReactionsQueryable = dbContext.RoleReactions;
List<RoleReaction> roleReactions = roleReactionsQueryable
@ -47,7 +47,7 @@ namespace ChaosBot.Discord.Services
if (!(optionalUser.Value is IGuildUser user)) return;
if (!(socketMessageChannel is SocketGuildChannel channel)) return;
await using ChaosbotContext dbContext = new ChaosbotContext();
await using DatabaseContext dbContext = new DatabaseContext();
IQueryable<RoleReaction> roleReactionsQueryable = dbContext.RoleReactions;
List<RoleReaction> roleReactions = roleReactionsQueryable

View File

@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace ChaosBot.Migrations
{
[DbContext(typeof(ChaosbotContext))]
[DbContext(typeof(DatabaseContext))]
[Migration("20200804195804_LodestoneCharacter")]
partial class LodestoneCharacter
{

View File

@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace ChaosBot.Migrations
{
[DbContext(typeof(ChaosbotContext))]
[DbContext(typeof(DatabaseContext))]
[Migration("20200804202834_LodestoneFreeCompany")]
partial class LodestoneFreeCompany
{

View File

@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace ChaosBot.Migrations
{
[DbContext(typeof(ChaosbotContext))]
[DbContext(typeof(DatabaseContext))]
[Migration("20200804203214_Points")]
partial class Points
{

View File

@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace ChaosBot.Migrations
{
[DbContext(typeof(ChaosbotContext))]
[DbContext(typeof(DatabaseContext))]
[Migration("20200804204404_Raffles")]
partial class Raffles
{

View File

@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace ChaosBot.Migrations
{
[DbContext(typeof(ChaosbotContext))]
[DbContext(typeof(DatabaseContext))]
[Migration("20200804204734_CommandPermissions")]
partial class CommandPermissions
{

View File

@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace ChaosBot.Migrations
{
[DbContext(typeof(ChaosbotContext))]
[DbContext(typeof(DatabaseContext))]
[Migration("20200804205955_Configuration")]
partial class Configuration
{

View File

@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace ChaosBot.Migrations
{
[DbContext(typeof(ChaosbotContext))]
[DbContext(typeof(DatabaseContext))]
[Migration("20200804213001_ExperiencePoints")]
partial class ExperiencePoints
{

View File

@ -8,7 +8,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace ChaosBot.Migrations
{
[DbContext(typeof(ChaosbotContext))]
[DbContext(typeof(DatabaseContext))]
[Migration("20200819203430_RoleReaction")]
partial class RoleReaction
{

View File

@ -8,7 +8,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace ChaosBot.Migrations
{
[DbContext(typeof(ChaosbotContext))]
[DbContext(typeof(DatabaseContext))]
[Migration("20200824124830_CustomCommand")]
partial class CustomCommand
{

View File

@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace ChaosBot.Migrations
{
[DbContext(typeof(ChaosbotContext))]
[DbContext(typeof(DatabaseContext))]
partial class ChaosbotContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)

View File

@ -15,18 +15,16 @@ namespace ChaosBot
private static ILogger _logger;
public static IConfiguration AppSettingsHandler;
private static string _appSettingsPath;
public const string AppSettingsPath = "./appsettings.json";
private static void Main(string[] args)
{
_appSettingsPath = args.Length > 0 ? args[0] : "./appsettings.json";
try
{
/*
* Load configuration from AppSettings.Json and save as Cfg
*/
AppSettingsHandler = LoadConfiguration(_appSettingsPath);
AppSettingsHandler = LoadConfiguration(AppSettingsPath);
/*
* Initialize the _logger for logging purposes
@ -36,8 +34,8 @@ namespace ChaosBot
}
catch (Exception ex)
{
Console.Write($"{MethodBase.GetCurrentMethod()?.ReflectedType?.FullName}: Exception [{ex}] thrown, <[{ex.Message}]>.\n{ex.StackTrace}");
return;
LoggingFacade.Exception(ex);
Environment.Exit(1);
}
/*
@ -55,6 +53,7 @@ namespace ChaosBot
catch (Exception ex)
{
LoggingFacade.Exception(ex);
Environment.Exit(1);
}
}

View File

@ -18,7 +18,7 @@ namespace ChaosBot.Services
// Get the possible permissions
List<CommandPermission> commandPermissions;
using (ChaosbotContext dbContext = new ChaosbotContext())
using (DatabaseContext dbContext = new DatabaseContext())
{
IQueryable<CommandPermission> permissions = dbContext.CommandPermissions;
commandPermissions = permissions.Where(p => p.Command.Equals(command))

View File

@ -33,7 +33,7 @@ namespace ChaosBot.WebServer.App.ApiControllers
if (!CheckPermissions.GetResult(AccessTokenCache, Request, guildId, out IActionResult result))
return result;
await using ChaosbotContext dbContext = new ChaosbotContext();
await using DatabaseContext dbContext = new DatabaseContext();
IQueryable<T> query = GetBasicQuery(dbContext);
List<T> list = ApplyFilterForCurrentGuild(query, guildId).ToList();
@ -63,7 +63,7 @@ namespace ChaosBot.WebServer.App.ApiControllers
if (!ValidationService.Validate(requestBody, GetValidationRules(), out string errors))
return BadRequest(errors);
await using ChaosbotContext dbContext = new ChaosbotContext();
await using DatabaseContext dbContext = new DatabaseContext();
T databaseObject = SetDefaultFieldsForUpsert(new T(), guildId);
@ -95,7 +95,7 @@ namespace ChaosBot.WebServer.App.ApiControllers
if (!CheckPermissions.GetResult(AccessTokenCache, Request, guildId, out IActionResult result))
return result;
await using ChaosbotContext dbContext = new ChaosbotContext();
await using DatabaseContext dbContext = new DatabaseContext();
List<T> toDelete = FilterQueryMultipleForDeletion(GetBasicQuery(dbContext), guildId, deleteParameter);
toDelete.Add(FilterQueryForDeletion(GetBasicQuery(dbContext), guildId, deleteParameter));
@ -112,7 +112,7 @@ namespace ChaosBot.WebServer.App.ApiControllers
return NoContent();
}
protected abstract DbSet<T> GetBasicQuery(ChaosbotContext context);
protected abstract DbSet<T> GetBasicQuery(DatabaseContext context);
protected abstract IQueryable<T> ApplyFilterForCurrentGuild(IQueryable<T> query, ulong guildId);
protected abstract List<string> GetIndexFields();
protected abstract Dictionary<string, List<string>> GetValidationRules();

View File

@ -46,7 +46,7 @@ namespace ChaosBot.WebServer.App.ApiControllers
return await Delete(guildId, command);
}
protected override DbSet<CustomCommand> GetBasicQuery(ChaosbotContext context)
protected override DbSet<CustomCommand> GetBasicQuery(DatabaseContext context)
{
return context.CustomCommands;
}