Usings and namespace cleanup

This commit is contained in:
jetsparrow 2024-11-15 19:19:40 +03:00
parent d2e8ed4d69
commit 2cc286ad9c
29 changed files with 1415 additions and 1530 deletions

View File

@ -1,17 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Perfusion;
using JetKarmaBot.Services.Handling;
using NLog;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using NLog;
using JetKarmaBot.Services.Handling;
using JetKarmaBot.Models;
namespace JetKarmaBot.Commands
namespace JetKarmaBot.Commands;
class AwardCommand : IChatCommand
{
class AwardCommand : IChatCommand
{
public IReadOnlyCollection<string> Names => new[] { "award", "revoke" };
[Inject] private Logger log;
@ -158,5 +153,4 @@ namespace JetKarmaBot.Commands
DescriptionID="jetkarmabot.award.tohelp"
}
};
}
}

View File

@ -1,15 +1,11 @@
using System.Collections.Generic;
using Perfusion;
using JetKarmaBot.Services.Handling;
using NLog;
using System.Linq;
using System.Threading.Tasks;
using JetKarmaBot.Services.Handling;
using JetKarmaBot.Models;
namespace JetKarmaBot.Commands
namespace JetKarmaBot.Commands;
class LocaleCommand : IChatCommand
{
class LocaleCommand : IChatCommand
{
public IReadOnlyCollection<string> Names => new[] { "changelocale", "locale" };
[Inject] private Logger log;
@ -76,5 +72,4 @@ namespace JetKarmaBot.Commands
DescriptionID="jetkarmabot.changelocale.localehelp"
}
};
}
}

View File

@ -1,11 +1,9 @@
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text.RegularExpressions;
namespace JetKarmaBot.Commands
namespace JetKarmaBot.Commands;
public class CommandString
{
public class CommandString
{
public CommandString(string command, params string[] parameters)
{
Command = command;
@ -47,5 +45,4 @@ namespace JetKarmaBot.Commands
if (TryParse(s, out var c)) return c;
throw new ArgumentException($"\"{s}\" is not a command");
}
}
}

View File

@ -1,15 +1,11 @@
using System.Collections.Generic;
using Perfusion;
using Microsoft.EntityFrameworkCore;
using JetKarmaBot.Services.Handling;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using JetKarmaBot.Models;
namespace JetKarmaBot.Commands
namespace JetKarmaBot.Commands;
public class CurrenciesCommand : IChatCommand
{
public class CurrenciesCommand : IChatCommand
{
[Inject] Localization Locale { get; set; }
public IReadOnlyCollection<string> Names => new[] { "currencies", "awardtypes" };
@ -29,5 +25,4 @@ namespace JetKarmaBot.Commands
.Select(x => $"{x.Symbol} ({x.CommandName}) <i>{currentLocale["jetkarmabot.awardtypes.nominative." + x.CommandName]}</i>")));
return true;
}
}
}

View File

@ -1,14 +1,9 @@
using System.Collections.Generic;
using Perfusion;
using JetKarmaBot.Services.Handling;
using Telegram.Bot.Types.Enums;
using System.Threading.Tasks;
using JetKarmaBot.Models;
using JetKarmaBot.Services.Handling;
namespace JetKarmaBot.Commands
namespace JetKarmaBot.Commands;
public class HelpCommand : IChatCommand
{
public class HelpCommand : IChatCommand
{
[Inject] Localization Locale { get; set; }
public IReadOnlyCollection<string> Names => new[] { "help" };
@ -40,5 +35,4 @@ namespace JetKarmaBot.Commands
return true;
}
}
}
}

View File

@ -1,32 +1,29 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using JetKarmaBot.Services.Handling;
using JetKarmaBot.Services.Handling;
namespace JetKarmaBot.Commands
namespace JetKarmaBot.Commands;
public interface IChatCommand
{
public interface IChatCommand
{
IReadOnlyCollection<string> Names { get; }
string Description { get; }
string DescriptionID { get; }
IReadOnlyCollection<ChatCommandArgument> Arguments { get; }
Task<bool> Execute(RequestContext ctx);
}
}
public struct ChatCommandArgument
{
public struct ChatCommandArgument
{
public string Name;
public bool Required;
public ChatCommandArgumentType Type;
public string Description;
public string DescriptionID;
}
}
public enum ChatCommandArgumentType
{
public enum ChatCommandArgumentType
{
Boolean,
String,
Integer,
}
}

View File

@ -1,15 +1,11 @@
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Perfusion;
using JetKarmaBot.Services.Handling;
using JetKarmaBot.Models;
namespace JetKarmaBot.Commands
namespace JetKarmaBot.Commands;
class LeaderboardCommand : IChatCommand
{
class LeaderboardCommand : IChatCommand
{
public IReadOnlyCollection<string> Names => new[] { "leaderboard" };
public async Task<bool> Execute(RequestContext ctx)
@ -66,5 +62,4 @@ namespace JetKarmaBot.Commands
DescriptionID= "jetkarmabot.leaderboard.awardtypehelp"
}
};
}
}

View File

@ -1,15 +1,11 @@
using System.Linq;
using System.Collections.Generic;
using Perfusion;
using JetKarmaBot.Services.Handling;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using JetKarmaBot.Services.Handling;
using JetKarmaBot.Models;
namespace JetKarmaBot.Commands
namespace JetKarmaBot.Commands;
class StatusCommand : IChatCommand
{
class StatusCommand : IChatCommand
{
public IReadOnlyCollection<string> Names => ["status"];
public async Task<bool> Execute(RequestContext ctx)
@ -52,5 +48,4 @@ namespace JetKarmaBot.Commands
public string DescriptionID => "jetkarmabot.status.help";
public IReadOnlyCollection<ChatCommandArgument> Arguments => [];
}
}

View File

@ -2,12 +2,11 @@
using Newtonsoft.Json;
using JsonNet.PrivateSettersContractResolvers;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
namespace JetKarmaBot
namespace JetKarmaBot;
public class Config : ConfigBase
{
public class Config : ConfigBase
{
public Config(string path) : base(path) { }
public string ApiKey { get; private set; }
@ -36,10 +35,10 @@ namespace JetKarmaBot
}
public TimeoutConfig Timeout { get; private set; } = new TimeoutConfig();
public bool SqlDebug { get; private set; } = false;
}
}
public abstract class ConfigBase
{
public abstract class ConfigBase
{
public ConfigBase(string path)
{
JObject configJson;
@ -73,6 +72,5 @@ namespace JetKarmaBot
System.Diagnostics.Debug.WriteLine(e);
}
}
}
}

View File

@ -1,15 +1,12 @@
using System.Collections.Generic;
namespace JetKarmaBot;
namespace JetKarmaBot
public static class IReadOnlyDictionaryExtensions
{
public static class IReadOnlyDictionaryExtensions
{
public static TValue GetOrDefault<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dict, TKey key)
{
TValue res = default(TValue);
TValue res = default;
if (key != null)
dict.TryGetValue(key, out res);
return res;
}
}
}

View File

@ -3,10 +3,10 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
// ReSharper disable once CheckNamespace
namespace JsonNet.PrivateSettersContractResolvers
namespace JsonNet.PrivateSettersContractResolvers;
public class PrivateSetterContractResolver : DefaultContractResolver
{
public class PrivateSetterContractResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var jProperty = base.CreateProperty(member, memberSerialization);
@ -17,10 +17,10 @@ namespace JsonNet.PrivateSettersContractResolvers
return jProperty;
}
}
}
public class PrivateSetterCamelCasePropertyNamesContractResolver : CamelCasePropertyNamesContractResolver
{
public class PrivateSetterCamelCasePropertyNamesContractResolver : CamelCasePropertyNamesContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var jProperty = base.CreateProperty(member, memberSerialization);
@ -31,15 +31,14 @@ namespace JsonNet.PrivateSettersContractResolvers
return jProperty;
}
}
}
internal static class MemberInfoExtensions
{
internal static class MemberInfoExtensions
{
internal static bool IsPropertyWithSetter(this MemberInfo member)
{
var property = member as PropertyInfo;
return property?.GetSetMethod(true) != null;
}
}
}

View File

@ -0,0 +1,6 @@
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Threading;
global using System.Threading.Tasks;
global using Perfusion;

View File

@ -3,20 +3,16 @@ using JetKarmaBot.Models;
using JetKarmaBot.Services;
using JetKarmaBot.Services.Handling;
using NLog;
using Perfusion;
using System;
using System.Threading;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
namespace JetKarmaBot
namespace JetKarmaBot;
public class JetKarmaBot : IDisposable
{
public class JetKarmaBot : IDisposable
{
[Inject] Config Config { get; set; }
[Inject] IContainer Container { get; set; }
[Inject] KarmaContextFactory Db { get; set; }
@ -138,5 +134,4 @@ namespace JetKarmaBot
}
#endregion
}
}

View File

@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations.Schema;
namespace JetKarmaBot.Models
namespace JetKarmaBot.Models;
public partial class Award
{
public partial class Award
{
public int AwardId { get; set; }
public long ChatId { get; set; }
public long FromId { get; set; }
@ -21,5 +19,4 @@ namespace JetKarmaBot.Models
public virtual User From { get; set; }
[ForeignKey("ToId")]
public virtual User To { get; set; }
}
}

View File

@ -1,10 +1,7 @@
using System;
using System.Collections.Generic;
namespace JetKarmaBot.Models;
namespace JetKarmaBot.Models
public partial class AwardType
{
public partial class AwardType
{
public AwardType()
{
Awards = new HashSet<Award>();
@ -17,5 +14,4 @@ namespace JetKarmaBot.Models
public string Description { get; set; }
public virtual ICollection<Award> Awards { get; set; }
}
}

View File

@ -1,10 +1,7 @@
using System;
using System.Collections.Generic;
namespace JetKarmaBot.Models;
namespace JetKarmaBot.Models
public partial class Chat
{
public partial class Chat
{
public Chat()
{
Awards = new HashSet<Award>();
@ -15,5 +12,4 @@ namespace JetKarmaBot.Models
public bool IsAdministrator { get; set; }
public virtual ICollection<Award> Awards { get; set; }
}
}

View File

@ -1,13 +1,10 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Perfusion;
using Microsoft.EntityFrameworkCore;
namespace JetKarmaBot.Models
namespace JetKarmaBot.Models;
[Transient]
public partial class KarmaContext : DbContext
{
[Transient]
public partial class KarmaContext : DbContext
{
public KarmaContext()
{
}
@ -179,5 +176,4 @@ namespace JetKarmaBot.Models
.HasDefaultValueSql("CURRENT_TIMESTAMP");
});
}
}
}

View File

@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations.Schema;
namespace JetKarmaBot.Models
namespace JetKarmaBot.Models;
public partial class User
{
public partial class User
{
public User()
{
AwardsFrom = new HashSet<Award>();
@ -19,5 +17,4 @@ namespace JetKarmaBot.Models
public virtual ICollection<Award> AwardsFrom { get; set; }
[InverseProperty("To")]
public virtual ICollection<Award> AwardsTo { get; set; }
}
}

View File

@ -1,16 +1,11 @@
using System;
using System.Reflection;
using System.Runtime.Loader;
using System.Threading;
using JetKarmaBot.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using NLog;
using Perfusion;
using JetKarmaBot.Models;
namespace JetKarmaBot
namespace JetKarmaBot;
public static class Program
{
public static class Program
{
private static Logger log = LogManager.GetCurrentClassLogger();
public enum ExitCode : int
{
@ -75,5 +70,4 @@ namespace JetKarmaBot
return (int)ExitCode.Ok;
}
}
}

View File

@ -1,16 +1,11 @@
using JetKarmaBot.Commands;
using NLog;
using Perfusion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NLog;
using Telegram.Bot;
using JetKarmaBot.Commands;
namespace JetKarmaBot.Services.Handling
namespace JetKarmaBot.Services.Handling;
public class ChatCommandRouter : IRequestHandler
{
public class ChatCommandRouter : IRequestHandler
{
public class Feature
{
public Type CommandType;
@ -107,5 +102,4 @@ namespace JetKarmaBot.Services.Handling
}
Dictionary<string, IChatCommand> commands = new Dictionary<string, IChatCommand>();
}
}

View File

@ -1,12 +1,7 @@
using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Perfusion;
namespace JetKarmaBot.Services.Handling;
namespace JetKarmaBot.Services.Handling
public class DatabaseHandler : IRequestHandler
{
public class DatabaseHandler : IRequestHandler
{
[Inject] private KarmaContextFactory Db;
[Inject] private Localization Locale;
public async Task Handle(RequestContext ctx, Func<RequestContext, Task> next)
@ -19,5 +14,4 @@ namespace JetKarmaBot.Services.Handling
await db.SaveChangesAsync();
}
}
}
}

View File

@ -1,17 +1,13 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using NLog;
using Perfusion;
namespace JetKarmaBot.Services.Handling
namespace JetKarmaBot.Services.Handling;
public interface IRequestHandler
{
public interface IRequestHandler
{
Task Handle(RequestContext ctx, Func<RequestContext, Task> next);
}
public class RequestChain : IRequestHandler
{
}
public class RequestChain : IRequestHandler
{
[Inject] private Logger log;
List<IRequestHandler> handlerStack = new List<IRequestHandler>();
public async Task Handle(RequestContext ctx, Func<RequestContext, Task> next = null)
@ -38,5 +34,4 @@ namespace JetKarmaBot.Services.Handling
log.ConditionalTrace($"Adding {handler.GetType().Name} to reqchain");
handlerStack.Add(handler);
}
}
}

View File

@ -1,17 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JetKarmaBot.Commands;
using JetKarmaBot.Models;
using Telegram.Bot;
using Telegram.Bot.Args;
using Telegram.Bot.Types;
using JetKarmaBot.Commands;
namespace JetKarmaBot.Services.Handling
namespace JetKarmaBot.Services.Handling;
public class RequestContext : IServiceProvider
{
public class RequestContext : IServiceProvider
{
public ITelegramBotClient Client { get; }
public Update EventArgs { get; }
public CommandString Command { get; }
@ -35,5 +29,4 @@ namespace JetKarmaBot.Services.Handling
parseMode: Telegram.Bot.Types.Enums.ParseMode.Html,
replyParameters: new ReplyParameters { MessageId = EventArgs.Message.MessageId }
);
}
}

View File

@ -1,12 +1,10 @@
using System;
using System.Threading.Tasks;
using JetKarmaBot.Models;
using Microsoft.EntityFrameworkCore;
using JetKarmaBot.Models;
namespace JetKarmaBot.Services.Handling
namespace JetKarmaBot.Services.Handling;
public class SaveData : IRequestHandler
{
public class SaveData : IRequestHandler
{
public async Task Handle(RequestContext ctx, Func<RequestContext, Task> next)
{
KarmaContext db = ctx.GetFeature<KarmaContext>();
@ -32,5 +30,4 @@ namespace JetKarmaBot.Services.Handling
else
(await db.Users.FindAsync(u.Id)).Username = un;
}
}
}

View File

@ -1,17 +1,11 @@
using Perfusion;
using System.Collections.Generic;
using System;
using System.Threading.Tasks;
using JetKarmaBot.Models;
using System.Threading;
using System.Linq;
using NLog;
using JetKarmaBot.Models;
namespace JetKarmaBot.Services.Handling
namespace JetKarmaBot.Services.Handling;
[Singleton]
public class TimeoutManager : IRequestHandler
{
[Singleton]
public class TimeoutManager : IRequestHandler
{
public class Feature
{
public double Multiplier = 1;
@ -129,5 +123,4 @@ namespace JetKarmaBot.Services.Handling
return (t.DeclaringType == null ? t.Namespace + "." + t.Name : getTypeName(t.DeclaringType) + "." + t.Name)
+ (t.GenericTypeArguments.Length > 0 ? "<" + string.Join(",", t.GenericTypeArguments.Select(getTypeName)) + ">" : "");
}
}
}

View File

@ -1,12 +1,10 @@
using JetKarmaBot.Models;
using Perfusion;
namespace JetKarmaBot.Services
namespace JetKarmaBot.Services;
public class KarmaContextFactory
{
public class KarmaContextFactory
{
[Inject] IContainer C { get; set; }
public KarmaContext GetContext() => C.GetInstance<KarmaContext>();
}
}

View File

@ -1,17 +1,13 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using Newtonsoft.Json.Linq;
using NLog;
using Perfusion;
namespace JetKarmaBot
namespace JetKarmaBot;
public class Localization : IReadOnlyDictionary<string, Locale>
{
public class Localization : IReadOnlyDictionary<string, Locale>
{
private Dictionary<string, Locale> locales = new Dictionary<string, Locale>();
public Localization(IContainer c)
@ -109,9 +105,9 @@ namespace JetKarmaBot
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
public class Locale : IReadOnlyDictionary<string, string>
{
}
public class Locale : IReadOnlyDictionary<string, string>
{
private Dictionary<string, string> locale;
private string localeName;
private string[] commonNames;
@ -157,15 +153,14 @@ namespace JetKarmaBot
{
return ((IReadOnlyDictionary<string, string>)locale).GetEnumerator();
}
}
[System.Serializable]
public class LocalizationException : Exception
{
}
[System.Serializable]
public class LocalizationException : Exception
{
public LocalizationException() { }
public LocalizationException(string message) : base(message) { }
public LocalizationException(string message, Exception inner) : base(message, inner) { }
protected LocalizationException(
SerializationInfo info,
StreamingContext context) : base(info, context) { }
}
}

View File

@ -1,12 +1,9 @@
using System;
using System.Linq;
using NLog;
using Perfusion;
namespace JetKarmaBot
namespace JetKarmaBot;
public class LogInfo : ObjectInfo
{
public class LogInfo : ObjectInfo
{
public override ObjectInfo Clone() => new LogInfo();
public override object GetInstance(IContainer c, Type requester = null)
@ -18,5 +15,4 @@ namespace JetKarmaBot
return (t.DeclaringType == null ? t.Namespace + "." + t.Name : getTypeName(t.DeclaringType) + "." + t.Name)
+ (t.GenericTypeArguments.Length > 0 ? "<" + string.Join(",", t.GenericTypeArguments.Select(getTypeName)) + ">" : "");
}
}
}

View File

@ -1,12 +1,10 @@
using System;
using Microsoft.Extensions.Logging;
using NLog;
using Perfusion;
namespace JetKarmaBot
namespace JetKarmaBot;
public class NLoggerFactory : ILoggerFactory
{
public class NLoggerFactory : ILoggerFactory
{
[Inject]
private NLoggerProvider c;
public void AddProvider(ILoggerProvider provider)
@ -18,9 +16,9 @@ namespace JetKarmaBot
public void Dispose()
{
}
}
public class NLoggerProvider : ILoggerProvider
{
}
public class NLoggerProvider : ILoggerProvider
{
[Inject]
private Container c;
@ -29,10 +27,10 @@ namespace JetKarmaBot
public void Dispose()
{
}
}
}
public class LoggerVirtualizer : Microsoft.Extensions.Logging.ILogger
{
public class LoggerVirtualizer : Microsoft.Extensions.Logging.ILogger
{
private Logger logger;
public LoggerVirtualizer(Logger logger)
@ -93,11 +91,10 @@ namespace JetKarmaBot
if (exception != null) logger.Log(getAppropriate(logLevel), exception, formatter(state, exception));
else logger.Log(getAppropriate(logLevel), state);
}
}
public class SomeDisposable : IDisposable
{
}
public class SomeDisposable : IDisposable
{
public void Dispose()
{
}
}
}