Make errorhandler and languagehandler async, await all database actions
This commit is contained in:
parent
926a632641
commit
95618b1f8b
46 changed files with 181 additions and 137 deletions
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
using Discord.Commands;
|
||||
using Discord.Net;
|
||||
using Geekbot.net.Lib.Localization;
|
||||
|
@ -34,11 +35,11 @@ namespace Geekbot.net.Lib.ErrorHandling
|
|||
}
|
||||
}
|
||||
|
||||
public void HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def")
|
||||
public async Task HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def")
|
||||
{
|
||||
try
|
||||
{
|
||||
var errorString = errorMessage == "def" ? _translation.GetString(context.Guild.Id, "errorHandler", "SomethingWentWrong") : errorMessage;
|
||||
var errorString = errorMessage == "def" ? await _translation.GetString(context.Guild.Id, "errorHandler", "SomethingWentWrong") : errorMessage;
|
||||
var errorObj = SimpleConextConverter.ConvertContext(context);
|
||||
if (e.Message.Contains("50007")) return;
|
||||
if (e.Message.Contains("50013")) return;
|
||||
|
@ -86,9 +87,9 @@ namespace Geekbot.net.Lib.ErrorHandling
|
|||
}
|
||||
}
|
||||
|
||||
public async void HandleHttpException(HttpException e, ICommandContext context)
|
||||
public async Task HandleHttpException(HttpException e, ICommandContext context)
|
||||
{
|
||||
var errorStrings = _translation.GetDict(context, "httpErrors");
|
||||
var errorStrings = await _translation.GetDict(context, "httpErrors");
|
||||
switch(e.HttpCode)
|
||||
{
|
||||
case HttpStatusCode.Forbidden:
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Discord.Commands;
|
||||
using Discord.Net;
|
||||
|
||||
|
@ -6,7 +7,7 @@ namespace Geekbot.net.Lib.ErrorHandling
|
|||
{
|
||||
public interface IErrorHandler
|
||||
{
|
||||
void HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def");
|
||||
void HandleHttpException(HttpException e, ICommandContext context);
|
||||
Task HandleCommandException(Exception e, ICommandContext context, string errorMessage = "def");
|
||||
Task HandleHttpException(HttpException e, ICommandContext context);
|
||||
}
|
||||
}
|
12
Geekbot.net/Lib/Extensions/StringExtensions.cs
Normal file
12
Geekbot.net/Lib/Extensions/StringExtensions.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
using System.Linq;
|
||||
|
||||
namespace Geekbot.net.Lib.Extensions
|
||||
{
|
||||
public static class StringExtensions
|
||||
{
|
||||
public static string CapitalizeFirst(this string source)
|
||||
{
|
||||
return source.First().ToString().ToUpper() + source.Substring(1);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Geekbot.net.Database;
|
||||
using Geekbot.net.Database.Models;
|
||||
|
||||
|
@ -13,7 +14,7 @@ namespace Geekbot.net.Lib.GlobalSettings
|
|||
_database = database;
|
||||
}
|
||||
|
||||
public bool SetKey(string keyName, string value)
|
||||
public async Task<bool> SetKey(string keyName, string value)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
@ -25,13 +26,13 @@ namespace Geekbot.net.Lib.GlobalSettings
|
|||
Name = keyName,
|
||||
Value = value
|
||||
});
|
||||
_database.SaveChanges();
|
||||
await _database.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
key.Value = value;
|
||||
_database.Globals.Update(key);
|
||||
_database.SaveChanges();
|
||||
await _database.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
using Geekbot.net.Database.Models;
|
||||
using System.Threading.Tasks;
|
||||
using Geekbot.net.Database.Models;
|
||||
|
||||
namespace Geekbot.net.Lib.GlobalSettings
|
||||
{
|
||||
public interface IGlobalSettings
|
||||
{
|
||||
bool SetKey(string keyName, string value);
|
||||
Task<bool> SetKey(string keyName, string value);
|
||||
string GetKey(string keyName);
|
||||
GlobalsModel GetKeyFull(string keyName);
|
||||
}
|
||||
|
|
|
@ -2,6 +2,6 @@
|
|||
{
|
||||
public interface ILevelCalc
|
||||
{
|
||||
int GetLevel(int experience);
|
||||
int GetLevel(int? experience);
|
||||
}
|
||||
}
|
|
@ -19,7 +19,7 @@ namespace Geekbot.net.Lib.Levels
|
|||
_levels = levels.ToArray();
|
||||
}
|
||||
|
||||
public int GetLevel(int messages)
|
||||
public int GetLevel(int? messages)
|
||||
{
|
||||
var returnVal = 1;
|
||||
foreach (var level in _levels)
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Discord.Commands;
|
||||
|
||||
namespace Geekbot.net.Lib.Localization
|
||||
{
|
||||
public interface ITranslationHandler
|
||||
{
|
||||
string GetString(ulong guildId, string command, string stringName);
|
||||
Dictionary<string, string> GetDict(ICommandContext context);
|
||||
Dictionary<string, string> GetDict(ICommandContext context, string command);
|
||||
bool SetLanguage(ulong guildId, string language);
|
||||
Task<string> GetString(ulong guildId, string command, string stringName);
|
||||
Task<Dictionary<string, string>> GetDict(ICommandContext context);
|
||||
Task<Dictionary<string, string>> GetDict(ICommandContext context, string command);
|
||||
Task<bool> SetLanguage(ulong guildId, string language);
|
||||
List<string> SupportedLanguages { get; }
|
||||
}
|
||||
}
|
|
@ -2,8 +2,8 @@
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using Geekbot.net.Database;
|
||||
using Geekbot.net.Database.Models;
|
||||
using Geekbot.net.Lib.Extensions;
|
||||
|
@ -78,7 +78,7 @@ namespace Geekbot.net.Lib.Localization
|
|||
}
|
||||
}
|
||||
|
||||
private string GetServerLanguage(ulong guildId)
|
||||
private async Task<string> GetServerLanguage(ulong guildId)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
@ -94,7 +94,7 @@ namespace Geekbot.net.Lib.Localization
|
|||
}
|
||||
catch
|
||||
{
|
||||
lang = GetGuild(guildId).Language ?? "EN";
|
||||
lang = (await GetGuild(guildId)).Language ?? "EN";
|
||||
_serverLanguages[guildId] = lang;
|
||||
return lang;
|
||||
}
|
||||
|
@ -106,9 +106,9 @@ namespace Geekbot.net.Lib.Localization
|
|||
}
|
||||
}
|
||||
|
||||
public string GetString(ulong guildId, string command, string stringName)
|
||||
public async Task<string> GetString(ulong guildId, string command, string stringName)
|
||||
{
|
||||
var translation = _translations[GetServerLanguage(guildId)][command][stringName];
|
||||
var translation = _translations[await GetServerLanguage(guildId)][command][stringName];
|
||||
if (!string.IsNullOrWhiteSpace(translation)) return translation;
|
||||
translation = _translations[command][stringName]["EN"];
|
||||
if (string.IsNullOrWhiteSpace(translation))
|
||||
|
@ -118,12 +118,12 @@ namespace Geekbot.net.Lib.Localization
|
|||
return translation;
|
||||
}
|
||||
|
||||
public Dictionary<string, string> GetDict(ICommandContext context)
|
||||
public async Task<Dictionary<string, string>> GetDict(ICommandContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
var command = context.Message.Content.Split(' ').First().TrimStart('!').ToLower();
|
||||
return _translations[GetServerLanguage(context.Guild.Id)][command];
|
||||
return _translations[await GetServerLanguage(context.Guild.Id)][command];
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
@ -132,11 +132,11 @@ namespace Geekbot.net.Lib.Localization
|
|||
}
|
||||
}
|
||||
|
||||
public Dictionary<string, string> GetDict(ICommandContext context, string command)
|
||||
public async Task<Dictionary<string, string>> GetDict(ICommandContext context, string command)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _translations[GetServerLanguage(context.Guild.Id)][command];
|
||||
return _translations[await GetServerLanguage(context.Guild.Id)][command];
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
@ -145,12 +145,12 @@ namespace Geekbot.net.Lib.Localization
|
|||
}
|
||||
}
|
||||
|
||||
public bool SetLanguage(ulong guildId, string language)
|
||||
public async Task<bool> SetLanguage(ulong guildId, string language)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!_supportedLanguages.Contains(language)) return false;
|
||||
var guild = GetGuild(guildId);
|
||||
var guild = await GetGuild(guildId);
|
||||
guild.Language = language;
|
||||
_database.GuildSettings.Update(guild);
|
||||
_serverLanguages[guildId] = language;
|
||||
|
@ -165,7 +165,7 @@ namespace Geekbot.net.Lib.Localization
|
|||
|
||||
public List<string> SupportedLanguages => _supportedLanguages;
|
||||
|
||||
private GuildSettingsModel GetGuild(ulong guildId)
|
||||
private async Task<GuildSettingsModel> GetGuild(ulong guildId)
|
||||
{
|
||||
var guild = _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildId.AsLong()));
|
||||
if (guild != null) return guild;
|
||||
|
@ -173,7 +173,7 @@ namespace Geekbot.net.Lib.Localization
|
|||
{
|
||||
GuildId = guildId.AsLong()
|
||||
});
|
||||
_database.SaveChanges();
|
||||
await _database.SaveChangesAsync();
|
||||
return _database.GuildSettings.FirstOrDefault(g => g.GuildId.Equals(guildId.AsLong()));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue